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.DoNotRetryIOException;
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  
39  public class RowResultGenerator extends ResultGenerator {
40    private static final Log LOG = LogFactory.getLog(RowResultGenerator.class);
41  
42    private Iterator<KeyValue> valuesI;
43    private KeyValue cache;
44  
45    public RowResultGenerator(final String tableName, final RowSpec rowspec,
46        final Filter filter) throws IllegalArgumentException, IOException {
47      HTablePool pool = RESTServlet.getInstance().getTablePool(); 
48      HTableInterface table = pool.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 == 2 && split[1].length != 0) {
55              get.addColumn(split[0], split[1]);
56            } else {
57              get.addFamily(split[0]);
58            }
59          }
60        }
61        get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
62        get.setMaxVersions(rowspec.getMaxVersions());
63        if (filter != null) {
64          get.setFilter(filter);
65        }
66        Result result = table.get(get);
67        if (result != null && !result.isEmpty()) {
68          valuesI = result.list().iterator();
69        }
70      } catch (DoNotRetryIOException e) {
71        // Warn here because Stargate will return 404 in the case if multiple
72        // column families were specified but one did not exist -- currently
73        // HBase will fail the whole Get.
74        // Specifying multiple columns in a URI should be uncommon usage but
75        // help to avoid confusion by leaving a record of what happened here in
76        // the log.
77        LOG.warn(StringUtils.stringifyException(e));
78      } finally {
79        table.close();
80      }
81    }
82  
83    public void close() {
84    }
85  
86    public boolean hasNext() {
87      if (cache != null) {
88        return true;
89      }
90      if (valuesI == null) {
91        return false;
92      }
93      return valuesI.hasNext();
94    }
95  
96    public KeyValue next() {
97      if (cache != null) {
98        KeyValue kv = cache;
99        cache = null;
100       return kv;
101     }
102     if (valuesI == null) {
103       return null;
104     }
105     try {
106       return valuesI.next();
107     } catch (NoSuchElementException e) {
108       return null;
109     }
110   }
111 
112   public void putBack(KeyValue kv) {
113     this.cache = kv;
114   }
115 
116   public void remove() {
117     throw new UnsupportedOperationException("remove not supported");
118   }
119 }