Updated on 2024-04-02 GMT+08:00

Accessing the ThriftServer Operation Table

Function

After importing the host where the ThriftServer instances are located and the port that provides services, you can create a Thrift client using the authentication credential and configuration file, access ThriftServer, and obtain table names, create a table, and delete a table based on the specified namespace.

Example Code

  • Invoking methods
    // Get table of specified namespace. getTableNamesByNamespace(client, "default");
    // Create table. createTable(client, TABLE_NAME);
    // Delete specified table.
     deleteTable(client, TABLE_NAME);
  • Obtains table names based on the specified namespace.

    The following code snippets are in the getTableNamesByNamespace method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void getTableNamesByNamespace(THBaseService.Iface client, String namespace) throws TException {
         client.getTableNamesByNamespace(namespace)
             .forEach(
                 tTableName -> LOGGER.info("{}", TableName.valueOf(tTableName.getNs(), tTableName.getQualifier())));
     }
  • Creating a table

    The following code snippets are in the createTable method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void createTable(THBaseService.Iface client, String tableName) throws TException, IOException {
         TTableName table = getTableName(tableName);
         TTableDescriptor descriptor = new TTableDescriptor(table);
         descriptor.setColumns(
             Collections.singletonList(new TColumnFamilyDescriptor().setName(COLUMN_FAMILY.getBytes())));
         if (client.tableExists(table)) {
             LOGGER.warn("Table {} is exists, delete it.", tableName);
             client.disableTable(table);
             client.deleteTable(table);
         }
         client.createTable(descriptor, null);
         if (client.tableExists(table)) {
             LOGGER.info("Created {}.", tableName);
         } else {
             LOGGER.error("Create {} failed.", tableName);
         }
     }
  • Deleting a table

    The following code snippets are in the deleteTable method in the ThriftSample class of the hbase-thrift-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void deleteTable(THBaseService.Iface client, String tableName) throws TException, IOException {
         TTableName table = getTableName(tableName);
         if (client.tableExists(table)) {
             client.disableTable(table);
             client.deleteTable(table);
             LOGGER.info("Deleted {}.", tableName);
         } else {
             LOGGER.warn("{} not exist.", tableName);
         }
     }