org.apache.hadoop.hbase.client.Table类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(194)

本文整理了Java中org.apache.hadoop.hbase.client.Table类的一些代码示例,展示了Table类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Table类的具体详情如下:
包路径:org.apache.hadoop.hbase.client.Table
类名称:Table

Table介绍

[英]Used to communicate with a single HBase table. Obtain an instance from a Connection and call #close() afterwards.

Table can be used to get, put, delete or scan data from a table.
[中]用于与单个HBase表通信。从连接中获取一个实例,然后调用#close()。
表可用于从表中获取、放置、删除或扫描数据。

代码示例

代码示例来源:origin: thinkaurelius/titan

  1. @Override
  2. public ResultScanner getScanner(Scan filter) throws IOException
  3. {
  4. return table.getScanner(filter);
  5. }

代码示例来源:origin: apache/hbase

  1. @Override
  2. public Object run() throws Exception {
  3. try(Connection conn = ConnectionFactory.createConnection(conf);
  4. Table t = conn.getTable(TEST_TABLE)) {
  5. return t.get(new Get(TEST_ROW));
  6. }
  7. }
  8. };

代码示例来源:origin: apache/hbase

  1. @Override
  2. public void workDone() throws IOException {
  3. try {
  4. table.close();
  5. } finally {
  6. connection.close();
  7. }
  8. }

代码示例来源:origin: apache/hbase

  1. /**
  2. * Invokes Table#delete to delete test data (i.e. the row)
  3. *
  4. * @param table Standard Table object
  5. * @throws IOException If IO problem is encountered
  6. */
  7. static void deleteRow(final Table table) throws IOException {
  8. System.out.println("Deleting row [" + Bytes.toString(MY_ROW_ID)
  9. + "] from Table ["
  10. + table.getName().getNameAsString() + "].");
  11. table.delete(new Delete(MY_ROW_ID));
  12. }

代码示例来源:origin: apache/hbase

  1. @Override
  2. protected void closeHTable() {
  3. if (table != null) {
  4. try {
  5. table.close();
  6. } catch (Exception e) {
  7. LOG.error("Error in closing the table "+table.getName(), e);
  8. }
  9. }
  10. }

代码示例来源:origin: apache/hbase

  1. @Override
  2. public Object run() throws Exception {
  3. Put p = new Put(TEST_ROW);
  4. p.addColumn(family1, qualifier, Bytes.toBytes("v1"));
  5. try (Connection conn = ConnectionFactory.createConnection(conf);
  6. Table t = conn.getTable(tableName)) {
  7. t.put(p);
  8. }
  9. return null;
  10. }
  11. };

代码示例来源:origin: apache/hbase

  1. /**
  2. * Test read only tables
  3. * @throws Exception
  4. */
  5. @Test
  6. public void testReadOnlyTable() throws Exception {
  7. final TableName name = TableName.valueOf(this.name.getMethodName());
  8. Table table = TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
  9. byte[] value = Bytes.toBytes("somedata");
  10. // This used to use an empty row... That must have been a bug
  11. Put put = new Put(value);
  12. put.addColumn(HConstants.CATALOG_FAMILY, HConstants.CATALOG_FAMILY, value);
  13. table.put(put);
  14. table.close();
  15. }

代码示例来源:origin: apache/hbase

  1. protected final void write(HBaseTestingUtility util, int start, int end) throws IOException {
  2. try (Table table = util.getConnection().getTable(TABLE_NAME)) {
  3. for (int i = start; i < end; i++) {
  4. table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
  5. }
  6. }
  7. }

