下面列出了org.apache.hadoop.hbase.client.Result#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testReadRowFromColFam2AsProcessOwner() throws Exception {
final Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "" + port);
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
Connection conn = ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf("temp"));
// Read a row
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
byte[] valResult = result.getValue(Bytes.toBytes("colfam2"), Bytes.toBytes("col1"));
Assert.assertTrue(Arrays.equals(valResult, Bytes.toBytes("val2")));
conn.close();
}
/**
* Returns the raw job history file as a byte array stored for the given
* cluster and job ID.
* @param jobId the cluster and job ID to look up
* @return the stored job history file contents or {@code null} if no
* corresponding record was found
* @throws IOException
*/
public byte[] getRawJobHistoryBytes(QualifiedJobId jobId) throws IOException {
byte[] historyData = null;
byte[] rowKey = idConv.toBytes(jobId);
Get get = new Get(rowKey);
get.addColumn(Constants.RAW_FAM_BYTES, Constants.JOBHISTORY_COL_BYTES);
Table rawTable = null;
try {
rawTable = hbaseConnection
.getTable(TableName.valueOf(Constants.HISTORY_RAW_TABLE));
Result result = rawTable.get(get);
if (result != null && !result.isEmpty()) {
historyData = result.getValue(Constants.RAW_FAM_BYTES,
Constants.JOBHISTORY_COL_BYTES);
}
} finally {
if (rawTable != null) {
rawTable.close();
}
}
return historyData;
}
private ManagedUser hbaseRowToUser(Result result) throws JsonParseException, JsonMappingException, IOException {
if (null == result || result.isEmpty())
return null;
String username = Bytes.toString(result.getRow());
byte[] valueBytes = result.getValue(Bytes.toBytes(AclConstant.USER_AUTHORITY_FAMILY),
Bytes.toBytes(AclConstant.USER_AUTHORITY_COLUMN));
UserGrantedAuthority[] deserialized = ugaSerializer.deserialize(valueBytes);
String password = "";
List<UserGrantedAuthority> authorities = Collections.emptyList();
// password is stored at [0] of authorities for backward compatibility
if (deserialized != null) {
if (deserialized.length > 0 && deserialized[0].getAuthority().startsWith(AclConstant.PWD_PREFIX)) {
password = deserialized[0].getAuthority().substring(AclConstant.PWD_PREFIX.length());
authorities = Arrays.asList(deserialized).subList(1, deserialized.length);
} else {
authorities = Arrays.asList(deserialized);
}
}
return new ManagedUser(username, password, false, authorities);
}
/**
* Delete prune upper bounds for the regions that are not in the given exclude set, and the
* prune upper bound is less than the given value.
* After the invalid list is pruned up to deletionPruneUpperBound, we do not need entries for regions that have
* prune upper bound less than deletionPruneUpperBound. We however limit the deletion to only regions that are
* no longer in existence (due to deletion, etc.), to avoid update/delete race conditions.
*
* @param deletionPruneUpperBound prune upper bound below which regions will be deleted
* @param excludeRegions set of regions that should not be deleted
* @throws IOException when not able to delete data in HBase
*/
public void deletePruneUpperBounds(long deletionPruneUpperBound, SortedSet<byte[]> excludeRegions)
throws IOException {
try (Table stateTable = stateTableSupplier.get()) {
byte[] startRow = makeRegionKey(EMPTY_BYTE_ARRAY);
Scan scan = new Scan(startRow, REGION_KEY_PREFIX_STOP);
scan.addColumn(FAMILY, PRUNE_UPPER_BOUND_COL);
try (ResultScanner scanner = stateTable.getScanner(scan)) {
Result next;
while ((next = scanner.next()) != null) {
byte[] region = getRegionFromKey(next.getRow());
if (!excludeRegions.contains(region)) {
byte[] timeBytes = next.getValue(FAMILY, PRUNE_UPPER_BOUND_COL);
if (timeBytes != null) {
long pruneUpperBoundRegion = Bytes.toLong(timeBytes);
if (pruneUpperBoundRegion < deletionPruneUpperBound) {
stateTable.delete(new Delete(next.getRow()));
}
}
}
}
}
}
}
public TransactionStatus getStatusForTransaction(final long transactionId) throws IOException {
HTableInterface table = getTable();
try {
Result result = table.get(new Get(getRow(transactionId)));
if (result == null || result.isEmpty()) {
return null;
}
byte[] statusCell = result.getValue(INFO_FAMILY, STATUS_QUALIFIER);
if (statusCell == null) {
throw new RuntimeException("No status cell for row " + transactionId);
}
String statusString = Bytes.toString(statusCell);
return TransactionStatus.valueOf(statusString);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
putTable(table);
}
}
private Event waitForEvent(final long eventNumber) throws Exception {
logger.debug("Waiting for event " + eventNumber);
Get getter = new Get(Bytes.toBytes( eventNumber ));
Event event = null;
while ( event == null ) {
Result r = channelTable.get(getter);
if ( r.isEmpty() ) {
Thread.sleep( WATCH_WINDOW_MILLIS);
} else {
final long timeStamp = r.rawCells()[0].getTimestamp();
final byte[] value = r.getValue(HBaseEventSink.colFamily, HBaseEventSink.qualifier);
event = new FileEvent(eventNumber,timeStamp,0L, Unpooled.wrappedBuffer(value));
}
}
logger.debug("Got Event " + eventNumber);
return event;
}
protected void verifyHBaseCell(String tableName, String rowKey,
String colFamily, String colName, String val) throws IOException {
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(colName));
HTable table = new HTable(new Configuration(
hbaseTestUtil.getConfiguration()), Bytes.toBytes(tableName));
try {
Result r = table.get(get);
byte [] actualVal = r.getValue(Bytes.toBytes(colFamily),
Bytes.toBytes(colName));
if (null == val) {
assertNull("Got a result when expected null", actualVal);
} else {
assertNotNull("No result, but we expected one", actualVal);
assertEquals(val, Bytes.toString(actualVal));
}
} finally {
table.close();
}
}
/**
* Delete prune upper bounds for the regions that are not in the given exclude set, and the
* prune upper bound is less than the given value.
* After the invalid list is pruned up to deletionPruneUpperBound, we do not need entries for regions that have
* prune upper bound less than deletionPruneUpperBound. We however limit the deletion to only regions that are
* no longer in existence (due to deletion, etc.), to avoid update/delete race conditions.
*
* @param deletionPruneUpperBound prune upper bound below which regions will be deleted
* @param excludeRegions set of regions that should not be deleted
* @throws IOException when not able to delete data in HBase
*/
public void deletePruneUpperBounds(long deletionPruneUpperBound, SortedSet<byte[]> excludeRegions)
throws IOException {
try (Table stateTable = stateTableSupplier.get()) {
byte[] startRow = makeRegionKey(EMPTY_BYTE_ARRAY);
Scan scan = new Scan(startRow, REGION_KEY_PREFIX_STOP);
scan.addColumn(FAMILY, PRUNE_UPPER_BOUND_COL);
try (ResultScanner scanner = stateTable.getScanner(scan)) {
Result next;
while ((next = scanner.next()) != null) {
byte[] region = getRegionFromKey(next.getRow());
if (!excludeRegions.contains(region)) {
byte[] timeBytes = next.getValue(FAMILY, PRUNE_UPPER_BOUND_COL);
if (timeBytes != null) {
long pruneUpperBoundRegion = Bytes.toLong(timeBytes);
if (pruneUpperBoundRegion < deletionPruneUpperBound) {
stateTable.delete(new Delete(next.getRow()));
}
}
}
}
}
}
}
/**
* Parses HBase {@link Result} into {@link Row}.
*/
public Row parseToRow(Result result, Object rowKey) {
for (int i = 0; i < fieldLength; i++) {
if (rowKeyIndex == i) {
resultRow.setField(rowKeyIndex, rowKey);
} else {
int f = (rowKeyIndex != -1 && i > rowKeyIndex) ? i - 1 : i;
// get family key
byte[] familyKey = families[f];
Row familyRow = familyRows[f];
for (int q = 0; q < this.qualifiers[f].length; q++) {
// get quantifier key
byte[] qualifier = qualifiers[f][q];
// get quantifier type idx
int typeIdx = qualifierTypes[f][q];
// read value
byte[] value = result.getValue(familyKey, qualifier);
if (value != null) {
familyRow.setField(q, HBaseTypeUtils.deserializeToObject(value, typeIdx, charset));
} else {
familyRow.setField(q, null);
}
}
resultRow.setField(i, familyRow);
}
}
return resultRow;
}
private long getCurrentBalance(int id) throws IOException {
Result r = txTable.get(new Get(Bytes.toBytes(id)));
byte[] balanceBytes = r.getValue(FAMILY, COL);
if (balanceBytes == null) {
return 0;
}
return Bytes.toLong(balanceBytes);
}
private static void parseRegionServerResult(final String regionServer, final Result result,
final RegionServerQuotasVisitor visitor) throws IOException {
byte[] data = result.getValue(QUOTA_FAMILY_INFO, QUOTA_QUALIFIER_SETTINGS);
if (data != null) {
Quotas quotas = quotasFromData(data);
visitor.visitRegionServerQuotas(regionServer, quotas);
}
}
private Optional<String> getUserSettings(Result result, String type) throws IOException {
Optional<String> userSettings = Optional.empty();
if (result != null) {
byte[] value = result.getValue(cf, Bytes.toBytes(type));
if (value != null) {
userSettings = Optional.of(new String(value, StandardCharsets.UTF_8));
}
}
return userSettings;
}
@Override
public Collection<byte[]> extract(Result result) {
byte[] bytes = result.getValue(columnFamily, columnQualifier);
if (bytes == null) {
return Collections.emptyList();
} else {
return Lists.newArrayList(bytes);
}
}
int setRegionState(ClusterConnection connection, String region,
RegionState.State newState)
throws IOException {
if (newState == null) {
throw new IllegalArgumentException("State can't be null.");
}
RegionState.State currentState = null;
Table table = connection.getTable(TableName.valueOf("hbase:meta"));
RowFilter filter = new RowFilter(CompareOperator.EQUAL, new SubstringComparator(region));
Scan scan = new Scan();
scan.setFilter(filter);
Result result = table.getScanner(scan).next();
if (result != null) {
byte[] currentStateValue = result.getValue(HConstants.CATALOG_FAMILY,
HConstants.STATE_QUALIFIER);
if (currentStateValue == null) {
System.out.println("WARN: Region state info on meta was NULL");
} else {
currentState = RegionState.State.valueOf(
org.apache.hadoop.hbase.util.Bytes.toString(currentStateValue));
}
Put put = new Put(result.getRow());
put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
org.apache.hadoop.hbase.util.Bytes.toBytes(newState.name()));
table.put(put);
System.out.println("Changed region " + region + " STATE from "
+ currentState + " to " + newState);
return EXIT_SUCCESS;
} else {
System.out.println("ERROR: Could not find region " + region + " in meta.");
}
return EXIT_FAILURE;
}
@VisibleForTesting
int getRegionCountForTime(Table stateTable, long time) throws IOException {
Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
get.addColumn(FAMILY, REGION_TIME_COL);
Result result = stateTable.get(get);
byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
return value == null ? -1 : Bytes.toInt(value);
}
protected final String parseColumn(String familyName, String columnName, Result result) {
if (result.isEmpty()) {
return null;
}
try {
byte[] value = result.getValue(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
return Bytes.toString(value);
} catch (Exception cause) {
throw new RuntimeException(cause);
}
}
@Override
public TraceStatisticsDto mapRow(Result result, int rowNum) throws Exception {
TraceStatisticsDto rpcTrace = new TraceStatisticsDto();
byte[] success = result.getValue(Bytes.toBytes(Constants.TRACE_TYPE), Bytes.toBytes(Constants.TRACE_SUCCESS));
if (null != success) {
rpcTrace.setSuccess(new String(success));
}
byte[] fail = result.getValue(Bytes.toBytes(Constants.TRACE_TYPE), Bytes.toBytes(Constants.TRACE_FAIL));
if (null != fail) {
rpcTrace.setFail(new String(fail));
}
byte[] max = result.getValue(Bytes.toBytes(Constants.TRACE_TYPE), Bytes.toBytes(Constants.TRACE_MAX));
if (null != max) {
rpcTrace.setMax(String.valueOf(Double.valueOf(new String(max)) / 1000));
}
byte[] min = result.getValue(Bytes.toBytes(Constants.TRACE_TYPE), Bytes.toBytes(Constants.TRACE_MIN));
if (null != min) {
rpcTrace.setMin(String.valueOf(Double.valueOf(new String(min)) / 1000));
}
byte[] average = result.getValue(Bytes.toBytes(Constants.TRACE_TYPE), Bytes.toBytes(Constants.TRACE_AVERAGE));
if (null != average) {
rpcTrace.setAverage(String.valueOf(Double.valueOf(new String(average)) / 1000));
}
byte[] row = result.getRow();
if (null != row) {
rpcTrace.setRowkey(new String(row));
}
return rpcTrace;
}
private SequenceEvent parseEventResult(Result r) throws IOException {
final String key = new String(r.getRow());
final long timestamp = r.getColumnLatestCell(EVENT_CF, EVENT_QUALIFIER).getTimestamp();
final byte[] value = r.getValue(EVENT_CF, EVENT_QUALIFIER);
final Event event = deserializeEvent(ByteString.of(value));
return SequenceEvent.parseKey(key, event, timestamp);
}
@VisibleForTesting
int getRegionCountForTime(Table stateTable, long time) throws IOException {
Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
get.addColumn(FAMILY, REGION_TIME_COL);
Result result = stateTable.get(get);
byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
return value == null ? -1 : Bytes.toInt(value);
}
private long getCurrentBalance(int id) throws IOException {
Result r = txTable.get(new Get(Bytes.toBytes(id)));
byte[] balanceBytes = r.getValue(FAMILY, COL);
if (balanceBytes == null) {
return 0;
}
return Bytes.toLong(balanceBytes);
}