Teach you to access apsaradb for HBase via thrift

Source: Internet
Author: User
Tags zookeeper

Summary: Teach you to use thrift to access APSARADB for HBase

Thrift Multi-language access
? Thrift provides multi-lingual access to hbase, supported language packs from the Thrift website: C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, Node . js, Smalltalk, OCaml, Delphi and other languages. The main process is the thriftserver that the user thrift Client accesses hbase via thrift protocol, Thriftserver makes a request to the storage service of HBase to do the reading and writing of the data. The approximate schema diagram is as follows:

? The following steps are required to access hbase through thrift Multi-language:

First, the opening of HBase Thriftserver services:
? In the user's own control page Click here to refer to open Thriftserver Service (high-availability version thriftserver), will get a host:port access to the portal, or you can choose the ECS self-built Thriftserver method, refer to Here, Finally, the IP (host) of the self-built ECS and the default port of 9090 as the Access portal.

Second, the User Thrift Client access:
? General customer access is a common way of access to Python and the way PHP access, here we first step by step to give the way PHP access;

2.1. Go thrift with PHP to access hbase:
? 2.1.1. Install thrift compilation Environment;

? Our Cloud HBase thrift environment is 0.9.0, so it is recommended that customers build their own thrift environment is also 0.9.0, here you can download thrift 0.9.0 version, download the source package we will use later, here need to install the Thrift compilation environment, For source installation can refer to Thrift official website;

The version information of installation thrift can be seen by the following command.

Thrift--version
? 2.1.2. Generate access files for thrift access client;

? We download from here our cloud HBase Hbase.thrift file, here we cloud hbase use is the THRIFT1 protocol, specifically can refer to the file to see the use of format, download completed after the execution of Thrift command to compile;

? The compile command is as follows:

Thrift--gen <language> Hbase.thrift
? The above is the abbreviation of the language, then the common is as follows:

Thrift--gen PHP Hbase.thrift
Thrift--gen CPP Hbase.thrift
Thrift--gen PY Hbase.thrift
? Execute Thrift--gen PHP hbase.thrift will be in the directory after the gen-php this is the function package file we need;

Thrift git: (last_dev) ll
Total 56
-rw-r--r--1 XUANLING.GC Staff 24K 3 5 15:06 Hbase.thrift
Drwxr-xr-x 3 XUANLING.GC Staff 96B 8 1 16:03 gen-php
? In addition we get thrift in 2.1.1 Source package files will be downloaded to the Thrift source folder under the/lib/php/lib Thrift folder and gen-php together in our business logic code under a src directory, Together with our own client.php code, the catalog results are as follows:

[email protected] thrift_client]# LL
Total 12
-rw-r--r--1 Zookeeper Games 2743 2 11:16 client.php
Drwxr-xr-x 3 Zookeeper Games 4096 2 01:22 gen-php
Drwxr-xr-x Zookeeper Games 4096 2 01:22 Thrift
? 2.1.3. PHP access code writing;

? This time, We write our client.php code logic, the above Thrift folder and Gen-php folder, can be named with their own project and personal style, here to make it easy to understand the directory structure, to preserve the original style; The following PHP code is posted, all of our programs are in HBase A table "new" was built:

<?php
Ini_set (' display_errors ', e_all);
$GLOBALS [' thrift_root '] = "/root/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); Send Timeout, per millisecond
$socket->setrecvtimeout (20000); Receive timeout, per millisecond
$transport = new Tbufferedtransport ($socket);
$protocol = new Tbinaryprotocol ($transport);
$client = new Hbaseclient ($protocol);

$transport->open ();

# # # #列出表 # #
echo "----list tables----\ n";
$tables = $client->gettablenames ();
foreach ($tables as $name) {
Var_dump ($tables);
}

$tablename = ' new ';
# # # #写数据 # #
echo "----Write data----\ n";
$row = ' key ';
$value = ' value ';
$atrribute = Array ();
$mutations = Array (
New Mutation (Array (
' Column ' = ' info:cn1 ',
' Value ' = $value
)),
);

try {
$client->mutaterow ($tablename, $row, $mutations, $atrribute);
} catch (Exception $e) {
Var_dump ($e);//play log here by yourself
}

# # #读数据 # # #
echo "---read data---\ n";
$result = $client->getrow ($tablename, $row, $atrribute);
Var_dump ($result);

# # #删数据 # # #
echo "---delete data---\ n";
$client->deleteallrow ($tablename, $row, $atrribute);
echo "---get data---\ n";
$result = $client->getrow ($tablename, $row, $atrribute);
Var_dump ($result);
?>
? The code execution results are as follows:

[[email protected] 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) {
}
2.2.python access process;
? There are also common Python customers, for Python, there is happybase this Python third party contains Thrift's library to do, we have seen some customers use Happybase to access HBase Thrift, see the article; Python has a rich library, we can install thrift through PIP, and access the thrift Library of HBase, the execution process is as follows, assuming that the user has installed Python and Pip:

PIP Install Thrift//Install thrift default Latest version
Pip Install Hbase-thrift//install HBase Thrift Interface Library
? After completing the 2 steps above, you can write code that accesses HBase:

Import Sys
Import time
Import OS

From Thrift Import Thrift
From Thrift.transport import Tsocket, Ttransport
From Thrift.protocol import Tbinaryprotocol
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.tbinaryprotocol (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 results corresponding to the above program execution are as follows:

[email protected] ~]# python hbase_client.py
---list table--
[' New ']
---write Data---
---get data----
Row:key, cols:info:cn1 = value
---delete data---
---end----
Iii. access to HBase thriftserver
? 3.1, access to the machine to open the white list

? The IP of the machine to be accessed is added to the white list of hbase clusters, and then the code can be executed normally;

Please add a link to the original link description

This article is the original content of the cloud-Habitat community and cannot be reproduced without permission.

Teach you to access apsaradb for HBase via thrift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.