org.apache.hadoop.hbase.client.Connection.getTable()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(252)

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

Connection.getTable介绍

[英]Retrieve a Table implementation for accessing a table. The returned Table is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned Table is neither required nor desired.

The caller is responsible for calling Table#close() on the returned table instance.

Since 0.98.1 this method no longer checks table existence. An exception will be thrown if the table does not exist only when the first operation is attempted.
[中]检索用于访问表的表实现。返回的表不是线程安全的,应该为每个使用线程创建一个新实例。这是一个轻量级操作,不需要也不需要对返回的表进行池或缓存。
调用者负责对返回的表实例调用表#close()。
自0.98.1以来,此方法不再检查表是否存在。如果表不存在,则仅在尝试第一个操作时才会引发异常。

代码示例

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

private void connectToTable() {
  if (this.conf == null) {
    this.conf = HBaseConfiguration.create();
  }
  try {
    Connection conn = ConnectionFactory.createConnection(conf);
    super.table = (HTable) conn.getTable(TableName.valueOf(tableName));
  } catch (TableNotFoundException tnfe) {
    LOG.error("The table " + tableName + " not found ", tnfe);
    throw new RuntimeException("HBase table '" + tableName + "' not found.", tnfe);
  } catch (IOException ioe) {
    LOG.error("Exception while creating connection to HBase.", ioe);
    throw new RuntimeException("Cannot create connection to HBase.", ioe);
  }
}

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

@Override
public TableMask getTable(String name) throws IOException
{
  return new HTable1_0(cnx.getTable(TableName.valueOf(name)));
}

代码示例来源:origin: alibaba/canal

boolean flag = false;
try {
  HTable table = (HTable) getConnection().getTable(TableName.valueOf(tableName));
  List<Put> puts = new ArrayList<>();
  for (HRow hRow : rows) {
    Put put = new Put(hRow.getRowKey());
    for (HRow.HCell hCell : hRow.getCells()) {
      put.addColumn(Bytes.toBytes(hCell.getFamily()),
        Bytes.toBytes(hCell.getQualifier()),
        hCell.getValue());

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

/**
 * Test multithreadedTableMappper map/reduce against a multi-region table
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws InterruptedException
 */
@Test
public void testMultithreadedTableMapper()
  throws IOException, InterruptedException, ClassNotFoundException {
 runTestOnTable(UTIL.getConnection().getTable(MULTI_REGION_TABLE_NAME));
}

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

@Override
 public Object run() throws Exception {
  Put p = new Put(Bytes.toBytes("a"));
  p.addColumn(family2, qualifier, Bytes.toBytes("v2"));
  try (Connection conn = ConnectionFactory.createConnection(conf);
    Table t = conn.getTable(tableName)) {
   t.put(p);
  }
  return null;
 }
};

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

@Override
 public Void run() throws Exception {
  try (Connection connection = ConnectionFactory.createConnection(conf);
     Table table = connection.getTable(tableName)) {
   Append append = new Append(row1);
   append.addColumn(fam, qual, Bytes.toBytes("b"));
   table.append(append);
  }
  return null;
 }
};

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

public static void insertNamespaceToMeta(Connection conn, NamespaceDescriptor ns)
  throws IOException {
 byte[] row = Bytes.toBytes(ns.getName());
 Put put = new Put(row, true).addColumn(HConstants.NAMESPACE_FAMILY,
  HConstants.NAMESPACE_COL_DESC_QUALIFIER,
  ProtobufUtil.toProtoNamespaceDescriptor(ns).toByteArray());
 try (Table table = conn.getTable(TableName.META_TABLE_NAME)) {
  table.put(put);
 }
}

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

private Table createTable() throws IOException {
 TableName tableName = TableName.valueOf(name.getMethodName());
 HTableDescriptor table = new HTableDescriptor(tableName);
 HColumnDescriptor fam = new HColumnDescriptor(FAMILY);
 fam.setNewVersionBehavior(true);
 fam.setMaxVersions(3);
 table.addFamily(fam);
 TEST_UTIL.getHBaseAdmin().createTable(table);
 return TEST_UTIL.getConnection().getTable(tableName);
}

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

public static void verifyMobRowCount(final HBaseTestingUtility util,
  final TableName tableName, long expectedRows) throws IOException {
 Table table = ConnectionFactory.createConnection(util.getConfiguration()).getTable(tableName);
 try {
  assertEquals(expectedRows, countMobRows(table));
 } finally {
  table.close();
 }
}

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

/**
 * Return the number of rows in the given table.
 */
public int countRows(final TableName tableName) throws IOException {
 Table table = getConnection().getTable(tableName);
 try {
  return countRows(table);
 } finally {
  table.close();
 }
}

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

@Override
 public Object run() throws Exception {
  Delete d = new Delete(TEST_ROW);
  d.addFamily(family1);
  try (Connection conn = ConnectionFactory.createConnection(conf);
    Table t = conn.getTable(tableName)) {
   t.delete(d);
  }
  return null;
 }
};

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

