In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to access ApsaraDB for HBase through Thrift, the article is very detailed, has a certain reference value, interested friends must read it!
Thrift multilingual access
Thrift provides the ability to access HBase in multiple languages. The supported language packs from Thrift's official website include: centering, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, Cocoa, JavaScript, Node.js, Smalltalk, OCaml, Delphi and other languages. The main process is that the user thrift Client accesses the thriftserver,thriftserver of HBase through Thrift protocol and forwards the request to the storage service of HBase to read and write data. The approximate architecture diagram is as follows:
The following steps are required to access HBase through thrift in multiple languages:
1. Activate HBase thriftserver service:
Click here to activate thriftserver Servicalization (highly available version thriftserver) on the user's own control page, and you will get an access entry for host:port, or you can choose the self-built thriftserver method of ECS. Refer to here, and finally the ip (host) of the self-built ECS and the default port 9090 are used as the access entry.
2. User Thrift client access:
The common access methods for ordinary customers are the access methods of python and php. Here, we give the access methods of php step by step.
2.1. Visit HBase via php at thrift:
2.1.1. Install the thrift compilation environment
The thrift environment of our cloud HBase is 0.9.0, so it is recommended that customers set up their own thrift environment. You can download the 0.9.0 version of thrift here. We will use the downloaded source package later. You need to install the thrift compiler environment first. For source code installation, please refer to the thrift official website.
You can see the version information of the thrift installation by using the following command
Thrift-version
2.1.2. Generate an access file for thrift to access client
We download the Hbase.thrift file of our cloud HBase from here. Here, our cloud HBase uses the thrift1 protocol. You can refer to the file to see the format. After the download is completed, execute the thrift command to compile.
The compilation command is as follows:
Thrift-gen Hbase.thrift
The above is an abbreviation for language, so the common ones are as follows:
Thrift-gen php Hbase.thriftthrift-gen cpp Hbase.thriftthrift-gen py Hbase.thrift
Execute thrift-- gen php Hbase.thrift will get gen-php in the directory later. This is the function package file we need.
Thrift git: (last_dev) lltotal 56 RWMurray Rafael-1 xuanling.gc staff 24K 3 5 15:06 Hbase.thriftdrwxr-xr-x 3 xuanling.gc staff 96B 8 1 16:03 gen-php
In addition, when we get the source package file of thrift in 2.1.1, we will download it to the Thrift folder under / lib/php/lib under the Thrift source folder, together with gen-php, and drop it under a src directory of our business logic code, plus our own client.php code. The directory results are as follows:
[root@xxxxxxxxxxx thrift_client] # lltotal 12 RW Thrift-1 zookeeper games 2743 Aug 2 11:16 client.phpdrwxr-xr-x 3 zookeeper games 4096 Aug 2 01:22 gen-phpdrwxr-xr-x 12 zookeeper games 4096 Aug 2 01:22 Thrift
2.1.3. Php access code writing
At this time, let's write our client.php code logic. The above Thrift folder and gen-php folder can be named according to your own project and personal style. Here, it is convenient for you to figure out the directory structure and retain the original style. The php code is posted below, and all of our following programs have built a table "new" in HBase:
The result of code execution is as follows:
[root@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) {}
2.2.python access proc
In addition, there are common python customers. For python, there is a third-party library containing thrift in python such as happybase. We have seen some customers use Happybase to access HBase thrift, see the article. In addition, python has rich libraries, and we can install thrift and access HBase's thrift library through pip. The execution process is as follows, assuming that users have installed python and pip:
Pip install thrift / / install the latest default version of thrift pip install hbase-thrift / / install the hbase thrift interface library
After the above two steps are completed, you can write code to access the HBase:
Import sysimport timeimport osfrom thrift import Thriftfrom thrift.transport import TSocket, TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import ttypesfrom hbase.Hbase import Client, ColumnDescriptor, Mutationdef printRow (entry): print "row:" + entry.row + ", cols:", for k in sorted (entry.columns): print k + "= >" + entry.columns [k] .value, printtransport = 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 of the above program execution are as follows:
[root@Test ~] # python Hbase_client.py---list table-- ['new']-write data-get data----row: key, cols: info:cn1 = > value---delete data-end
3. Visit HBase thriftserver
3.1. Access the whitelist of the machine
Add the ip of the accessed machine to the whitelist of the HBase cluster, and then the code can be executed normally
The above is all the contents of the article "how to access ApsaraDB for HBase through Thrift". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.