|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.rest; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.List; |
| 24 | +import org.apache.hadoop.hbase.CellUtil; |
| 25 | +import org.apache.hadoop.hbase.DoNotRetryIOException; |
| 26 | +import org.apache.hadoop.hbase.client.Get; |
| 27 | +import org.apache.hadoop.hbase.client.Result; |
| 28 | +import org.apache.hadoop.hbase.client.Table; |
| 29 | +import org.apache.hadoop.hbase.filter.Filter; |
| 30 | +import org.apache.hadoop.hbase.security.AccessDeniedException; |
| 31 | +import org.apache.hadoop.util.StringUtils; |
| 32 | +import org.apache.yetus.audience.InterfaceAudience; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +@InterfaceAudience.Private |
| 37 | +public class MultiRowResultReader { |
| 38 | + |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(MultiRowResultReader.class); |
| 40 | + |
| 41 | + private Result[] results; |
| 42 | + |
| 43 | + public MultiRowResultReader(final String tableName, final Collection<RowSpec> rowspecs, |
| 44 | + final Filter filter, final boolean cacheBlocks) throws IOException { |
| 45 | + try (Table table = RESTServlet.getInstance().getTable(tableName)) { |
| 46 | + List<Get> gets = new ArrayList<>(rowspecs.size()); |
| 47 | + for (RowSpec rowspec : rowspecs) { |
| 48 | + Get get = new Get(rowspec.getRow()); |
| 49 | + if (rowspec.hasColumns()) { |
| 50 | + for (byte[] col : rowspec.getColumns()) { |
| 51 | + byte[][] split = CellUtil.parseColumn(col); |
| 52 | + if (split.length == 1) { |
| 53 | + get.addFamily(split[0]); |
| 54 | + } else if (split.length == 2) { |
| 55 | + get.addColumn(split[0], split[1]); |
| 56 | + } else { |
| 57 | + throw new IllegalArgumentException("Invalid column specifier."); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime()); |
| 62 | + get.readVersions(rowspec.getMaxVersions()); |
| 63 | + if (filter != null) { |
| 64 | + get.setFilter(filter); |
| 65 | + } |
| 66 | + get.setCacheBlocks(cacheBlocks); |
| 67 | + gets.add(get); |
| 68 | + } |
| 69 | + results = table.get(gets); |
| 70 | + } catch (DoNotRetryIOException e) { |
| 71 | + // TODO this is copied from RowResultGenerator, but we probably shouldn't swallow |
| 72 | + // every type of exception but AccessDeniedException |
| 73 | + LOG.warn(StringUtils.stringifyException(e)); |
| 74 | + // Lets get the exception rethrown to get a more meaningful error message than 404 |
| 75 | + if (e instanceof AccessDeniedException) { |
| 76 | + throw e; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public Result[] getResults() { |
| 82 | + return results; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments