View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  import java.util.Iterator;
24  import java.util.NoSuchElementException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.DoNotRetryIOException;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.client.Get;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.filter.Filter;
36  import org.apache.hadoop.util.StringUtils;
37  
38  @InterfaceAudience.Private
39  public class RowResultGenerator extends ResultGenerator {
40    private static final Log LOG = LogFactory.getLog(RowResultGenerator.class);
41  
42    private Iterator<Cell> valuesI;
43    private Cell cache;
44  
45    public RowResultGenerator(final String tableName, final RowSpec rowspec,
46        final Filter filter, final boolean cacheBlocks)
47        throws IllegalArgumentException, IOException {
48      Table table = RESTServlet.getInstance().getTable(tableName);
49      try {
50        Get get = new Get(rowspec.getRow());
51        if (rowspec.hasColumns()) {
52          for (byte[] col: rowspec.getColumns()) {
53            byte[][] split = KeyValue.parseColumn(col);
54            if (split.length == 1) {
55              get.addFamily(split[0]);
56            } else if (split.length == 2) {
57              get.addColumn(split[0], split[1]);
58            } else {
59              throw new IllegalArgumentException("Invalid column specifier.");
60            }
61          }
62        }
63        get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
64        get.setMaxVersions(rowspec.getMaxVersions());
65        if (filter != null) {
66          get.setFilter(filter);
67        }
68        get.setCacheBlocks(cacheBlocks);
69        Result result = table.get(get);
70        if (result != null && !result.isEmpty()) {
71          valuesI = result.listCells().iterator();
72        }
73      } catch (DoNotRetryIOException e) {
74        // Warn here because Stargate will return 404 in the case if multiple
75        // column families were specified but one did not exist -- currently
76        // HBase will fail the whole Get.
77        // Specifying multiple columns in a URI should be uncommon usage but
78        // help to avoid confusion by leaving a record of what happened here in
79        // the log.
80        LOG.warn(StringUtils.stringifyException(e));
81      } finally {
82        table.close();
83      }
84    }
85  
86    public void close() {
87    }
88  
89    public boolean hasNext() {
90      if (cache != null) {
91        return true;
92      }
93      if (valuesI == null) {
94        return false;
95      }
96      return valuesI.hasNext();
97    }
98  
99    public Cell next() {
100     if (cache != null) {
101       Cell kv = cache;
102       cache = null;
103       return kv;
104     }
105     if (valuesI == null) {
106       return null;
107     }
108     try {
109       return valuesI.next();
110     } catch (NoSuchElementException e) {
111       return null;
112     }
113   }
114 
115   public void putBack(Cell kv) {
116     this.cache = kv;
117   }
118 
119   public void remove() {
120     throw new UnsupportedOperationException("remove not supported");
121   }
122 }