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