All Products
Search
Document Center

:Use the multi-language API to access ApsaraDB for HBase Standard Edition clusters

Last Updated:Mar 19, 2024

You can use Thrift to enable multi-language access to ApsaraDB for HBase Standard Edition clusters. Thrift is a service component in ApsaraDB for HBase Standard Edition and is developed based on Apache Thrift. Apache Thrift is a software framework for scalable cross-language services development. This topic describes how to use Thrift to enable multi-language access to ApsaraDB for HBase Standard Edition clusters.

Prerequisites

An ApsaraDB for HBase Standard Edition cluster is created. For more information, see Purchase a cluster.

Access process

Your request is sent to Thrift. Thrift accesses an ApsaraDB for HBase Standard Edition cluster by using the Java API.

Obtain Thrift connection information

  1. Log on to the ApsaraDB for HBase console.

  2. In the top navigation bar, select the region where the cluster that you want to manage is deployed.

  3. On the Clusters page, find the cluster and click the ID of the cluster.

  4. In the left-side navigation pane, click Database Connection.

  5. In the Connection Information section, view the value of the address that Thrift uses to access ApsaraDB for HBase.

    Note

    The endpoint of HBase Thrift is an internal endpoint instead of a public endpoint.

Access an ApsaraDB for HBase Standard Edition cluster by using PHP

  1. Download the Thrift installation package.

    Note

    ApsaraDB for HBase Standard Edition supports Apache Thrift 0.9.0. We recommend that you use Apache Thrift 0.9.0.

  2. Download the Thrift1 definition file for ApsaraDB for HBase. ApsaraDB for HBase uses the Thrift1 protocol.

  3. Generate an interface definition file for the specified programming language.

    1. Decompress the thrift-0.9.0.tar.gz package.

    2. In the command line interface (CLI), open the extracted thrift-0.9.0.tar.gz file and run the following command to generate an interface definition file:

      thrift --gen php Hbase.thrift
      Note

      To easily view files, you can place the extracted Thrift file and the gen-php interface definition file in the src directory. The following code shows the directory structure:

      [testuser@xxxxxxxxxxx thrift_client]# ll
      total 12
      -rw-r--r--  1 zookeeper games 2743 Aug  2 11:16 client.php
      drwxr-xr-x  3 zookeeper games 4096 Aug  2 01:22 gen-php
      drwxr-xr-x 12 zookeeper games 4096 Aug  2 01:22 Thrift
  4. Use the following PHP code to create a table named new in an ApsaraDB for HBase Standard Edition cluster.

    <?php
    ini_set('display_errors', E_ALL);
    $GLOBALS['THRIFT_ROOT'] = "/home/thrift_client";
    /* Dependencies. In the proper order. */
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TTransport.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TSocket.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TProtocol.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TBinaryProtocol.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TBinaryProtocolAccelerated.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TBufferedTransport.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Type/TMessageType.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Factory/TStringFuncFactory.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/StringFunc/TStringFunc.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/StringFunc/Core.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Type/TType.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TException.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TTransportException.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TProtocolException.php';
    
    require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Types.php';
    require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Hbase.php';
    
    use Thrift\Protocol\TBinaryProtocol;
    use Thrift\Transport\TBufferedTransport;
    use Thrift\Transport\TSocket;
    use Hbase\HbaseClient;
    use Hbase\ColumnDescriptor;
    use Hbase\Mutation;
    
    $host='hb-bp12pt6alr1788y35-001.hbase.rds.aliyuncs.com';
    $port=9099;
    
    $socket = new TSocket($host, $port);
    
    $socket->setSendTimeout(10000); // The timeout period for sending a request. Unit: milliseconds.
    $socket->setRecvTimeout(20000); // The timeout period for receiving a response. Unit: milliseconds.
    $transport = new TBufferedTransport($socket);
    $protocol = new TBinaryProtocol($transport);
    $client = new HbaseClient($protocol);
    
    $transport->open();
    
    ####Query tables####
    echo "----list tables----\n";
    $tables = $client->getTableNames();
    foreach ($tables as $name) {
        var_dump($tables);
    }
    
    $tablename='new';
    ####Write data####
    echo "----write data----\n";
    $row = 'key';
    $value = 'value';
    $attribute = array();
    $mutations = array(
        new Mutation(array(
            'column' => 'info:cn1',
            'value' => $value
        )),
    );
    
    try {
        $client->mutateRow($tablename, $row, $mutations, $attribute);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    ###Query data####
    echo "---read data---\n";
    $result = $client->getRow($tablename, $row, $attribute);
    var_dump($result);
    
    ###Delete data####
    echo "---delete data---\n";
    $client->deleteAllRow($tablename, $row, $attribute);
    echo "---get data---\n";
    $result = $client->getRow($tablename, $row, $attribute);
    var_dump($result);
    
    ###Scan data###
    $row = 'ID1';
    $value = 'v1';
    $mutations = array(
       new Mutation(array(
            'column' => 'info:c1',
            'value' => $value
        )),
    );
    try {
        $client->mutateRow($tablename, $row, $mutations, $attribute);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    $row = 'ID6';
    $value = 'v2';
    $mutations = array(
       new Mutation(array(
            'column' => 'info:c1',
            'value' => $value
        )),
    );
    try {
        $client->mutateRow($tablename, $row, $mutations, $attribute);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    $row = 'ID3';
    $value = 'v3';
    $mutations = array(
       new Mutation(array(
            'column' => 'info:c1',
            'value' => $value
        )),
    );
    try {
        $client->mutateRow($tablename, $row, $mutations, $attribute);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    echo 'prefix scan';
    $scan = $client->scannerOpenWithPrefix($tablename, 'ID', null, null);
    
    $nbRows = 100;
    $arr = $client->scannerGetList($scan, $nbRows);
    echo 'count of result :'.count($arr)."\n";
    var_dump($arr);
    foreach ($arr as $k => $TRowResult) {
       echo "\trow:$TRowResult->row\tcolumns(array):";
       foreach ($TRowResult->columns as $key => $value) {
          echo "key:$key\tvalue:$value->value\ttimestamp:$value->timestamp\n";
       }
    }
    
    echo 'range scan';
    $scan = $client->scannerOpenWithStop($tablename, 'ID0', 'ID6', null, null);
    
    $nbRows = 100;
    $arr = $client->scannerGetList($scan, $nbRows);
    echo 'count of result :'.count($arr)."\n";
    var_dump($arr);
    foreach ($arr as $k => $TRowResult) {
       echo "\trow:$TRowResult->row\tcolumns(array):";
       foreach ($TRowResult->columns as $key => $value) {
          echo "key:$key\tvalue:$value->value\ttimestamp:$value->timestamp\n";
       }
    }
    
    ###Insert a row###
    echo "do increment on a new row";
    $row = 'ID4';
    try {
    $newCount = $client->atomicIncrement($tablename, $row, 'info:c1', 1234);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    echo "new count $newCount\n";
    
    ###Read or write data of the LONG type from or to ApsaraDB for HBase###
    
    $row = 'ID5';
    $value = pack("J", 4567);
    
    $mutations = array(
       new Mutation(array(
            'column' => 'info:c1',
            'value' => $value
        )),
    );
    try {
        $client->mutateRow($tablename, $row, $mutations, null);
    } catch (Exception $e) {
        var_dump($e);
    }
    
    echo "---read data and print it as long ---\n";
    $result = $client->getRow($tablename, $row, null);
    
    foreach ($result[0]->columns as $key => $value) {
      $count = unpack("J*mycount", $value->value);
      var_dump($count);
    }
    
    ?>

    The following results are returned:

    [testuser@xxxxxxxxxxx thrift_client]# php client.php
    ----list tables----
    array(1) {
      [0]=>
      string(3) "new"
    }
    ----write data----
    ---read data---
    array(1) {
      [0]=>
      object(Hbase\TRowResult)#8 (3) {
        ["row"]=>
        string(3) "key"
        ["columns"]=>
        array(1) {
          ["info:cn1"]=>
          object(Hbase\TCell)#10 (2) {
            ["value"]=>
            string(5) "value"
            ["timestamp"]=>
            int(1533179795969)
          }
        }
        ["sortedColumns"]=>
        NULL
      }
    }
    ---delete data---
    ---get data---
    array(0) {
    }

Access an ApsaraDB for HBase Standard Edition cluster by using Python

If you have installed Python and pip, you can use pip to install Thrift and access an ApsaraDB for HBase Standard Edition cluster.

Note

The sample code provided in the following steps is based on Python 2.

  1. Use the following code to install Thrift.

    pip install thrift
    pip install hbase-thrift
  2. Access an ApsaraDB for HBase Standard Edition cluster.

    import sys
    import time
    import os
    
    from thrift import Thrift
    from thrift.transport import TSocket, TTransport
    from thrift.protocol import TBinaryProtocol
    from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated
    from hbase import ttypes
    from hbase.Hbase import Client, ColumnDescriptor, Mutation
    
    def printRow(entry):
      print "row: " + entry.row + ", cols:",
      for k in sorted(entry.columns):
        print k + " => " + entry.columns[k].value,
      print
    
    
    transport = TSocket.TSocket('hb-bp12pt6alr1788y35-001.hbase.rds.aliyuncs.com', 9099)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
    client = Client(protocol)
    transport.open()
    
    print "---list table--"
    print client.getTableNames()
    
    table="new"
    row="key"
    
    print "---write data---"
    mutations = [Mutation(column="info:cn1", value="value")]
    client.mutateRow(table, row, mutations, {})
    
    print "---get data----"
    printRow(client.getRow(table, row, {})[0])
    
    print "---delete data---"
    client.deleteAllRow(table, row, {})
    print "---end----"
    
    transport.close()

    The following results are returned:

    [testuser@Test ~]# python Hbase_client.py
    ---list table--
    ['new']
    ---write data---
    ---get data----
    row: key, cols: info:cn1 => value
    ---delete data---
    ---end----

Access an ApsaraDB for HBase Standard Edition cluster by using Go

  1. Download the Thrift installation package.

    wget https://public-hbase.oss-cn-hangzhou.aliyuncs.com/thrift/goh.tar.gz
  2. Decompress the goh.tar.gz installation package to the src directory.

    tar -xvzf goh.tar.gz
    mv github.com $GOPATH/src
  3. For more information about how to use Go to access an ApsaraDB for HBase Standard Edition cluster, see Sample code.