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

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

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

Connection介绍

[英]A cluster connection encapsulating lower level individual connections to actual servers and a connection to zookeeper. Connections are instantiated through the ConnectionFactoryclass. The lifecycle of the connection is managed by the caller, who has to #close()the connection to release the resources.

The connection object contains logic to find the master, locate regions out on the cluster, keeps a cache of locations and then knows how to re-calibrate after they move. The individual connections to servers, meta cache, zookeeper connection, etc are all shared by the Table and Admin instances obtained from this connection.

Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so that the client can create a connection once, and share it with different threads. Table and Admin instances, on the other hand, are light-weight and are not thread-safe. Typically, a single connection per client application is instantiated and every thread will obtain its own Table instance. Caching or pooling of Table and Adminis not recommended.
[中]群集连接封装了到实际服务器的低级单独连接和到zookeeper的连接。连接通过ConnectionFactoryclass实例化。连接的生命周期由调用方管理,调用方必须#关闭()连接以释放资源。
连接对象包含查找主节点、在集群上定位区域、保持位置缓存以及在位置移动后知道如何重新校准的逻辑。到服务器、元缓存、zookeeper连接等的各个连接都由从该连接获得的表和管理实例共享。
连接创建是一项繁重的操作。连接实现是线程安全的,因此客户端可以创建一次连接,并与不同的线程共享。另一方面,Table和Admin实例是轻量级的,并且不是线程安全的。通常,每个客户端应用程序都会实例化一个连接,每个线程都会获得自己的表实例。不建议缓存或池化表和管理员。

代码示例

代码示例来源: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: alibaba/canal

public boolean tableExists(String tableName) {
  try (HBaseAdmin admin = (HBaseAdmin) getConnection().getAdmin()) {
    return admin.tableExists(TableName.valueOf(tableName));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

private void generatePartitions(Path partitionsPath) throws IOException {
 Connection connection = ConnectionFactory.createConnection(getConf());
 Pair<byte[][], byte[][]> regionKeys
  = connection.getRegionLocator(TableName.valueOf(tableHash.tableName)).getStartEndKeys();
 connection.close();
 tableHash.selectPartitions(regionKeys);
 LOG.info("Writing " + tableHash.partitions.size() + " partition keys to " + partitionsPath);
 tableHash.writePartitionFile(getConf(), partitionsPath);
}

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

@Override
public void close() throws IOException {
 if (this.connection != null && !this.connection.isClosed()) {
  this.connection.close();
 }
}

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

private boolean isValid(Connection conn) {
 return conn != null
   && !conn.isAborted()
   && !conn.isClosed();
}

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

/**
 * Callers should call close on the returned {@link Table} instance.
 * @param connection connection we're using to access Meta
 * @return An {@link Table} for <code>hbase:meta</code>
 */
public static Table getMetaHTable(final Connection connection)
throws IOException {
 // We used to pass whole CatalogTracker in here, now we just pass in Connection
 if (connection == null) {
  throw new NullPointerException("No connection");
 } else if (connection.isClosed()) {
  throw new IOException("connection is closed");
 }
 return connection.getTable(TableName.META_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

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

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

@Override
 public Object run() throws Exception {
  try (Connection conn = ConnectionFactory.createConnection(conf);
    Table acl = conn.getTable(AccessControlLists.ACL_TABLE_NAME)) {
   BlockingRpcChannel service = acl.coprocessorService(TEST_TABLE.getName());
   AccessControlService.BlockingInterface protocol =
     AccessControlService.newBlockingStub(service);
   AccessControlUtil.getUserPermissions(null, protocol, Bytes.toBytes(namespace1), "dummy");
  }
  return null;
 }
};

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

/**
 * Make sure we can use the cluster
 */
private void testSanity(final String testName) throws Exception {
 String tableName = testName + "_" + System.currentTimeMillis();
 TableDescriptor desc = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName))
   .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam")).build();
 LOG.info("Creating table " + tableName);
 Admin admin = TEST_UTIL.getAdmin();
 try {
  admin.createTable(desc);
 } finally {
  admin.close();
 }
 Table table = TEST_UTIL.getConnection().getTable(desc.getTableName());
 Put put = new Put(Bytes.toBytes("testrow"));
 put.addColumn(Bytes.toBytes("fam"), Bytes.toBytes("col"), Bytes.toBytes("testdata"));
 LOG.info("Putting table " + tableName);
 table.put(put);
 table.close();
}

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

@Test
public void testConnectionRideOverClusterRestart() throws IOException, InterruptedException {
 Configuration config = new Configuration(TEST_UTIL.getConfiguration());
 final TableName tableName = TableName.valueOf(name.getMethodName());
 TEST_UTIL.createTable(tableName, new byte[][] {FAM_NAM}).close();
 Connection connection = ConnectionFactory.createConnection(config);
 Table table = connection.getTable(tableName);
 // this will cache the meta location and table's region location
 table.get(new Get(Bytes.toBytes("foo")));
 // restart HBase
 TEST_UTIL.shutdownMiniHBaseCluster();
 TEST_UTIL.restartHBaseCluster(2);
 // this should be able to discover new locations for meta and table's region
 table.get(new Get(Bytes.toBytes("foo")));
 TEST_UTIL.deleteTable(tableName);
 table.close();
 connection.close();
}

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

@Test
public void testToViolation() throws Exception {
 final TableName tn = TableName.valueOf("inviolation");
 final SpaceQuotaSnapshot snapshot = new SpaceQuotaSnapshot(
   new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 1024L, 512L);
 final Table quotaTable = mock(Table.class);
 when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable);
 final Put expectedPut = new Put(Bytes.toBytes("t." + tn.getNameAsString()));
 final QuotaProtos.SpaceQuotaSnapshot protoQuota = QuotaProtos.SpaceQuotaSnapshot.newBuilder()
   .setQuotaStatus(QuotaProtos.SpaceQuotaStatus.newBuilder().setInViolation(true)
   .setViolationPolicy(QuotaProtos.SpaceViolationPolicy.NO_INSERTS))
   .setQuotaLimit(512L)
   .setQuotaUsage(1024L)
   .build();
 expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("p"), protoQuota.toByteArray());
 notifier.transitionTable(tn, snapshot);
 verify(quotaTable).put(argThat(new SingleCellMutationMatcher<Put>(expectedPut)));
}

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