private void restoreVerifyTable(Connection conn, BackupAdmin client, TableName table,
  String backupId, long expectedRows) throws IOException {
 TableName[] tablesRestoreIncMultiple = new TableName[] { table };
 restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
  tablesRestoreIncMultiple, null, true), client);
 Table hTable = conn.getTable(table);
 Assert.assertEquals(expectedRows, util.countRows(hTable));
 hTable.close();
}

代码示例来源:origin: alibaba/canal

/**
 * 插入一行数据
 *
 * @param tableName 表名
 * @param hRow 行数据对象
 * @return 是否成功
 */
public Boolean put(String tableName, HRow hRow) {
  boolean flag = false;
  try {
    HTable table = (HTable) getConnection().getTable(TableName.valueOf(tableName));
    Put put = new Put(hRow.getRowKey());
    for (HRow.HCell hCell : hRow.getCells()) {
      put.addColumn(Bytes.toBytes(hCell.getFamily()), Bytes.toBytes(hCell.getQualifier()), hCell.getValue());
    }
    table.put(put);
    flag = true;
  } catch (Exception e) {
    logger.error(e.getMessage(), e);
    throw new RuntimeException(e);
  }
  return flag;
}

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

/**
 * Test a map/reduce against a multi-region table
 * @throws IOException
 */
@Test
public void testMultiRegionTable() throws IOException {
 runTestOnTable(UTIL.getConnection().getTable(MULTI_REGION_TABLE_NAME));
}

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

@Override
 public Object run() throws Exception {
  Put p = new Put(TEST_ROW);
  p.addColumn(family1, qualifier, Bytes.toBytes("v1"));
  try (Connection conn = ConnectionFactory.createConnection(conf);
    Table t = conn.getTable(tableName)) {
   t.put(p);
  }
  return null;
 }
};

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

@Override
 public Object run() throws Exception {
  try (Connection connection = ConnectionFactory.createConnection(conf);
    Table acl = connection.getTable(AccessControlLists.ACL_TABLE_NAME)) {
   BlockingRpcChannel service = acl.coprocessorService(HConstants.EMPTY_START_ROW);
   AccessControlService.BlockingInterface protocol =
     AccessControlService.newBlockingStub(service);
   AccessControlUtil.getUserPermissions(null, protocol, Bytes.toBytes(TEST_NAMESPACE));
  }
  return null;
 }
};

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

HTable insertIntoTable(Connection conn, TableName table, byte[] family, int id, int numRows)
  throws IOException {
 HTable t = (HTable) conn.getTable(table);
 Put p1;
 for (int i = 0; i < numRows; i++) {
  p1 = new Put(Bytes.toBytes("row-" + table + "-" + id + "-" + i));
  p1.addColumn(family, qualName, Bytes.toBytes("val" + i));
  t.put(p1);
 }
 return t;
}

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

/**
 * Caller closes the table afterwards.
 */
public Table getTable(String tableName) throws IOException {
 ConnectionInfo connInfo = getCurrentConnection();
 return connInfo.connection.getTable(TableName.valueOf(tableName));
}

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

@Override
protected Table createTable(byte[] fam) throws IOException {
 TableName tableName = TableName.valueOf(testName.getMethodName());
 TEST_UTIL.getAdmin().createTable(TableDescriptorBuilder.newBuilder(tableName)
   .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam)).build());
 return TEST_UTIL.getConnection().getTable(tableName);
}

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

public void setUp(long threshold, String TN, DataBlockEncoding encoding)
  throws Exception {
 desc = new HTableDescriptor(TableName.valueOf(TN));
 hcd = new HColumnDescriptor(family);
 hcd.setMobEnabled(true);
 hcd.setMobThreshold(threshold);
 hcd.setMaxVersions(4);
 hcd.setDataBlockEncoding(encoding);
 desc.addFamily(hcd);
 admin = TEST_UTIL.getAdmin();
 admin.createTable(desc);
 table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
     .getTable(TableName.valueOf(TN));
}

相关文章