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.HTableInterface;
34  import org.apache.hadoop.hbase.client.Result;
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) throws IllegalArgumentException, IOException {
47      HTableInterface table = RESTServlet.getInstance().getTable(tableName);
48      try {
49        Get get = new Get(rowspec.getRow());
50        if (rowspec.hasColumns()) {
51          for (byte[] col: rowspec.getColumns()) {
52            byte[][] split = KeyValue.parseColumn(col);
53            if (split.length == 1) {
54              get.addFamily(split[0]);
55            } else if (split.length == 2) {
56              get.addColumn(split[0], split[1]);
57            } else {
58              throw new IllegalArgumentException("Invalid column specifier.");
59            }
60          }
61        }
62        get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
63        get.setMaxVersions(rowspec.getMaxVersions());
64        if (filter != null) {
65          get.setFilter(filter);
66        }
67        Result result = table.get(get);
68        if (result != null && !result.isEmpty()) {
69          valuesI = result.listCells().iterator();
70        }
71      } catch (DoNotRetryIOException e) {
72        // Warn here because Stargate will return 404 in the case if multiple
73        // column families were specified but one did not exist -- currently
74        // HBase will fail the whole Get.
75        // Specifying multiple columns in a URI should be uncommon usage but
76        // help to avoid confusion by leaving a record of what happened here in
77        // the log.
78        LOG.warn(StringUtils.stringifyException(e));
79      } finally {
80        table.close();
81      }
82    }
83  
84    public void close() {
85    }
86  
87    public boolean hasNext() {
88      if (cache != null) {
89        return true;
90      }
91      if (valuesI == null) {
92        return false;
93      }
94      return valuesI.hasNext();
95    }
96  
97    public Cell next() {
98      if (cache != null) {
99        Cell kv = cache;
100       cache = null;
101       return kv;
102     }
103     if (valuesI == null) {
104       return null;
105     }
106     try {
107       return valuesI.next();
108     } catch (NoSuchElementException e) {
109       return null;
110     }
111   }
112 
113   public void putBack(Cell kv) {
114     this.cache = kv;
115   }
116 
117   public void remove() {
118     throw new UnsupportedOperationException("remove not supported");
119   }
120 }