View Javadoc

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