下面列出了org.apache.hadoop.hbase.client.Result#isEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void verifyReplicationStuck() throws Exception {
try (Table normalTable = utility1.getConnection().getTable(TABLE)) {
Put put = new Put(ROW);
put.addColumn(NORMAL_FAMILY, QUALIFIER, VALUE);
normalTable.put(put);
}
try (Table normalTable = utility2.getConnection().getTable(TABLE)) {
for (int i = 0; i < NB_RETRIES; i++) {
Result result = normalTable.get(new Get(ROW).addColumn(NORMAL_FAMILY, QUALIFIER));
if (result != null && !result.isEmpty()) {
fail("Edit should have been stuck behind dropped tables, but value is " + Bytes
.toString(result.getValue(NORMAL_FAMILY, QUALIFIER)));
} else {
LOG.info("Row not replicated, let's wait a bit more...");
Thread.sleep(SLEEP_TIME);
}
}
}
}
private Result internalGetFromHTable(Table table, String path, boolean fetchContent, boolean fetchTimestamp)
throws IOException {
byte[] rowkey = Bytes.toBytes(path);
Get get = new Get(rowkey);
if (!fetchContent && !fetchTimestamp) {
get.setCheckExistenceOnly(true);
} else {
if (fetchContent)
get.addColumn(B_FAMILY, B_COLUMN);
if (fetchTimestamp)
get.addColumn(B_FAMILY, B_COLUMN_TS);
}
Result result = table.get(get);
boolean exists = result != null && (!result.isEmpty() || (result.getExists() != null && result.getExists()));
return exists ? result : null;
}
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);
}
private void familyQualifierBasedDeletion(HBaseTransaction tx, Put deleteP, Get deleteG) throws IOException {
Result result = this.get(tx, deleteG);
if (!result.isEmpty()) {
for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entryF : result.getMap()
.entrySet()) {
byte[] family = entryF.getKey();
for (Entry<byte[], NavigableMap<Long, byte[]>> entryQ : entryF.getValue().entrySet()) {
byte[] qualifier = entryQ.getKey();
addWriteSetElement(tx, new HBaseCellId(this, deleteP.getRow(), family, qualifier,
tx.getWriteTimestamp()));
}
deleteP.addColumn(family, CellUtils.FAMILY_DELETE_QUALIFIER, tx.getWriteTimestamp(),
CellUtils.DELETE_TOMBSTONE);
addWriteSetElement(tx, new HBaseCellId(this, deleteP.getRow(), family, CellUtils.FAMILY_DELETE_QUALIFIER,
tx.getWriteTimestamp()));
}
}
}
/**
* Add backup set (list of tables)
* @param name set name
* @param newTables list of tables, comma-separated
* @throws IOException if a table operation fails
*/
public void addToBackupSet(String name, String[] newTables) throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace("Backup set add: " + name + " tables [" + StringUtils.join(newTables, " ") + "]");
}
String[] union = null;
try (Table table = connection.getTable(tableName)) {
Get get = createGetForBackupSet(name);
Result res = table.get(get);
if (res.isEmpty()) {
union = newTables;
} else {
res.advance();
String[] tables = cellValueToBackupSet(res.current());
union = merge(tables, newTables);
}
Put put = createPutForBackupSet(name, union);
table.put(put);
}
}
protected final Object parseFamily(String familyName, Result result) {
if (result.isEmpty()) {
return null;
}
try {
Field familyField = clazz.getDeclaredField(familyName);
byte[] familyNameBytes = Bytes.toBytes(familyField.getName());
if (HBaseUtil.isBaseFamily(familyField)) {
return parseNotArrayClass(familyField, familyNameBytes, result);
} else if (HBaseUtil.isArrayFamily(familyField)) {
byte[] countBytes = result.getValue(familyNameBytes, COUNT_COLUMN_BYTE_ARRAY);
if (countBytes != null) {
int count = Bytes.toInt(countBytes);
Map<byte[], byte[]> familyMap = result.getFamilyMap(familyNameBytes);
if (familyMap != null) {
return parseArrayClass(familyField, count, familyMap);
}
}
} else {
LogUtil.warn(LOGGER, "������ݿ��ѯ���ʱ��������[" + familyField.getName() + "]");
}
return null;
} catch (Exception cause) {
throw new RuntimeException(cause);
}
}
private void verifyReplicationStuck() throws Exception {
try (Table normalTable = utility1.getConnection().getTable(NORMAL_TABLE)) {
Put put = new Put(ROW);
put.addColumn(FAMILY, QUALIFIER, VALUE);
normalTable.put(put);
}
try (Table normalTable = utility2.getConnection().getTable(NORMAL_TABLE)) {
for (int i = 0; i < NB_RETRIES; i++) {
Result result = normalTable.get(new Get(ROW).addColumn(FAMILY, QUALIFIER));
if (result != null && !result.isEmpty()) {
fail("Edit should have been stuck behind dropped tables, but value is " + Bytes
.toString(result.getValue(FAMILY, QUALIFIER)));
} else {
LOG.info("Row not replicated, let's wait a bit more...");
Thread.sleep(SLEEP_TIME);
}
}
}
}
@Override
public Application mapRow(Result result, int rowNum) throws Exception {
if (result.isEmpty()) {
return null;
}
byte[] value = result.value();
if (value.length != HbaseTableConstatns.APPLICATION_NAME_MAX_LEN + 2) {
logger.warn("Invalid value. {}", Arrays.toString(value));
}
String applicationName = Bytes.toString(value, 0, HbaseTableConstatns.APPLICATION_NAME_MAX_LEN - 1).trim();
short serviceTypeCode = Bytes.toShort(value, HbaseTableConstatns.APPLICATION_NAME_MAX_LEN);
return this.applicationFactory.createApplication(applicationName, serviceTypeCode);
}
/**
* Reads backup status object (instance of backup info) from backup system table table
* @param backupId backup id
* @return Current status of backup session or null
*/
public BackupInfo readBackupInfo(String backupId) throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace("read backup status from backup system table for: " + backupId);
}
try (Table table = connection.getTable(tableName)) {
Get get = createGetForBackupInfo(backupId);
Result res = table.get(get);
if (res.isEmpty()) {
return null;
}
return resultToBackupInfo(res);
}
}
public RowIterator(Result... results) {
this.resultScanner = null;
List<Result> rs = new ArrayList<>(results.length);
for (Result result : results) {
// Get by Ids may return empty result
if (!result.isEmpty()) {
rs.add(result);
}
}
this.results = rs.iterator();
}
@Override
public List<AcceptApplication> mapRow(Result result, int rowNum) throws Exception {
if (result.isEmpty()) {
return Collections.emptyList();
}
// readRowKey(result.getRow());
final List<AcceptApplication> acceptApplicationList = new ArrayList<>(result.size());
for (Cell cell : result.rawCells()) {
AcceptApplication acceptedApplication = createAcceptedApplication(cell);
acceptApplicationList.add(acceptedApplication);
}
return acceptApplicationList;
}
/**
* Extracts the size component from a serialized {@link SpaceQuotaSnapshot} protobuf.
*
* @param r A Result containing one cell with a SpaceQuotaSnapshot protobuf
* @return The size in bytes of the snapshot.
*/
long getSnapshotSizeFromResult(Result r) throws InvalidProtocolBufferException {
// Per javadoc, Result should only be null if an exception was thrown. So, if we're here,
// we should be non-null. If we can't advance to the first cell, same as "no cell".
if (!r.isEmpty() && r.advance()) {
return QuotaTableUtil.parseSnapshotSize(r.current());
}
return 0L;
}
@Override
public Notification getNotification(String notificationId) {
try {
String tableName = notificationMapper.getTableName();
LOG.debug("getting notification with notificationId {} from table {}", notificationId, tableName);
Get get = new Get(notificationId.getBytes(StandardCharsets.UTF_8));
Result result = tables.get(tableName).get().get(get);
return result.isEmpty() ? null : notificationMapper.entity(result);
} catch (IOException ex) {
throw new NotificationStoreException("Error getting notification id: " + notificationId, ex);
}
}
/**
* Validates the current state of the data stored at the end of the test. Each update by a client consists of two
* parts: a withdrawal of a random amount from a randomly select other account, and a corresponding to deposit to
* the client's own account. So, if all the updates were performed consistently (no partial updates or partial
* rollbacks), then the total sum of all balances at the end should be 0.
*/
public boolean verify() {
boolean success = false;
try {
TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TableName.valueOf(TABLE)));
TransactionContext context = new TransactionContext(txClient, table);
LOG.info("VERIFYING BALANCES");
context.start();
long totalBalance = 0;
try (ResultScanner scanner = table.getScanner(new Scan())) {
for (Result r : scanner) {
if (!r.isEmpty()) {
int rowId = Bytes.toInt(r.getRow());
long balance = Bytes.toLong(r.getValue(FAMILY, COL));
totalBalance += balance;
LOG.info("Client #{}: balance = ${}", rowId, balance);
}
}
}
if (totalBalance == 0) {
LOG.info("PASSED!");
success = true;
} else {
LOG.info("FAILED! Total balance should be 0 but was {}", totalBalance);
}
context.finish();
} catch (Exception e) {
LOG.error("Failed verification check", e);
}
return success;
}
/**
* Validates the current state of the data stored at the end of the test. Each update by a client consists of two
* parts: a withdrawal of a random amount from a randomly select other account, and a corresponding to deposit to
* the client's own account. So, if all the updates were performed consistently (no partial updates or partial
* rollbacks), then the total sum of all balances at the end should be 0.
*/
public boolean verify() {
boolean success = false;
try {
TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TableName.valueOf(TABLE)));
TransactionContext context = new TransactionContext(txClient, table);
LOG.info("VERIFYING BALANCES");
context.start();
long totalBalance = 0;
try (ResultScanner scanner = table.getScanner(new Scan())) {
for (Result r : scanner) {
if (!r.isEmpty()) {
int rowId = Bytes.toInt(r.getRow());
long balance = Bytes.toLong(r.getValue(FAMILY, COL));
totalBalance += balance;
LOG.info("Client #{}: balance = ${}", rowId, balance);
}
}
}
if (totalBalance == 0) {
LOG.info("PASSED!");
success = true;
} else {
LOG.info("FAILED! Total balance should be 0 but was {}", totalBalance);
}
context.finish();
} catch (Exception e) {
LOG.error("Failed verification check", e);
}
return success;
}
/**
* Validates the current state of the data stored at the end of the test. Each update by a client consists of two
* parts: a withdrawal of a random amount from a randomly select other account, and a corresponding to deposit to
* the client's own account. So, if all the updates were performed consistently (no partial updates or partial
* rollbacks), then the total sum of all balances at the end should be 0.
*/
public boolean verify() {
boolean success = false;
try {
TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TABLE));
TransactionContext context = new TransactionContext(txClient, table);
LOG.info("VERIFYING BALANCES");
context.start();
long totalBalance = 0;
try (ResultScanner scanner = table.getScanner(new Scan())) {
for (Result r : scanner) {
if (!r.isEmpty()) {
int rowId = Bytes.toInt(r.getRow());
long balance = Bytes.toLong(r.getValue(FAMILY, COL));
totalBalance += balance;
LOG.info("Client #{}: balance = ${}", rowId, balance);
}
}
}
if (totalBalance == 0) {
LOG.info("PASSED!");
success = true;
} else {
LOG.info("FAILED! Total balance should be 0 but was {}", totalBalance);
}
context.finish();
} catch (Exception e) {
LOG.error("Failed verification check", e);
}
return success;
}
/**
* Method to account for the size of retained cells and retained data blocks.
* @param context rpc call context
* @param r result to add size.
* @param lastBlock last block to check whether we need to add the block size in context.
* @return an object that represents the last referenced block from this response.
*/
Object addSize(RpcCallContext context, Result r, Object lastBlock) {
if (context != null && r != null && !r.isEmpty()) {
for (Cell c : r.rawCells()) {
context.incrementResponseCellSize(PrivateCellUtil.estimatedSerializedSizeOf(c));
// Since byte buffers can point all kinds of crazy places it's harder to keep track
// of which blocks are kept alive by what byte buffer.
// So we make a guess.
if (c instanceof ByteBufferExtendedCell) {
ByteBufferExtendedCell bbCell = (ByteBufferExtendedCell) c;
ByteBuffer bb = bbCell.getValueByteBuffer();
if (bb != lastBlock) {
context.incrementResponseBlockSize(bb.capacity());
lastBlock = bb;
}
} else {
// We're using the last block being the same as the current block as
// a proxy for pointing to a new block. This won't be exact.
// If there are multiple gets that bounce back and forth
// Then it's possible that this will over count the size of
// referenced blocks. However it's better to over count and
// use two rpcs than to OOME the regionserver.
byte[] valueArray = c.getValueArray();
if (valueArray != lastBlock) {
context.incrementResponseBlockSize(valueArray.length);
lastBlock = valueArray;
}
}
}
}
return lastBlock;
}
@Override
public List<JoinStatBo> mapRow(Result result, int rowNum) throws Exception {
if (result.isEmpty()) {
return Collections.emptyList();
}
final byte[] distributedRowKey = result.getRow();
final String applicationId = this.hbaseOperationFactory.getApplicationId(distributedRowKey);
final long baseTimestamp = this.hbaseOperationFactory.getBaseTimestamp(distributedRowKey);
List<JoinStatBo> dataPoints = new ArrayList<>();
for (Cell cell : result.rawCells()) {
if (CellUtil.matchingFamily(cell, HbaseColumnFamily.APPLICATION_STAT_STATISTICS.getName())) {
Buffer qualifierBuffer = new OffsetFixedBuffer(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
Buffer valueBuffer = new OffsetFixedBuffer(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
long timestampDelta = this.decoder.decodeQualifier(qualifierBuffer);
ApplicationStatDecodingContext decodingContext = new ApplicationStatDecodingContext();
decodingContext.setApplicationId(applicationId);
decodingContext.setBaseTimestamp(baseTimestamp);
decodingContext.setTimestampDelta(timestampDelta);
List<JoinStatBo> candidates = this.decoder.decodeValue(valueBuffer, decodingContext);
for (JoinStatBo candidate : candidates) {
long timestamp = candidate.getTimestamp();
if (this.filter.filter(timestamp)) {
continue;
}
dataPoints.add(candidate);
}
}
}
// Reverse sort as timestamp is stored in a reversed order.
dataPoints.sort(REVERSE_TIMESTAMP_COMPARATOR);
return dataPoints;
}
void compareOneRow(TableMeta tableMeta, Row row, Result r) throws Exception{
if (row == null) {
throw new Exception("row is null");
}
byte[] antsKey = KeyBytes.create(row.getKeyAddress()).get();
if (r == null || r.isEmpty()) {
throw new Exception("Row can't be found in hbase - key: " + bytesToHex(antsKey));
}
// some preparation
// NavigableMap<byte[], byte[]> sysFamilyMap = r.getFamilyMap(Helper.SYS_COLUMN_FAMILY_BYTES);
NavigableMap<byte[], byte[]> dataFamilyMap = r.getFamilyMap(Helper.DATA_COLUMN_FAMILY_BYTES);
// byte[] colDataType = sysFamilyMap.get(Helper.SYS_COLUMN_DATATYPE_BYTES);
// byte[] versionBytes = sysFamilyMap.get(Helper.SYS_COLUMN_VERSION_BYTES);
// byte[] sizeBytes = sysFamilyMap.get(Helper.SYS_COLUMN_SIZE_BYTES);
// long version = Bytes.toLong(versionBytes);
// int size = Bytes.toInt(sizeBytes);
byte[] key = Helper.hbaseKeyToAnts(r.getRow());
if (!Arrays.equals(key, antsKey)) {
throw new Exception("Row key not match - ANTS: " + bytesToHex(antsKey) + ", HBASE: " + bytesToHex(key));
}
int maxColumnId = row.getMaxColumnId();
// byte[] types = new byte[maxColumnId+1];
String errMsg = "";
for (int i=0; i<=maxColumnId; i++) {
byte[] qualifier = tableColumnQualifierList.get(i);
if (qualifier == null) {
continue;
}
long pValue = row.getFieldAddress(i);
byte[] antsValue = Helper.toBytes(pValue);
byte[] value = dataFamilyMap.get(qualifier);
if (!Arrays.equals(antsValue, value)) {
String columnName = new String(qualifier, StandardCharsets.US_ASCII);
if (errMsg.length() == 0) {
errMsg += "Row Key=[" + bytesToHex(key) + "]";
}
errMsg += String.format("\n Column %1$d '%2$s'[%3$s] not match - ANTS:[%4$s] HBASE:[%5$s]",
i, columnName, bytesToHex(qualifier), bytesToHex(antsValue), bytesToHex(value));
}
}
if (errMsg != "") {
throw new Exception(errMsg);
}
}
/**
* Returns the list of distinct versions for the given application sorted in
* reverse chronological order
*
* @param cluster
* @param user
* @param appId
* @return the list of versions sorted in reverse chronological order (the
* list will be empty if no versions are found)
* @throws IOException
*/
public List<VersionInfo> getDistinctVersions(String cluster, String user,
String appId) throws IOException {
Get get = new Get(getRowKey(cluster, user, appId));
List<VersionInfo> versions = Lists.newArrayList();
Long ts = 0L;
Table versionsTable = null;
try {
versionsTable = hbaseConnection
.getTable(TableName.valueOf(Constants.HISTORY_APP_VERSION_TABLE));
Result r = versionsTable.get(get);
if (r != null && !r.isEmpty()) {
for (Cell c : r.listCells()) {
ts = 0L;
try {
ts = Bytes.toLong(CellUtil.cloneValue(c));
versions.add(new VersionInfo(
Bytes.toString(CellUtil.cloneQualifier(c)), ts));
} catch (IllegalArgumentException e1) {
// Bytes.toLong may throw IllegalArgumentException, although
// unlikely.
LOG.error(
"Caught conversion error while converting timestamp to long value "
+ e1.getMessage());
// rethrow the exception in order to propagate it
throw e1;
}
}
}
if (versions.size() > 0) {
Collections.sort(versions);
}
} finally {
if (versionsTable != null) {
versionsTable.close();
}
}
return versions;
}