@Test(expected=RetriesExhaustedException.class)
 public void testSocketClosed() throws IOException, InterruptedException {
  TableName tableName = TableName.valueOf(name.getMethodName());
  UTIL.createTable(tableName, fam1).close();

  Configuration conf = new Configuration(UTIL.getConfiguration());
  conf.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
   MyRpcClientImpl.class.getName());
  conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
  Connection connection = ConnectionFactory.createConnection(conf);
  Table table = connection.getTable(TableName.valueOf(name.getMethodName()));
  table.get(new Get(Bytes.toBytes("asd")));
  connection.close();
  for (Socket socket : MyRpcClientImpl.savedSockets) {
   assertTrue("Socket + " +  socket + " is not closed", socket.isClosed());
  }
 }
}

代码示例来源: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

/**
 * For HADOOP-2579
 * @throws IOException
 */
@Test (expected=TableNotFoundException.class)
public void testTableNotFoundExceptionWithoutAnyTables() throws IOException {
 TableName tableName = TableName
   .valueOf("testTableNotFoundExceptionWithoutAnyTables");
 Table ht = TEST_UTIL.getConnection().getTable(tableName);
 ht.get(new Get(Bytes.toBytes("e")));
}

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

@Override
 public Object run() throws Exception {
  Put p = new Put(TEST_ROW);
  p.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(1));
  try(Connection conn = ConnectionFactory.createConnection(conf);
    Table t = conn.getTable(TEST_TABLE)) {
   t.checkAndMutate(TEST_ROW, TEST_FAMILY).qualifier(TEST_QUALIFIER)
     .ifEquals(Bytes.toBytes("test_value")).thenPut(p);
  }
  return null;
 }
};

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

@Test
public void testEnableDisableAddColumnDeleteColumn() throws Exception {
 final TableName tableName = TableName.valueOf(name.getMethodName());
 TEST_UTIL.createTable(tableName, HConstants.CATALOG_FAMILY).close();
 while (!this.admin.isTableEnabled(TableName.valueOf(name.getMethodName()))) {
  Thread.sleep(10);
 }
 this.admin.disableTable(tableName);
 try {
  TEST_UTIL.getConnection().getTable(tableName);
 } catch (org.apache.hadoop.hbase.DoNotRetryIOException e) {
  //expected
 }
 this.admin.addColumnFamily(tableName, new HColumnDescriptor("col2"));
 this.admin.enableTable(tableName);
 try {
  this.admin.deleteColumnFamily(tableName, Bytes.toBytes("col2"));
 } catch (TableNotDisabledException e) {
  LOG.info(e.toString(), e);
 }
 this.admin.disableTable(tableName);
 this.admin.deleteTable(tableName);
}

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

/**
 * simple test that just executes parts of the client
 * API that accept a pre-created Connection instance
 */
@Test
public void testUnmanagedHConnection() throws IOException {
 final TableName tableName = TableName.valueOf(name.getMethodName());
 TEST_UTIL.createTable(tableName, HConstants.CATALOG_FAMILY);
 Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
 Table t = conn.getTable(tableName);
 Admin admin = conn.getAdmin();
 assertTrue(admin.tableExists(tableName));
 assertTrue(t.get(new Get(ROW)).isEmpty());
 admin.close();
}

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

@Test
public void testRegionObserverSingleRegion() throws IOException {
 final TableName tableName = TableName.valueOf(name.getMethodName());
 try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
    Admin admin = connection.getAdmin()) {
  admin.createTable(
    new HTableDescriptor(tableName)
      .addFamily(new HColumnDescriptor(foo))
      // add the coprocessor for the region
      .addCoprocessor(CustomRegionObserver.class.getName()));
  try (Table table = connection.getTable(tableName)) {
   table.get(new Get(foo));
   table.get(new Get(foo)); // 2 gets
  }
 }
 assertPreGetRequestsCounter(CustomRegionObserver.class);
}

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

@BeforeClass
public static void before() throws Exception {
 HTU.startMiniCluster(NB_SERVERS);
 final TableName tableName = TableName.valueOf(TestRegionServerNoMaster.class.getSimpleName());
 // Create table then get the single region for our new table.
 table = HTU.createTable(tableName,HConstants.CATALOG_FAMILY);
 Put p = new Put(row);
 p.addColumn(HConstants.CATALOG_FAMILY, row, row);
 table.put(p);
 try (RegionLocator locator = HTU.getConnection().getRegionLocator(tableName)) {
  hri = locator.getRegionLocation(row, false).getRegionInfo();
 }
 regionName = hri.getRegionName();
 stopMasterAndAssignMeta(HTU);
}

相关文章