代码示例来源:origin: apache/hbase

  1. private static final void createTable(HTableDescriptor desc) throws Exception {
  2. Admin admin = TEST_UTIL.getAdmin();
  3. admin.createTable(desc, new byte[][]{ROWS[rowSeperator1], ROWS[rowSeperator2]});
  4. TEST_UTIL.waitUntilAllRegionsAssigned(desc.getTableName());
  5. Table table = TEST_UTIL.getConnection().getTable(desc.getTableName());
  6. try {
  7. for (int i = 0; i < ROWSIZE; i++) {
  8. Put put = new Put(ROWS[i]);
  9. put.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(i));
  10. table.put(put);
  11. }
  12. } finally {
  13. table.close();
  14. }
  15. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testFlush() throws IOException {
  3. byte[] row = Bytes.toBytes("row");
  4. byte[] cf = Bytes.toBytes("cf");
  5. byte[] cq = Bytes.toBytes("cq");
  6. byte[] value = Bytes.toBytes("value");
  7. TableName name = TableName.valueOf(getClass().getSimpleName());
  8. Table t = util.createTable(name, cf);
  9. t.put(new Put(row).addColumn(cf, cq, value));
  10. util.getAdmin().flush(name);
  11. assertArrayEquals(value, t.get(new Get(row)).getValue(cf, cq));
  12. }
  13. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testBatchWithRowMutation() throws Exception {
  3. LOG.info("Starting testBatchWithRowMutation");
  4. final TableName TABLENAME = TableName.valueOf("testBatchWithRowMutation");
  5. try (Table t = TEST_UTIL.createTable(TABLENAME, FAMILY)) {
  6. byte [][] QUALIFIERS = new byte [][] {
  7. Bytes.toBytes("a"), Bytes.toBytes("b")
  8. };
  9. new Put(ROW).addColumn(FAMILY, QUALIFIERS[0], VALUE)));
  10. Object[] batchResult = new Object[1];
  11. t.batch(Arrays.asList(arm), batchResult);
  12. Result r = t.get(g);
  13. assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0])));
  14. new Put(ROW).addColumn(FAMILY, QUALIFIERS[1], VALUE),
  15. new Delete(ROW).addColumns(FAMILY, QUALIFIERS[0])));
  16. t.batch(Arrays.asList(arm), batchResult);
  17. r = t.get(g);
  18. assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[1])));
  19. assertNull(r.getValue(FAMILY, QUALIFIERS[0]));
  20. arm = RowMutations.of(Collections.singletonList(
  21. new Put(ROW).addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE)));
  22. t.batch(Arrays.asList(arm), batchResult);
  23. fail("Expected RetriesExhaustedWithDetailsException with NoSuchColumnFamilyException");
  24. } catch(RetriesExhaustedWithDetailsException e) {

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testHTableExistsBeforeGet() throws Exception {
  3. Table table = TEST_UTIL.createTable(TableName.valueOf(name.getMethodName()),
  4. new byte[][] { FAMILY });
  5. try {
  6. Put put = new Put(ROW);
  7. put.addColumn(FAMILY, QUALIFIER, VALUE);
  8. table.put(put);
  9. Get get = new Get(ROW);
  10. boolean exist = table.exists(get);
  11. assertEquals(true, exist);
  12. Result result = table.get(get);
  13. assertEquals(false, result.isEmpty());
  14. assertTrue(Bytes.equals(VALUE, result.getValue(FAMILY, QUALIFIER)));
  15. } finally {
  16. table.close();
  17. }
  18. }

代码示例来源:origin: apache/hbase

  1. @Test(expected=RetriesExhaustedException.class)
  2. public void testSocketClosed() throws IOException, InterruptedException {
  3. TableName tableName = TableName.valueOf(name.getMethodName());
  4. UTIL.createTable(tableName, fam1).close();
  5. Configuration conf = new Configuration(UTIL.getConfiguration());
  6. conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
  7. MyRpcClientImpl.class.getName());
  8. conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
  9. Connection connection = ConnectionFactory.createConnection(conf);
  10. Table table = connection.getTable(TableName.valueOf(name.getMethodName()));
  11. table.get(new Get(Bytes.toBytes("asd")));
  12. connection.close();
  13. for (Socket socket : MyRpcClientImpl.savedSockets) {
  14. assertTrue("Socket + " + socket + " is not closed", socket.isClosed());
  15. }
  16. }
  17. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testBatchWithIncrementAndAppend() throws Exception {
  3. LOG.info("test=testBatchWithIncrementAndAppend");
  4. final byte[] QUAL1 = Bytes.toBytes("qual1");
  5. final byte[] QUAL2 = Bytes.toBytes("qual2");
  6. final byte[] QUAL3 = Bytes.toBytes("qual3");
  7. final byte[] QUAL4 = Bytes.toBytes("qual4");
  8. Table table = UTIL.getConnection().getTable(TEST_TABLE);
  9. Delete d = new Delete(ONE_ROW);
  10. table.delete(d);
  11. Put put = new Put(ONE_ROW);
  12. put.addColumn(BYTES_FAMILY, QUAL1, Bytes.toBytes("abc"));
  13. put.addColumn(BYTES_FAMILY, QUAL2, Bytes.toBytes(1L));
  14. table.put(put);
  15. table.batch(actions, multiRes);
  16. validateResult(multiRes[1], QUAL1, Bytes.toBytes("abcdef"));
  17. validateResult(multiRes[1], QUAL4, Bytes.toBytes("xyz"));
  18. validateResult(multiRes[0], QUAL2, Bytes.toBytes(2L));
  19. validateResult(multiRes[0], QUAL3, Bytes.toBytes(1L));
  20. table.close();

代码示例来源:origin: apache/hbase

  1. /**
  2. * For HADOOP-2579
  3. * @throws IOException
  4. */
  5. @Test (expected=TableNotFoundException.class)
  6. public void testTableNotFoundExceptionWithoutAnyTables() throws IOException {
  7. TableName tableName = TableName
  8. .valueOf("testTableNotFoundExceptionWithoutAnyTables");
  9. Table ht = TEST_UTIL.getConnection().getTable(tableName);
  10. ht.get(new Get(Bytes.toBytes("e")));
  11. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testDisabledWAL() throws Exception {
  3. LOG.info("Writing data to table " + tableName);
  4. Put p = new Put(Bytes.toBytes("row"));
  5. p.addColumn(fam, Bytes.toBytes("qual"), Bytes.toBytes("val"));
  6. table.put(p);
  7. LOG.info("Flushing table " + tableName);
  8. TEST_UTIL.flush(tableName);
  9. LOG.info("Getting data from table " + tableName);
  10. Get get = new Get(Bytes.toBytes("row"));
  11. Result result = table.get(get);
  12. assertNotNull(result.getValue(fam, Bytes.toBytes("qual")));
  13. }
  14. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testIncrement() throws IOException {
  3. try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
  4. Put put = new Put(ROW);
  5. put.addColumn(FAMILY, QUAL, VALUE);
  6. t.put(put);
  7. assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE);
  8. Increment inc = new Increment(ROW);
  9. inc.addColumn(FAMILY, QUAL, 99);
  10. assertRowAndValue(t.increment(inc), ROW, FIXED_VALUE);
  11. assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.toBytes(199L));
  12. }
  13. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testBatchAppendWithReturnResultFalse() throws Exception {
  3. LOG.info("Starting testBatchAppendWithReturnResultFalse");
  4. final TableName tableName = TableName.valueOf(name.getMethodName());
  5. Table table = TEST_UTIL.createTable(tableName, FAMILY);
  6. Append append1 = new Append(Bytes.toBytes("row1"));
  7. append1.setReturnResults(false);
  8. append1.addColumn(FAMILY, Bytes.toBytes("f1"), Bytes.toBytes("value1"));
  9. Append append2 = new Append(Bytes.toBytes("row1"));
  10. append2.setReturnResults(false);
  11. append2.addColumn(FAMILY, Bytes.toBytes("f1"), Bytes.toBytes("value2"));
  12. List<Append> appends = new ArrayList<>();
  13. appends.add(append1);
  14. appends.add(append2);
  15. Object[] results = new Object[2];
  16. table.batch(appends, results);
  17. assertTrue(results.length == 2);
  18. for(Object r : results) {
  19. Result result = (Result)r;
  20. assertTrue(result.isEmpty());
  21. }
  22. table.close();
  23. }

