下面列出了org.apache.hadoop.hbase.client.Row#org.apache.hadoop.hbase.client.Get 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
public Optional<String> get(Pair<String, HBaseColumn> key) throws Exception {
if (key.getKey().isEmpty()) {
return Optional.absent();
}
Get g = new Get(Bytes.toBytes(key.getKey()));
HBaseColumn hBaseColumn = key.getValue();
if (hBaseColumn.getCf().isPresent() && hBaseColumn.getQualifier().isPresent()) {
g.addColumn(hBaseColumn.getCf().get(), hBaseColumn.getQualifier().get());
}
if (hBaseColumn.getTimestamp().isPresent()) {
g.setTimeStamp(hBaseColumn.getTimestamp().getAsLong());
}
Result result = hBaseProcessor.get(g);
String value = getValue(hBaseColumn, result);
return Optional.fromNullable(value);
}
@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();
}
@Override
public List<Long> timeMeta() {
List<Long> times = new ArrayList<>();
Get get = (new Get(HBASE_LATTICE_KEY)).addFamily(INST_FAMILY);
Result result;
try {
result = metaTable.get(get);
List<Cell> cells = result.listCells();
if (log.isDebugEnabled())
log.debug("Time lattice is {}", cells.stream()
.map(c -> Bytes.toLong(c.getValueArray(),
c.getValueOffset()))
.collect(Collectors.toList()));
for (Cell cell : cells) {
long current = Bytes.toLong(cell.getValueArray(), cell.getValueOffset());
times.add(current);
}
} catch (Exception e) {
if (log.isErrorEnabled())
log.error("get time lattice from hbase failed", e);
}
return times;
}
public Result[] getByIndex(byte[] value) throws IOException {
try {
transactionContext.start();
Scan scan = new Scan(value, Bytes.add(value, new byte[0]));
scan.addColumn(secondaryIndexFamily, secondaryIndexQualifier);
ResultScanner indexScanner = secondaryIndexTable.getScanner(scan);
ArrayList<Get> gets = new ArrayList<Get>();
for (Result result : indexScanner) {
for (Cell cell : result.listCells()) {
gets.add(new Get(cell.getValue()));
}
}
Result[] results = transactionAwareHTable.get(gets);
transactionContext.finish();
return results;
} catch (Exception e) {
try {
transactionContext.abort();
} catch (TransactionFailureException e1) {
throw new IOException("Could not rollback transaction", e1);
}
}
return null;
}
@Override
public void process(Object[] record) throws HiveException {
final String document = (String) stringOI.getPrimitiveJavaObject(record[0]);
if (document == null) {
return;
}
String[] tokens = document.split(",");
String[] results = tokens[1].split(" ");
try {
hTable = new HTable(conf, "bi");
Get get = new Get(Bytes.toBytes(tokens[0]));
result = hTable.exists(get);
} catch (Exception e) {
e.printStackTrace();
}
if (!result) {
for (String r : results) {
forward(new Object[]{tokens[0], r});
}
}
}
private void verifyRows(Table table, Get get, List<byte[]> expectedValues) throws Exception {
Result result = table.get(get);
if (expectedValues == null) {
assertTrue(result.isEmpty());
} else {
assertFalse(result.isEmpty());
byte[] family = TestBytes.family;
byte[] col = TestBytes.qualifier;
if (get.hasFamilies()) {
family = get.getFamilyMap().keySet().iterator().next();
col = get.getFamilyMap().get(family).first();
}
Iterator<Cell> it = result.getColumnCells(family, col).iterator();
for (byte[] expectedValue : expectedValues) {
Assert.assertTrue(it.hasNext());
assertArrayEquals(expectedValue, CellUtil.cloneValue(it.next()));
}
}
}
@Override
public JSONObject findById(String tableName, String id) {
if (isDisabled() || validator.isEmpty(tableName) || validator.isEmpty(id))
return null;
try {
Table table = getTable(tableName);
Result result = table.get(new Get(Bytes.toBytes(id)));
if (result == null || result.isEmpty()) {
table.close();
return null;
}
JSONObject object = new JSONObject();
setToJson(object, id, result);
table.close();
return object;
} catch (IOException e) {
logger.warn(e, "检索HBase数据[{}:{}]时发生异常!", tableName, id);
return null;
}
}
@Test
public void testPositiveAuthentication() throws Exception {
// Validate that we can read that record back out as the user with our custom auth'n
UserGroupInformation user1 = UserGroupInformation.createUserForTesting("user1", new String[0]);
user1.addToken(createPasswordToken("user1", USER1_PASSWORD, clusterId));
user1.doAs(new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
try (Connection conn = ConnectionFactory.createConnection(getClientConf());
Table t = conn.getTable(tableName)) {
Result r = t.get(new Get(Bytes.toBytes("r1")));
assertNotNull(r);
assertFalse("Should have read a non-empty Result", r.isEmpty());
final Cell cell = r.getColumnLatestCell(Bytes.toBytes("f1"), Bytes.toBytes("q1"));
assertTrue("Unexpected value", CellUtil.matchingValue(cell, Bytes.toBytes("1")));
return null;
}
}
});
}
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;
}
public ResultScanner getVer(byte[] tableName, byte[] rowkey,
byte[] columnFamily, String[] columns, int ver) throws IOException {
table = new HTable(config, tableName);
Get get = new Get(rowkey);
if (null != columnFamily && null != columns && columns.length > 0) {
for (int i = 0; i < columns.length; i++) {
get.addColumn(columnFamily, Bytes.toBytes(columns[i]));
}
} else if (null != columnFamily
&& (null == columns || columns.length == 0)) {
get.addFamily(columnFamily);
}
Scan scanner = new Scan(get);
scanner.setMaxVersions(ver);
return table.getScanner(scanner);
}
private Get buildGet(HbData opData) throws IOException {
Get get = new Get(opData.getRowKey());
if (opData.getMaxVersion() > 0) {
get.setMaxVersions(opData.getMaxVersion());
}
for (HbColumn column : opData.getColumns()) {
if (column.getTimestamp() > 0) {
get.setTimeStamp(column.getTimestamp());
} else if (opData.getStartTime() > 0 && opData.getEndTime() > 0
&& opData.getEndTime() > opData.getStartTime()) {
get.setTimeRange(opData.getStartTime(), opData.getEndTime());
}
if (StringUtils.isNotEmpty(column.getColumnFamily()) && StringUtils.isNotEmpty(column.getColumnName())) {
get.addColumn(Bytes.toBytes(column.getColumnFamily()), Bytes.toBytes(column.getColumnName()));
}
}
return get;
}
/**
* Get backup set description (list of tables)
* @param name set's name
* @return list of tables in a backup set
* @throws IOException if a table operation fails
*/
public List<TableName> describeBackupSet(String name) throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace(" Backup set describe: " + name);
}
try (Table table = connection.getTable(tableName)) {
Get get = createGetForBackupSet(name);
Result res = table.get(get);
if (res.isEmpty()) {
return null;
}
res.advance();
String[] tables = cellValueToBackupSet(res.current());
return Arrays.asList(tables).stream().map(item -> TableName.valueOf(item))
.collect(Collectors.toList());
}
}
/**
* Ensure that optimize does not cause the Get to do more seeking than required. Optimize
* (see HBASE-15392) was causing us to seek all Cells in a block when a Get Scan if the next block
* index/start key was a different row to the current one. A bug. We'd call next too often
* because we had to exhaust all Cells in the current row making us load the next block just to
* discard what we read there. This test is a little cryptic. Takes a bit of staring to figure
* what it up to.
*/
@Test
public void testOptimizeAndGetWithFakedNextBlockIndexStart() throws IOException {
// First test a Get of second column in the row R2. Every Get is a Scan. Second column has a
// qualifier of R2.
Get get = new Get(THREE);
get.addColumn(CF, TWO);
Scan scan = new Scan(get);
try (CellGridStoreScanner scanner = new CellGridStoreScanner(scan, this.scanInfo)) {
List<Cell> results = new ArrayList<>();
// For a Get there should be no more next's after the first call.
assertEquals(false, scanner.next(results));
// Should be one result only.
assertEquals(1, results.size());
// And we should have gone through optimize twice only.
assertEquals("First qcode is SEEK_NEXT_COL and second INCLUDE_AND_SEEK_NEXT_ROW", 2,
scanner.count.get());
}
}
@Override
public Get getGet(byte[] row) {
Get get = new Get(row);
if (isSafeMode) {
return get;
}
for (Entry<byte[], NavigableSet<byte[]>> familyMapEntry : familyMap.entrySet()) {
// see DefaultResultToSolrMapper
byte[] columnFamily = familyMapEntry.getKey();
if (familyMapEntry.getValue() == null) {
get.addFamily(columnFamily);
} else {
for (byte[] qualifier : familyMapEntry.getValue()) {
get.addColumn(columnFamily, qualifier);
}
}
}
return get;
}
protected KVSerializable getRow(String tableName, Class clazz, byte[] key) {
HTableInterface table = getHTableInterface(tableName);
Get get = new Get(key);
HTableInterface htable;
try {
htable = getHTableInterface(tableName);
KVSerializable kvInst = (KVSerializable) clazz.getConstructors()[0].newInstance();
Result result = htable.get(get);
if (result != null) {
kvInst.fromKV(key, result.getValue(CF, V_DATA));
return kvInst;
}
} catch (Exception ex) {
logger.error("Scan metric meta error, class:{}", clazz.getSimpleName(), ex);
} finally {
closeTable(table);
}
return null;
}
public Result[] getByIndex(byte[] value) throws IOException {
try {
transactionContext.start();
Scan scan = new Scan(value, Bytes.add(value, new byte[0]));
scan.addColumn(secondaryIndexFamily, secondaryIndexQualifier);
ResultScanner indexScanner = secondaryIndexTable.getScanner(scan);
ArrayList<Get> gets = new ArrayList<>();
for (Result result : indexScanner) {
for (Cell cell : result.listCells()) {
gets.add(new Get(cell.getValue()));
}
}
Result[] results = transactionAwareHTable.get(gets);
transactionContext.finish();
return results;
} catch (Exception e) {
try {
transactionContext.abort();
} catch (TransactionFailureException e1) {
throw new IOException("Could not rollback transaction", e1);
}
}
return null;
}
/**
* Here all qualifiers' values goes into qualifierValues of InternalLog as given a row, we can't
* differentiate it's a tag or a field
*
* @param rowkeys
* @return
* @throws IOException
*/
public List<InternalLog> get(List<byte[]> rowkeys) throws IOException, NoSuchRowException {
final List<Get> gets = createGets(rowkeys);
final Result[] results = tbl.get(gets);
final List<InternalLog> logs = new ArrayList<InternalLog>();
for (Result result : results) {
final InternalLog log = buildLog(result);
logs.add(log);
}
return logs;
}
protected final void verify(HBaseTestingUtility util, int start, int end) throws IOException {
try (Table table = util.getConnection().getTable(TABLE_NAME)) {
for (int i = start; i < end; i++) {
assertEquals(i, Bytes.toInt(table.get(new Get(Bytes.toBytes(i))).getValue(CF, CQ)));
}
}
}
private List<? extends Row> transactionalizeActions(List<? extends Row> actions) throws IOException {
List<Row> transactionalizedActions = new ArrayList<>(actions.size());
for (Row action : actions) {
if (action instanceof Get) {
transactionalizedActions.add(transactionalizeAction((Get) action));
} else if (action instanceof Put) {
transactionalizedActions.add(transactionalizeAction((Put) action));
} else if (action instanceof Delete) {
transactionalizedActions.add(transactionalizeAction((Delete) action));
} else {
transactionalizedActions.add(action);
}
}
return transactionalizedActions;
}
@Override
public Result[] get(List<Get> gets) throws IOException {
List<TGet> tGets = ThriftUtilities.getsFromHBase(gets);
try {
List<TResult> results = client.getMultiple(tableNameInBytes, tGets);
return ThriftUtilities.resultsFromThrift(results);
} catch (TException e) {
throw new IOException(e);
}
}
@Override
public void preGetOp(ObserverContext<RegionCoprocessorEnvironment> e, Get get, List<Cell> results)
throws IOException {
Transaction tx = getFromOperation(get);
if (tx != null) {
projectFamilyDeletes(get);
get.setMaxVersions();
get.setTimeRange(TxUtils.getOldestVisibleTimestamp(ttlByFamily, tx, readNonTxnData),
TxUtils.getMaxVisibleTimestamp(tx));
Filter newFilter = getTransactionFilter(tx, ScanType.USER_SCAN, get.getFilter());
get.setFilter(newFilter);
}
}
@Test
public void testVisibilityLabelsForUserWithNoAuths() throws Throwable {
String user = "admin";
String[] auths = { SECRET };
try (Connection conn = ConnectionFactory.createConnection(conf)) {
VisibilityClient.clearAuths(conn, auths, user); // Removing all auths if any.
VisibilityClient.setAuths(conn, auths, "user1");
}
TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
final Table table = createTableAndWriteDataWithLabels(tableName, SECRET);
SecureTestUtil.grantOnTable(TEST_UTIL, NORMAL_USER1.getShortName(), tableName,
null, null, Permission.Action.READ);
SecureTestUtil.grantOnTable(TEST_UTIL, NORMAL_USER2.getShortName(), tableName,
null, null, Permission.Action.READ);
PrivilegedExceptionAction<Void> getAction = new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
Get g = new Get(row1);
g.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
try (Connection connection = ConnectionFactory.createConnection(conf);
Table t = connection.getTable(table.getName())) {
Result result = t.get(g);
assertTrue(result.isEmpty());
}
return null;
}
};
NORMAL_USER2.runAs(getAction);
}
@Test
public void testMultiColumnFamilyRowDeleteRollback() throws Exception {
Table hTable = createTable(Bytes.toBytes("TestMultColFam"), new byte[][] {TestBytes.family, TestBytes.family2});
try (TransactionAwareHTable txTable = new TransactionAwareHTable(hTable, TxConstants.ConflictDetection.ROW)) {
TransactionContext txContext = new TransactionContext(new InMemoryTxSystemClient(txManager), txTable);
txContext.start();
txTable.put(new Put(TestBytes.row).add(TestBytes.family, TestBytes.qualifier, TestBytes.value));
txContext.finish();
txContext.start();
//noinspection ConstantConditions
txContext.getCurrentTransaction().setVisibility(Transaction.VisibilityLevel.SNAPSHOT_ALL);
Result result = txTable.get(new Get(TestBytes.row));
Assert.assertEquals(1, result.getFamilyMap(TestBytes.family).size());
Assert.assertEquals(0, result.getFamilyMap(TestBytes.family2).size());
txContext.finish();
//Start a tx, delete the row and then abort the tx
txContext.start();
txTable.delete(new Delete(TestBytes.row));
txContext.abort();
//Start a tx and scan all the col families to make sure none of them have delete markers
txContext.start();
txContext.getCurrentTransaction().setVisibility(Transaction.VisibilityLevel.SNAPSHOT_ALL);
result = txTable.get(new Get(TestBytes.row));
Assert.assertEquals(1, result.getFamilyMap(TestBytes.family).size());
Assert.assertEquals(0, result.getFamilyMap(TestBytes.family2).size());
txContext.finish();
}
}
private RegionState.State getCurrentRegionState(RegionInfo regionInfo) throws IOException{
Table metaTable = TEST_UTIL.getConnection().getTable(TableName.valueOf("hbase:meta"));
Get get = new Get(regionInfo.getRegionName());
get.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER);
Result result = metaTable.get(get);
byte[] currentStateValue = result.getValue(HConstants.CATALOG_FAMILY,
HConstants.STATE_QUALIFIER);
return currentStateValue != null ?
RegionState.State.valueOf(Bytes.toString(currentStateValue))
: null;
}
protected static void workaroundHBASE2198(Get get, Filter filter,byte[][] qualifiers) {
if (filter instanceof SingleColumnValueFilter) {
if(qualifiers == null) {
get.addFamily(((SingleColumnValueFilter) filter).getFamily());
}else{
get.addColumn(((SingleColumnValueFilter) filter).getFamily(), ((SingleColumnValueFilter) filter).getQualifier());
}
return;
}
if (filter instanceof FilterList) {
for (Filter f : ((FilterList)filter).getFilters()) {
workaroundHBASE2198(get, f,qualifiers);
}
}
}
@Test
public void testRecoverStandby() throws IOException, StreamLacksCapabilityException {
setupSyncReplicationWALs();
long procId = procExec.submitProcedure(new RecoverStandbyProcedure(PEER_ID, false));
ProcedureTestingUtility.waitProcedure(procExec, procId);
ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
try (Table table = UTIL.getConnection().getTable(tableName)) {
for (int i = 0; i < WAL_NUMBER * ROW_COUNT; i++) {
Result result = table.get(new Get(Bytes.toBytes(i)).setTimestamp(timestamp));
assertNotNull(result);
assertEquals(i, Bytes.toInt(result.getValue(family, qualifier)));
}
}
}
private List<? extends Row> transactionalizeActions(List<? extends Row> actions) throws IOException {
List<Row> transactionalizedActions = new ArrayList<>(actions.size());
for (Row action : actions) {
if (action instanceof Get) {
transactionalizedActions.add(transactionalizeAction((Get) action));
} else if (action instanceof Put) {
transactionalizedActions.add(transactionalizeAction((Put) action));
} else if (action instanceof Delete) {
transactionalizedActions.add(transactionalizeAction((Delete) action));
} else {
transactionalizedActions.add(action);
}
}
return transactionalizedActions;
}
@Override
public void postGetOp(ObserverContext<RegionCoprocessorEnvironment> e, Get get,
List<Cell> results) throws IOException {
if (results.size() > 0) {
// Check tag presence in the 1st cell in 1st Result
if (!results.isEmpty()) {
Cell cell = results.get(0);
TAGS = PrivateCellUtil.getTags(cell);
}
}
}
private List<? extends Row> transactionalizeActions(List<? extends Row> actions) throws IOException {
List<Row> transactionalizedActions = new ArrayList<>(actions.size());
for (Row action : actions) {
if (action instanceof Get) {
transactionalizedActions.add(transactionalizeAction((Get) action));
} else if (action instanceof Put) {
transactionalizedActions.add(transactionalizeAction((Put) action));
} else if (action instanceof Delete) {
transactionalizedActions.add(transactionalizeAction((Delete) action));
} else {
transactionalizedActions.add(action);
}
}
return transactionalizedActions;
}