PHP uses thrift to operate hbase

Source: Internet
Author: User
Tags hadoop mapreduce
Document directory
  • 1. Download thrift
  • 2. Extract

HBase is an open-source NoSQL product that implements an open-source product for Google BigTable paper. Together with Hadoop and HDFS, HBase can be used to store and process massive column family data. Official Website: http://hbase.apache.org

1. HBase access interface

1. Native Java API, the most common and efficient access method, suitable for Hadoop MapReduce Job concurrent batch processing of HBase table data
2. HBase Shell, HBase command line tool, and the simplest interface are suitable for HBase management.
3. Thrift gateway uses thrift serialization technology and supports multiple languages such as C ++, PHP, and python. It is suitable for other heterogeneous systems to access hbase table data online.
4. REST Gateway supports restful Http APIs to access HBase, removing language restrictions
5. Pig can use Pig Latin stream programming language to operate HBase data. Similar to Hive, Pig Latin is essentially compiled into MapReduce Job to process HBase table data. It is suitable for data statistics.
6. Hive, the current Hive Release version has not yet added support for HBase, but HBase will be supported in the next version Hive 0.7.0, and HBase can be accessed using similar SQL languages
If you use PHP to operate Hbase, it is recommended to use thrift open-source Facebook, the official website is: http://thrift.apache.org/, it is a middleware similar to ice, for information exchange between different system languages.
Ii. Install Thrift

Install Thrift on clusters installed in both Hadoop and Hbase, and install Thrift on Hmaster machines.

1. Download thrift

Wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz

2. Extract

Tar-xzf thrift-0.8.0.tar.gz

3. Compile and install:

If the source code is compiled, first use./boostrap. sh to create the file./configure. The downloaded tar package comes with the configure file. (Read the README file ))

If you are building from the first time out of the source repository, you will
Need to generate the configure scripts. (This is not necessary if you
Downloaded a tarball.) From the top directory, do:
./Bootstrap. sh

./Configure
Make; make install

4. Start:

#./Bin/hbase-daemon.sh start thrift [-- port = PORT]
Starting thrift, logging to/home/banping/hbase/hbase-0.90.3/bin/../logs/hbase-root-thrift-localhost.localdomain.out

The default listening port of thrift is 9090.

Use jps to view the process and view the ThriftServer process:

Iii. Test: 1. Operate Hbase in the php script Library

 

PHP accesses Hbase through Thrift in the thrift-0.8.0/lib/php/src directory. In fact, this folder also contains PHP extension source code for accessing Hbase through Thrift.

1) Copy thrift-0.8.0/lib/php to the corresponding php web directory.

2) then generate the php and hbase interface files

#/Usr/local/thrift/bin/thrift -- gen php/usr/local/hbase/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase. thrift # (based on your own directory settings) generate a directory file:/usr/local/hbase/gen-php/Hbase with a file: Hbase. php, Hbase_types.php

Copy Hbase. php and Hbase_types.php to the: web directory/php/src/packages/Hbase/

3) Use a php script to test:

<? Phpini_set ('display _ errors ', E_ALL); $ GLOBALS ['thrift _ root'] = '. /php/src '; require_once ($ GLOBALS ['thrift _ root']. '/Thrift. php '); require_once ($ GLOBALS ['thrift _ root']. '/transport/TSocket. php '); require_once ($ GLOBALS ['thrift _ root']. '/transport/TBufferedTransport. php '); require_once ($ GLOBALS ['thrift _ root']. '/protocol/TBinaryProtocol. php '); require_once ($ GLOBALS ['thrift _ root']. '/packages/H Base/Hbase. php '); $ socket = new TSocket ('10. 64.60.83 ', '123'); $ socket-> setSendTimeout (9090); // Ten seconds (too long for production, but this is just a demo $ socket-> setRecvTimeout (20000); // Twenty seconds $ transport = new TBufferedTransport ($ socket); $ protocol = new TBinaryProtocol ($ transport ); $ client = new HbaseClient ($ protocol); $ transport-> open (); // get the table list $ tables = $ client-> getTableNames (); sort ($ Tables); foreach ($ tables as $ name) {echo ("found: {$ name} \ n ");} // create a new table student $ columns = array (new ColumnDescriptor (array ('name' => 'id: ', 'maxversion' => 10 )), new ColumnDescriptor (array ('name' => 'name: '), new ColumnDescriptor (array ('name' => 'score :')),); $ tableName = "student"; try {$ client-> createTable ($ tableName, $ columns);} catch (AlreadyExists $ AE) {echo ("WARN: {$ AE-> message} \ N ") ;}// get the table description $ descriptors = $ client-> getColumnDescriptors ($ tableName); asort ($ descriptors); foreach ($ descriptors as $ col) {echo ("column :{$ col-> name}, maxVer: {$ col-> maxVersions} \ n ");} // modify the table column data $ row = '2'; $ valid = "foobar-\ xE7 \ x94 \ x9F \ xE3 \ x83 \ x93 "; $ mutations = array (new Mutation (array ('column '=> 'score', 'value' => $ valid),); $ client-> mutateRow ($ tableName, $ row, $ mutations); // get table column data $ row_n Ame = '2'; $ fam_col_name = 'score '; $ arr = $ client-> get ($ tableName, $ row_name, $ fam_col_name ); // $ arr = arrayforeach ($ arr as $ k => $ v) {// $ k = TCell echo ("value = {$ v-> value }, <br> "); echo (" timestamp = {$ v-> timestamp} <br> ") ;}$ arr = $ client-> getRow ($ tableName, $ row_name); // $ client-> getRow return a arrayforeach ($ arr as $ k => $ TRowResult) {// $ k = 0; non-use // $ TRowResult = TRowResult var_dum P ($ TRowResult) ;}$ transport-> close () ;?>

View all tables in the project in a browser, which proves that PHP can access HBase through thrift.

2. Use thrift with PHP Extension

We use PHP phpize to generate Thtift php extensions. Source code structure of the extension:

Hadoop @ ubuntu: // usr/local/hbase-0.90.4/thrift-0.8.0/lib/php/src
$ Cd ext/thrift_protocol
$/Usr/local/php/bin/phpize
$./Configure -- with-php-config =/usr/local/php/bin/php-config -- enable-thrift_protocol
$ Make
$ Make install

Configure the generated thrift_protocol.so file to PhP. ini and restart the apache service.

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.