代码示例来源:origin: apache/hbase

  1. public int testGet(final RegionInfo hri, final int nrows) throws IOException {
  2. int nresults = 0;
  3. final Table table = UTIL.getConnection().getTable(hri.getTable());
  4. for (int i = 0; i < nrows; ++i) {
  5. final byte[] row = Bytes.add(hri.getStartKey(), Bytes.toBytes(i));
  6. final Result result = table.get(new Get(row));
  7. if (result != null && !result.isEmpty() &&
  8. Bytes.equals(row, result.getValue(FAMILY, null))) {
  9. nresults++;
  10. }
  11. }
  12. return nresults;
  13. }
  14. }

代码示例来源:origin: apache/hbase

  1. @Test
  2. public void testHTableWithLargeBatch() throws Exception {
  3. Table table = TEST_UTIL.createTable(TableName.valueOf(name.getMethodName()),
  4. new byte[][] { FAMILY });
  5. int sixtyFourK = 64 * 1024;
  6. try {
  7. List actions = new ArrayList();
  8. Object[] results = new Object[(sixtyFourK + 1) * 2];
  9. for (int i = 0; i < sixtyFourK + 1; i ++) {
  10. Put put1 = new Put(ROW);
  11. put1.addColumn(FAMILY, QUALIFIER, VALUE);
  12. actions.add(put1);
  13. Put put2 = new Put(ANOTHERROW);
  14. put2.addColumn(FAMILY, QUALIFIER, VALUE);
  15. actions.add(put2);
  16. }
  17. table.batch(actions, results);
  18. } finally {
  19. table.close();
  20. }
  21. }

相关文章