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

Accessing the ThriftServer Operation Table

Scenario

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.

Procedure

  1. Log in to FusionInsight Manager, choose Cluster > Service > HBase > Configuration and click All Configurations, search for and modify the parameter hbase.thrift.security.qop of the ThriftServer instance. The value of this parameter must be the same as that of hbase.rpc.protection. Save the configuration and restart the node service for the configuration to take effect.

    The mapping between hbase.rpc.protection and hbase.thrift.security.qop is as follows:

    • "privacy" - "auth-conf"
    • "authentication" - "auth"
    • "integrity" - "auth-int"

  2. Obtain the configuration file of the ThriftServer instance in the cluster.

    • Method 1: Choose Cluster > Service > HBase > Instance, click the ThriftServer instance to go to the details page, and obtain the configuration files hdfs-site.xml, core-site.xml, and hbase-site.xml.
    • Method 2: Obtain the configuration files by decompressing the client file in Preparing for Development and Operating Environment. Manually add the following configuration to hbase-site.xml. The value of hbase.thrift.security.qop must be the same as that in 1.
      <property>
      <name>hbase.thrift.security.qop</name>
      <value>auth</value>
      </property>
      <property>
      <name>hbase.thrift.kerberos.principal</name>
      <value>thrift/hadoop.hadoop.com@HADOOP.COM</value>
      </property>
      <property>
      <name>hbase.thrift.keytab.file</name>
      <value>/opt/huawei/Bigdata/FusionInsight_HD_8.1.2.2/install/FusionInsight-HBase-2.2.3/keytabs/HBase/thrift.keytab</value>
      </property>

Example Code

  • Initializing configuration
    The following code snippets belong to the TestMain class in the com.huawei.bigdata.hbase.examples package of the hbase-thrift-example sample project.
        private static void init() throws IOException {
            // Default load from conf directory
            conf = HBaseConfiguration.create();
    
            String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;[1]
            //In Linux environment
            //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
            conf.addResource(new Path(userdir + "core-site.xml"), false);
            conf.addResource(new Path(userdir + "hdfs-site.xml"), false);
            conf.addResource(new Path(userdir + "hbase-site.xml"), false);
    }

    [1] userdir obtains the conf directory in the resource path after compilation. The core-site.xml, hdfs-site.xml, and hbase-site.xml files used for initial configuration must be stored in the src/main/resources/conf directory.

  • Connecting to a ThriftServer instance
    The following code snippets belong to the TestMain class in the com.huawei.bigdata.hbase.examples package of the hbase-thrift-example sample project.
        try {    
            test = new ThriftSample();    
            test.test("100.120.16.170", THRIFT_PORT, conf);[2]
        } catch (TException | IOException e) {
            LOG.error("Test thrift error", e);
        }

    [2] The value of the input parameter test.test() is the IP address of the node where the ThriftServer instance to be accessed is located. Change the IP address to the actual one. The IP address of the node must be configured in the hosts file of the local host where the sample code is run.

    THRIFT_PORT is the value of hbase.regionserver.thrift.port configured for the ThriftServer instance.

  • Invoking methods
    // Get table of specified namespace. 
    getTableNamesByNamespace(client, "default");
    // Create table. 
    createTable(client, TABLE_NAME);
    // Delete specified table.
     deleteTable(client, TABLE_NAME);
  • Obtaining 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);
         }
     }