下面列出了org.apache.hadoop.hbase.client.Result#cellScanner ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Just for debug
*/
@SuppressWarnings("unused")
private void dump(String table, Scan scan) throws IOException {
System.out.println(String.format(">>>> scan table %s with %s",
table, scan));
RowIterator iterator = this.scan(table, scan);
while (iterator.hasNext()) {
Result row = iterator.next();
System.out.println(StringEncoding.format(row.getRow()));
CellScanner cellScanner = row.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
byte[] key = CellUtil.cloneQualifier(cell);
byte[] val = CellUtil.cloneValue(cell);
System.out.println(String.format(" %s=%s",
StringEncoding.format(key),
StringEncoding.format(val)));
}
}
}
public static int getRowCount(Table table, boolean isRaw) throws IOException {
Scan s = new Scan();
s.setRaw(isRaw);;
s.setMaxVersions();
int rows = 0;
try (ResultScanner scanner = table.getScanner(s)) {
Result result = null;
while ((result = scanner.next()) != null) {
rows++;
CellScanner cellScanner = result.cellScanner();
Cell current = null;
while (cellScanner.advance()) {
current = cellScanner.current();
}
}
}
return rows;
}
public static CellCount getCellCount(Table table, boolean isRaw) throws IOException {
Scan s = new Scan();
s.setRaw(isRaw);;
s.setMaxVersions();
CellCount cellCount = new CellCount();
try (ResultScanner scanner = table.getScanner(s)) {
Result result = null;
while ((result = scanner.next()) != null) {
CellScanner cellScanner = result.cellScanner();
Cell current = null;
while (cellScanner.advance()) {
current = cellScanner.current();
cellCount.addCell(Bytes.toString(CellUtil.cloneRow(current)));
}
}
}
return cellCount;
}
@Override
public boolean postScannerNext(ObserverContext<RegionCoprocessorEnvironment> e,
InternalScanner s, List<Result> results, int limit, boolean hasMore) throws IOException {
if (checkTagPresence) {
if (results.size() > 0) {
// Check tag presence in the 1st cell in 1st Result
Result result = results.get(0);
CellScanner cellScanner = result.cellScanner();
if (cellScanner.advance()) {
Cell cell = cellScanner.current();
tags = PrivateCellUtil.getTags(cell);
}
}
}
return hasMore;
}
protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result)
throws IOException {
byte[] row = key.get();
CellScanner scanner = result.cellScanner();
while (scanner.advance()) {
Cell cell = scanner.current();
//assert that all Cells in the Result have the same key
Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
}
for (byte[] family : FAMILIES) {
byte[] actual = result.getValue(family, family);
Assert.assertArrayEquals(
"Row in snapshot does not match, expected:" + Bytes.toString(row) + " ,actual:" + Bytes
.toString(actual), row, actual);
}
}
/**
* Returns a list of {@code Delete} to remove all entries returned by the passed scanner.
* @param connection connection to re-use
* @param scan the scanner to use to generate the list of deletes
*/
static List<Delete> createDeletesForExistingSnapshotsFromScan(Connection connection, Scan scan)
throws IOException {
List<Delete> deletes = new ArrayList<>();
try (Table quotaTable = connection.getTable(QUOTA_TABLE_NAME);
ResultScanner rs = quotaTable.getScanner(scan)) {
for (Result r : rs) {
CellScanner cs = r.cellScanner();
while (cs.advance()) {
Cell c = cs.current();
byte[] family = Bytes.copy(c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength());
byte[] qual =
Bytes.copy(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength());
Delete d = new Delete(r.getRow());
d.addColumns(family, qual);
deletes.add(d);
}
}
return deletes;
}
}
/**
* Fetches any persisted HBase snapshot sizes stored in the quota table. The sizes here are
* computed relative to the table which the snapshot was created from. A snapshot's size will
* not include the size of files which the table still refers. These sizes, in bytes, are what
* is used internally to compute quota violation for tables and namespaces.
*
* @return A map of snapshot name to size in bytes per space quota computations
*/
public static Map<String,Long> getObservedSnapshotSizes(Connection conn) throws IOException {
try (Table quotaTable = conn.getTable(QUOTA_TABLE_NAME);
ResultScanner rs = quotaTable.getScanner(createScanForSpaceSnapshotSizes())) {
final Map<String,Long> snapshotSizes = new HashMap<>();
for (Result r : rs) {
CellScanner cs = r.cellScanner();
while (cs.advance()) {
Cell c = cs.current();
final String snapshot = extractSnapshotNameFromSizeCell(c);
final long size = parseSnapshotSize(c);
snapshotSizes.put(snapshot, size);
}
}
return snapshotSizes;
}
}
/**
* Returns a multimap for all existing table snapshot entries.
* @param conn connection to re-use
*/
public static Multimap<TableName, String> getTableSnapshots(Connection conn) throws IOException {
try (Table quotaTable = conn.getTable(QUOTA_TABLE_NAME);
ResultScanner rs = quotaTable.getScanner(createScanForSpaceSnapshotSizes())) {
Multimap<TableName, String> snapshots = HashMultimap.create();
for (Result r : rs) {
CellScanner cs = r.cellScanner();
while (cs.advance()) {
Cell c = cs.current();
final String snapshot = extractSnapshotNameFromSizeCell(c);
snapshots.put(getTableFromRowKey(r.getRow()), snapshot);
}
}
return snapshots;
}
}
private void assertNoIndexDeletes(Connection conn, long minTimestamp, String fullIndexName) throws IOException, SQLException {
if (!this.mutable) {
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable index = pconn.getTable(new PTableKey(null, fullIndexName));
byte[] physicalIndexTable = index.getPhysicalName().getBytes();
try (Table hIndex = pconn.getQueryServices().getTable(physicalIndexTable)) {
Scan scan = new Scan();
scan.setRaw(true);
if (this.transactional) {
minTimestamp = TransactionUtil.convertToNanoseconds(minTimestamp);
}
scan.setTimeRange(minTimestamp, HConstants.LATEST_TIMESTAMP);
ResultScanner scanner = hIndex.getScanner(scan);
Result result;
while ((result = scanner.next()) != null) {
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell current = cellScanner.current();
assertTrue(CellUtil.isPut(current));
}
}
};
}
}
public static void dumpTable(Table table) throws IOException {
System.out.println("************ dumping " + table + " **************");
Scan s = new Scan();
s.setRaw(true);;
s.setMaxVersions();
try (ResultScanner scanner = table.getScanner(s)) {
Result result = null;
while ((result = scanner.next()) != null) {
CellScanner cellScanner = result.cellScanner();
Cell current = null;
while (cellScanner.advance()) {
current = cellScanner.current();
System.out.println(current);
}
}
}
System.out.println("-----------------------------------------------");
}
protected void parseRowColumns(Result row, BackendEntry entry, Query query)
throws IOException {
CellScanner cellScanner = row.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
entry.columns(BackendColumn.of(CellUtil.cloneQualifier(cell),
CellUtil.cloneValue(cell)));
}
}
private void scanAndCountTable(Table table, int expected) throws IOException {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
int count = 0;
for (Result result : scanner) {
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
count++;
}
}
assertEquals(expected, count);
}
/**
* Fetches any serialized snapshot sizes from the quota table for the {@code tn} provided. Any
* malformed records are skipped with a warning printed out.
*/
long getSnapshotSizesForTable(TableName tn) throws IOException {
try (Table quotaTable = conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
Scan s = QuotaTableUtil.createScanForSpaceSnapshotSizes(tn);
ResultScanner rs = quotaTable.getScanner(s);
try {
long size = 0L;
// Should just be a single row (for our table)
for (Result result : rs) {
// May have multiple columns, one for each snapshot
CellScanner cs = result.cellScanner();
while (cs.advance()) {
Cell current = cs.current();
try {
long snapshotSize = QuotaTableUtil.parseSnapshotSize(current);
if (LOG.isTraceEnabled()) {
LOG.trace("Saw snapshot size of " + snapshotSize + " for " + current);
}
size += snapshotSize;
} catch (InvalidProtocolBufferException e) {
LOG.warn("Failed to parse snapshot size from cell: " + current);
}
}
}
return size;
} finally {
if (null != rs) {
rs.close();
}
}
}
}
private void verifyTableSnapshotSize(
Table quotaTable, TableName tn, String snapshotName, long expectedSize) throws IOException {
Result r = quotaTable.get(QuotaTableUtil.makeGetForSnapshotSize(tn, snapshotName));
CellScanner cs = r.cellScanner();
assertTrue(cs.advance());
Cell c = cs.current();
assertEquals(expectedSize, QuotaProtos.SpaceQuotaSnapshot.parseFrom(
UnsafeByteOperations.unsafeWrap(
c.getValueArray(), c.getValueOffset(), c.getValueLength())).getQuotaUsage());
assertFalse(cs.advance());
}
private void verifyRecordPresentInQuotaTable(ThrottleType type, long limit, TimeUnit tu,
QuotaScope scope) throws Exception {
// Verify the RPC Quotas in the table
try (Table quotaTable = TEST_UTIL.getConnection().getTable(QuotaTableUtil.QUOTA_TABLE_NAME);
ResultScanner scanner = quotaTable.getScanner(new Scan())) {
Result r = Iterables.getOnlyElement(scanner);
CellScanner cells = r.cellScanner();
assertTrue("Expected to find a cell", cells.advance());
assertRPCQuota(type, limit, tu, scope, cells.current());
}
}
private long extractSnapshotSize(
Table quotaTable, TableName tn, String snapshot) throws IOException {
Get g = QuotaTableUtil.makeGetForSnapshotSize(tn, snapshot);
Result r = quotaTable.get(g);
assertNotNull(r);
CellScanner cs = r.cellScanner();
assertTrue(cs.advance());
Cell c = cs.current();
assertNotNull(c);
return QuotaTableUtil.extractSnapshotSize(
c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
void updateValueSize(final Result r) throws IOException {
if (r == null || !isRandomValueSize()) return;
int size = 0;
for (CellScanner scanner = r.cellScanner(); scanner.advance();) {
size += scanner.current().getValueLength();
}
updateValueSize(size);
}
/**
* Returns a set of the names of all namespaces containing snapshot entries.
* @param conn connection to re-use
*/
public static Set<String> getNamespaceSnapshots(Connection conn) throws IOException {
try (Table quotaTable = conn.getTable(QUOTA_TABLE_NAME);
ResultScanner rs = quotaTable.getScanner(createScanForNamespaceSnapshotSizes())) {
Set<String> snapshots = new HashSet<>();
for (Result r : rs) {
CellScanner cs = r.cellScanner();
while (cs.advance()) {
cs.current();
snapshots.add(getNamespaceFromRowKey(r.getRow()));
}
}
return snapshots;
}
}
private static void assertShadowCells(Connection conn, String fullTableName, String fullIndexName, boolean exists)
throws Exception {
PTable ptable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, fullTableName));
int nTableKVColumns = ptable.getColumns().size() - ptable.getPKColumns().size();
Table hTable = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(fullTableName));
ResultScanner tableScanner = hTable.getScanner(new Scan());
Result tableResult;
PTable pindex = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, fullIndexName));
int nIndexKVColumns = pindex.getColumns().size() - pindex.getPKColumns().size();
Table hIndex = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(fullIndexName));
ResultScanner indexScanner = hIndex.getScanner(new Scan());
Result indexResult;
while ((indexResult = indexScanner.next()) != null) {
int nColumns = 0;
CellScanner scanner = indexResult.cellScanner();
while (scanner.advance()) {
nColumns++;
}
assertEquals(exists, nColumns > nIndexKVColumns * 2);
assertNotNull(tableResult = tableScanner.next());
nColumns = 0;
scanner = tableResult.cellScanner();
while (scanner.advance()) {
nColumns++;
}
assertEquals(exists, nColumns > nTableKVColumns * 2);
}
assertNull(tableScanner.next());
}
public static GuidePostsInfo readStatistics(Table statsHTable, GuidePostsKey key, long clientTimeStamp)
throws IOException {
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
ptr.set(key.getColumnFamily());
byte[] tableNameBytes = key.getPhysicalName();
byte[] startKey = getStartKey(tableNameBytes, ptr);
byte[] endKey = getEndKey(tableNameBytes, ptr);
Scan s = MetaDataUtil.newTableRowsScan(startKey, endKey, MetaDataProtocol.MIN_TABLE_TIMESTAMP, clientTimeStamp);
s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH_BYTES);
s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.GUIDE_POSTS_ROW_COUNT_BYTES);
s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES);
GuidePostsInfoBuilder guidePostsInfoBuilder = new GuidePostsInfoBuilder();
Cell current = null;
GuidePostsInfo emptyGuidePost = null;
try (ResultScanner scanner = statsHTable.getScanner(s)) {
Result result = null;
while ((result = scanner.next()) != null) {
CellScanner cellScanner = result.cellScanner();
long rowCount = 0;
long byteCount = 0;
while (cellScanner.advance()) {
current = cellScanner.current();
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 if (Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
current.getQualifierLength(), PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH_BYTES, 0,
PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH_BYTES.length)) {
byteCount = PLong.INSTANCE.getCodec().decodeLong(current.getValueArray(),
current.getValueOffset(), SortOrder.getDefault());
}
}
if (current != null) {
int tableNameLength = tableNameBytes.length + 1;
int cfOffset = current.getRowOffset() + tableNameLength;
int cfLength = getVarCharLength(current.getRowArray(), cfOffset,
current.getRowLength() - tableNameLength);
ptr.set(current.getRowArray(), cfOffset, cfLength);
byte[] cfName = ByteUtil.copyKeyBytesIfNecessary(ptr);
byte[] newGPStartKey = getGuidePostsInfoFromRowKey(tableNameBytes, cfName, result.getRow());
boolean isEmptyGuidePost = GuidePostsInfo.isEmptyGpsKey(newGPStartKey);
// Use the timestamp of the cell as the time at which guidepost was
// created/updated
long guidePostUpdateTime = current.getTimestamp();
if (isEmptyGuidePost) {
emptyGuidePost =
GuidePostsInfo.createEmptyGuidePost(byteCount, guidePostUpdateTime);
} else {
guidePostsInfoBuilder.trackGuidePost(
new ImmutableBytesWritable(newGPStartKey), byteCount, rowCount,
guidePostUpdateTime);
}
}
}
}
// We write a row with an empty KeyValue in the case that stats were generated but without enough data
// for any guideposts. If we have no rows, it means stats were never generated.
return current == null ? GuidePostsInfo.NO_GUIDEPOST : guidePostsInfoBuilder.isEmpty() ? emptyGuidePost : guidePostsInfoBuilder.build();
}