Java Code Examples for org.apache.hadoop.hbase.client.HTableInterface#getScanner()
The following examples show how to use
org.apache.hadoop.hbase.client.HTableInterface#getScanner() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObserverEnabler.java From Kylin with Apache License 2.0 | 6 votes |
public static ResultScanner scanWithCoprocessorIfBeneficial(CubeSegment segment, Cuboid cuboid, TupleFilter tupleFiler, // Collection<TblColRef> groupBy, Collection<RowValueDecoder> rowValueDecoders, StorageContext context, HTableInterface table, Scan scan) throws IOException { if (context.isCoprocessorEnabled() == false) { return table.getScanner(scan); } CoprocessorRowType type = CoprocessorRowType.fromCuboid(segment, cuboid); CoprocessorFilter filter = CoprocessorFilter.fromFilter(segment, tupleFiler); CoprocessorProjector projector = CoprocessorProjector.makeForObserver(segment, cuboid, groupBy); ObserverAggregators aggrs = ObserverAggregators.fromValueDecoders(rowValueDecoders); if (DEBUG_LOCAL_COPROCESSOR) { RegionScanner innerScanner = new RegionScannerAdapter(table.getScanner(scan)); AggregationScanner aggrScanner = new AggregationScanner(type, filter, projector, aggrs, innerScanner); return new ResultScannerAdapter(aggrScanner); } else { scan.setAttribute(AggregateRegionObserver.COPROCESSOR_ENABLE, new byte[] { 0x01 }); scan.setAttribute(AggregateRegionObserver.TYPE, CoprocessorRowType.serialize(type)); scan.setAttribute(AggregateRegionObserver.PROJECTOR, CoprocessorProjector.serialize(projector)); scan.setAttribute(AggregateRegionObserver.AGGREGATORS, ObserverAggregators.serialize(aggrs)); scan.setAttribute(AggregateRegionObserver.FILTER, CoprocessorFilter.serialize(filter)); return table.getScanner(scan); } }
Example 2
Source File: GridTableHBaseBenchmark.java From Kylin with Apache License 2.0 | 6 votes |
private static void fullScan(HConnection conn, boolean[] hits, Stats stats) throws IOException { HTableInterface table = conn.getTable(TEST_TABLE); try { stats.markStart(); Scan scan = new Scan(); scan.addFamily(CF); ResultScanner scanner = table.getScanner(scan); int i = 0; for (Result r : scanner) { if (hits[i]) stats.consume(r); dot(i, N_ROWS); i++; } stats.markEnd(); } finally { IOUtils.closeQuietly(table); } }
Example 3
Source File: HBaseResourceStore.java From Kylin with Apache License 2.0 | 6 votes |
private Result getByScan(String path, byte[] family, byte[] column) throws IOException { byte[] startRow = Bytes.toBytes(path); byte[] endRow = plusZero(startRow); Scan scan = new Scan(startRow, endRow); if (family == null || column == null) { scan.setFilter(new KeyOnlyFilter()); } else { scan.addColumn(family, column); } HTableInterface table = getConnection().getTable(getAllInOneTableName()); try { ResultScanner scanner = table.getScanner(scan); Result result = null; for (Result r : scanner) { result = r; } return result == null || result.isEmpty() ? null : result; } finally { IOUtils.closeQuietly(table); } }
Example 4
Source File: PhoenixRuntimeIT.java From phoenix with Apache License 2.0 | 6 votes |
private static void assertTenantIds(Expression e, HTableInterface htable, Filter filter, String[] tenantIds) throws IOException { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = htable.getScanner(scan); Result result = null; ResultTuple tuple = new ResultTuple(); List<String> actualTenantIds = Lists.newArrayListWithExpectedSize(tenantIds.length); List<String> expectedTenantIds = Arrays.asList(tenantIds); while ((result = scanner.next()) != null) { tuple.setResult(result); e.evaluate(tuple, ptr); String tenantId = (String)PVarchar.INSTANCE.toObject(ptr); actualTenantIds.add(tenantId == null ? "" : tenantId); } // Need to sort because of salting Collections.sort(actualTenantIds); assertEquals(expectedTenantIds, actualTenantIds); }
Example 5
Source File: AclService.java From Kylin with Apache License 2.0 | 5 votes |
@Override public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) { List<ObjectIdentity> oids = new ArrayList<ObjectIdentity>(); HTableInterface htable = null; try { htable = HBaseConnection.get(hbaseUrl).getTable(aclTableName); Scan scan = new Scan(); SingleColumnValueFilter parentFilter = new SingleColumnValueFilter(Bytes.toBytes(ACL_INFO_FAMILY), Bytes.toBytes(ACL_INFO_FAMILY_PARENT_COLUMN), CompareOp.EQUAL, domainObjSerializer.serialize(new DomainObjectInfo(parentIdentity))); parentFilter.setFilterIfMissing(true); scan.setFilter(parentFilter); ResultScanner scanner = htable.getScanner(scan); for (Result result = scanner.next(); result != null; result = scanner.next()) { String id = Bytes.toString(result.getRow()); String type = Bytes.toString(result.getValue(Bytes.toBytes(ACL_INFO_FAMILY), Bytes.toBytes(ACL_INFO_FAMILY_TYPE_COLUMN))); oids.add(new ObjectIdentityImpl(type, id)); } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(htable); } return oids; }
Example 6
Source File: DataJanitorState.java From phoenix-tephra with Apache License 2.0 | 5 votes |
@Nullable private TimeRegions getNextSetOfTimeRegions(HTableInterface stateTable, long time) throws IOException { byte[] timeBytes = Bytes.toBytes(getInvertedTime(time)); Scan scan = new Scan(makeTimeRegionKey(timeBytes, EMPTY_BYTE_ARRAY), REGION_TIME_KEY_PREFIX_STOP); scan.addColumn(FAMILY, REGION_TIME_COL); long currentRegionTime = -1; SortedSet<byte[]> regions = new TreeSet<>(Bytes.BYTES_COMPARATOR); Result next; try (ResultScanner scanner = stateTable.getScanner(scan)) { while ((next = scanner.next()) != null) { Map.Entry<Long, byte[]> timeRegion = getTimeRegion(next.getRow()); // Stop if reached next time value if (currentRegionTime == -1) { currentRegionTime = timeRegion.getKey(); } else if (timeRegion.getKey() < currentRegionTime) { break; } else if (timeRegion.getKey() > currentRegionTime) { throw new IllegalStateException( String.format("Got out of order time %d when expecting time less than or equal to %d", timeRegion.getKey(), currentRegionTime)); } regions.add(timeRegion.getValue()); } } return regions.isEmpty() ? null : new TimeRegions(currentRegionTime, Collections.unmodifiableSortedSet(regions)); }
Example 7
Source File: DataJanitorState.java From phoenix-tephra with Apache License 2.0 | 5 votes |
@VisibleForTesting void deleteFromScan(HTableInterface stateTable, Scan scan) throws IOException { try (ResultScanner scanner = stateTable.getScanner(scan)) { Result next; while ((next = scanner.next()) != null) { stateTable.delete(new Delete(next.getRow())); } } }
Example 8
Source File: TransactionAwareHTableTest.java From phoenix-tephra with Apache License 2.0 | 5 votes |
private void verifyScan(HTableInterface table, Scan scan, List<KeyValue> expectedCells) throws Exception { List<Cell> actualCells = new ArrayList<>(); try (ResultScanner scanner = table.getScanner(scan)) { Result[] results = scanner.next(expectedCells.size() + 1); for (Result result : results) { actualCells.addAll(Lists.newArrayList(result.rawCells())); } Assert.assertEquals(expectedCells, actualCells); } }
Example 9
Source File: GridTableHBaseBenchmark.java From Kylin with Apache License 2.0 | 5 votes |
private static void testColumnScan(HConnection conn, List<Pair<Integer, Integer>> colScans) throws IOException { Stats stats = new Stats("COLUMN_SCAN"); HTableInterface table = conn.getTable(TEST_TABLE); try { stats.markStart(); int nLogicCols = colScans.size(); int nLogicRows = colScans.get(0).getSecond() - colScans.get(0).getFirst(); Scan[] scans = new Scan[nLogicCols]; ResultScanner[] scanners = new ResultScanner[nLogicCols]; for (int i = 0; i < nLogicCols; i++) { scans[i] = new Scan(); scans[i].addFamily(CF); scanners[i] = table.getScanner(scans[i]); } for (int i = 0; i < nLogicRows; i++) { for (int c = 0; c < nLogicCols; c++) { Result r = scanners[c].next(); stats.consume(r); } dot(i, nLogicRows); } stats.markEnd(); } finally { IOUtils.closeQuietly(table); } }
Example 10
Source File: UserService.java From Kylin with Apache License 2.0 | 5 votes |
@Override public List<String> getUserAuthorities() { Scan s = new Scan(); s.addColumn(Bytes.toBytes(USER_AUTHORITY_FAMILY), Bytes.toBytes(USER_AUTHORITY_COLUMN)); List<String> authorities = new ArrayList<String>(); HTableInterface htable = null; ResultScanner scanner = null; try { htable = HBaseConnection.get(hbaseUrl).getTable(userTableName); scanner = htable.getScanner(s); for (Result result = scanner.next(); result != null; result = scanner.next()) { byte[] uaBytes = result.getValue(Bytes.toBytes(USER_AUTHORITY_FAMILY), Bytes.toBytes(USER_AUTHORITY_COLUMN)); Collection<? extends GrantedAuthority> authCollection = Arrays.asList(ugaSerializer.deserialize(uaBytes)); for (GrantedAuthority auth : authCollection) { if (!authorities.contains(auth.getAuthority())) { authorities.add(auth.getAuthority()); } } } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeQuietly(scanner); IOUtils.closeQuietly(htable); } return authorities; }
Example 11
Source File: HBaseRowDigestTest.java From Kylin with Apache License 2.0 | 4 votes |
@Test public static void test() throws IOException { String hbaseUrl = "hbase"; // use hbase-site.xml on classpath HConnection conn = null; HTableInterface table = null; try { conn = HBaseConnection.get(hbaseUrl); table = conn.getTable("KYLIN_II_YTYWP3CQGJ"); ResultScanner scanner = table.getScanner(CF, QN); StringBuffer sb = new StringBuffer(); while (true) { Result r = scanner.next(); if (r == null) break; Cell[] cells = r.rawCells(); Cell c = cells[0]; k.set(c.getRowArray(), c.getRowOffset(), c.getRowLength()); v.set(c.getValueArray(), c.getValueOffset(), c.getValueLength()); byte[] row = k.copyBytes(); byte[] value = v.copyBytes(); // byte[] row = r.getRow(); // byte[] value = r.getValue(CF, QN); // sb.append("row length: " + row.length + "\r\n"); sb.append(BytesUtil.toReadableText(row) + "\r\n"); sb.append("value length: " + value.length + "\r\n"); sb.append(BytesUtil.toReadableText(value) + "\r\n"); } System.out.println(sb.toString()); FileUtils.writeStringToFile(new File("/Users/honma/Desktop/a3"), sb.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (table != null) table.close(); if (conn != null) conn.close(); } }
Example 12
Source File: GridTableHBaseBenchmark.java From Kylin with Apache License 2.0 | 4 votes |
private static void jumpScan(HConnection conn, boolean[] hits, Stats stats) throws IOException { final int jumpThreshold = 6; // compensate for Scan() overhead, totally by experience HTableInterface table = conn.getTable(TEST_TABLE); try { stats.markStart(); int i = 0; while (i < N_ROWS) { int start, end; for (start = i; start < N_ROWS; start++) { if (hits[start]) break; } for (end = start + 1; end < N_ROWS; end++) { boolean isEnd = true; for (int j = 0; j < jumpThreshold && end + j < N_ROWS; j++) if (hits[end + j]) isEnd = false; if (isEnd) break; } if (start < N_ROWS) { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start)); scan.setStopRow(Bytes.toBytes(end)); scan.addFamily(CF); ResultScanner scanner = table.getScanner(scan); i = start; for (Result r : scanner) { stats.consume(r); dot(i, N_ROWS); i++; } } i = end; } stats.markEnd(); } finally { IOUtils.closeQuietly(table); } }
Example 13
Source File: AbstractHBaseClient.java From jstorm with Apache License 2.0 | 4 votes |
protected ResultScanner getScanner(HTableInterface table, Scan scan) throws IOException { return table.getScanner(scan); }
Example 14
Source File: GridTableHBaseBenchmark.java From Kylin with Apache License 2.0 | 4 votes |
private static void prepareData(HConnection conn) throws IOException { HTableInterface table = conn.getTable(TEST_TABLE); try { // check how many rows existing int nRows = 0; Scan scan = new Scan(); scan.setFilter(new KeyOnlyFilter()); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { r.getRow(); // nothing to do nRows++; } if (nRows > 0) { System.out.println(nRows + " existing rows"); if (nRows != N_ROWS) throw new IOException("Expect " + N_ROWS + " rows but it is not"); return; } // insert rows into empty table System.out.println("Writing " + N_ROWS + " rows to " + TEST_TABLE); long nBytes = 0; for (int i = 0; i < N_ROWS; i++) { byte[] rowkey = Bytes.toBytes(i); Put put = new Put(rowkey); byte[] cell = randomBytes(); put.add(CF, QN, cell); table.put(put); nBytes += cell.length; dot(i, N_ROWS); } System.out.println(); System.out.println("Written " + N_ROWS + " rows, " + nBytes + " bytes"); } finally { IOUtils.closeQuietly(table); } }
Example 15
Source File: MetaDataEndpointImpl.java From phoenix with Apache License 2.0 | 4 votes |
/** * @param tableName parent table's name * Looks for whether child views exist for the table specified by table. * TODO: should we pass a timestamp here? */ private TableViewFinderResult findChildViews(HRegion region, byte[] tenantId, PTable table) throws IOException { byte[] schemaName = table.getSchemaName().getBytes(); byte[] tableName = table.getTableName().getBytes(); boolean isMultiTenant = table.isMultiTenant(); Scan scan = new Scan(); // If the table is multi-tenant, we need to check across all tenant_ids, // so we can't constrain the row key. Otherwise, any views would have // the same tenantId. if (!isMultiTenant) { byte[] startRow = ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY); byte[] stopRow = ByteUtil.nextKey(startRow); scan.setStartRow(startRow); scan.setStopRow(stopRow); } SingleColumnValueFilter linkFilter = new SingleColumnValueFilter(TABLE_FAMILY_BYTES, LINK_TYPE_BYTES, CompareOp.EQUAL, PHYSICAL_TABLE_BYTES); linkFilter.setFilterIfMissing(true); byte[] suffix = ByteUtil.concat(QueryConstants.SEPARATOR_BYTE_ARRAY, SchemaUtil.getTableNameAsBytes(schemaName, tableName)); SuffixFilter rowFilter = new SuffixFilter(suffix); Filter filter = new FilterList(linkFilter, rowFilter); scan.setFilter(filter); scan.addColumn(TABLE_FAMILY_BYTES, LINK_TYPE_BYTES); // Original region-only scanner modified due to PHOENIX-1208 // RegionScanner scanner = region.getScanner(scan); // The following *should* work, but doesn't due to HBASE-11837 // TableName systemCatalogTableName = region.getTableDesc().getTableName(); // HTableInterface hTable = env.getTable(systemCatalogTableName); // These deprecated calls work around the issue HTableInterface hTable = ServerUtil.getHTableForCoprocessorScan(env, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES); try { boolean allViewsInCurrentRegion = true; int numOfChildViews = 0; List<Result> results = Lists.newArrayList(); ResultScanner scanner = hTable.getScanner(scan); try { for (Result result = scanner.next(); (result != null); result = scanner.next()) { numOfChildViews++; ImmutableBytesWritable ptr = new ImmutableBytesWritable(); ResultTuple resultTuple = new ResultTuple(result); resultTuple.getKey(ptr); byte[] key = ptr.copyBytes(); if (checkTableKeyInRegion(key, region) != null) { allViewsInCurrentRegion = false; } results.add(result); } TableViewFinderResult tableViewFinderResult = new TableViewFinderResult(results); if (numOfChildViews > 0 && !allViewsInCurrentRegion) { tableViewFinderResult.setAllViewsNotInSingleRegion(); } return tableViewFinderResult; } finally { scanner.close(); } } finally { hTable.close(); } }
Example 16
Source File: StatisticsUtil.java From phoenix with Apache License 2.0 | 4 votes |
public static PTableStats readStatistics(HTableInterface statsHTable, byte[] tableNameBytes, long clientTimeStamp) throws IOException { ImmutableBytesWritable ptr = new ImmutableBytesWritable(); Scan s = MetaDataUtil.newTableRowsScan(tableNameBytes, MetaDataProtocol.MIN_TABLE_TIMESTAMP, clientTimeStamp); s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_BYTES); s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT_BYTES); ResultScanner scanner = statsHTable.getScanner(s); Result result = null; long timeStamp = MetaDataProtocol.MIN_TABLE_TIMESTAMP; TreeMap<byte[], GuidePostsInfo> guidePostsPerCf = new TreeMap<byte[], GuidePostsInfo>( Bytes.BYTES_COMPARATOR); while ((result = scanner.next()) != null) { CellScanner cellScanner = result.cellScanner(); long rowCount = 0; ImmutableBytesPtr valuePtr = new ImmutableBytesPtr(HConstants.EMPTY_BYTE_ARRAY); byte[] cfName = null; int tableNameLength; int cfOffset; int cfLength; boolean valuesSet = false; // Only the two cells with quals GUIDE_POSTS_ROW_COUNT_BYTES and GUIDE_POSTS_BYTES would be retrieved while (cellScanner.advance()) { Cell current = cellScanner.current(); if (!valuesSet) { tableNameLength = tableNameBytes.length + 1; cfOffset = current.getRowOffset() + tableNameLength; cfLength = getVarCharLength(current.getRowArray(), cfOffset, current.getRowLength() - tableNameLength); ptr.set(current.getRowArray(), cfOffset, cfLength); valuesSet = true; } cfName = ByteUtil.copyKeyBytesIfNecessary(ptr); if (Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT_BYTES, 0, PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT_BYTES.length)) { rowCount = PLong.INSTANCE.getCodec().decodeLong(current.getValueArray(), current.getValueOffset(), SortOrder.getDefault()); } else { valuePtr.set(current.getValueArray(), current.getValueOffset(), current.getValueLength()); } if (current.getTimestamp() > timeStamp) { timeStamp = current.getTimestamp(); } } if (cfName != null) { GuidePostsInfo newGPInfo = GuidePostsInfo.deserializeGuidePostsInfo( valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength(), rowCount); GuidePostsInfo oldInfo = guidePostsPerCf.put(cfName, newGPInfo); if (oldInfo != null) { newGPInfo.combine(oldInfo); } } } if (!guidePostsPerCf.isEmpty()) { return new PTableStatsImpl(guidePostsPerCf, timeStamp); } return PTableStats.EMPTY_STATS; }
Example 17
Source File: PingHBaseCLI.java From Kylin with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws IOException { String metadataUrl = args[0]; String hbaseTable = args[1]; System.out.println("Hello friend."); Configuration hconf = HadoopUtil.newHBaseConfiguration(metadataUrl); if (User.isHBaseSecurityEnabled(hconf)) { try { System.out.println("--------------Getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName()); TokenUtil.obtainAndCacheToken(hconf, UserGroupInformation.getCurrentUser()); } catch (InterruptedException e) { System.out.println("--------------Error while getting kerberos credential for user " + UserGroupInformation.getCurrentUser().getUserName()); } } Scan scan = new Scan(); int limit = 20; HConnection conn = null; HTableInterface table = null; ResultScanner scanner = null; try { conn = HConnectionManager.createConnection(hconf); table = conn.getTable(hbaseTable); scanner = table.getScanner(scan); int count = 0; for (Result r : scanner) { byte[] rowkey = r.getRow(); System.out.println(Bytes.toStringBinary(rowkey)); count++; if (count == limit) break; } } finally { if (scanner != null) { scanner.close(); } if (table != null) { table.close(); } if (conn != null) { conn.close(); } } }
Example 18
Source File: HBaseUtils.java From bigdata-tutorial with Apache License 2.0 | 2 votes |
/** * return all row from a table * * @param table * @throws Exception */ public static ResultScanner scanAll(HTableInterface table) throws Exception { Scan s = new Scan(); ResultScanner rs = table.getScanner(s); return rs; }
Example 19
Source File: HBaseUtils.java From bigdata-tutorial with Apache License 2.0 | 2 votes |
/** * return all match row from a table by scan filter * * @param table * @throws Exception */ public static ResultScanner scan(HTableInterface table, Scan s) throws Exception { ResultScanner rs = table.getScanner(s); return rs; }
Example 20
Source File: HBaseDMLHandler.java From bigdata-tutorial with Apache License 2.0 | 2 votes |
/** * return all row from a table * * @param table * @throws Exception */ public static ResultScanner scanAll(HTableInterface table) throws Exception { Scan s = new Scan(); ResultScanner rs = table.getScanner(s); return rs; }