PHP operates HBase via thrift
HBase is an open source NoSQL product that is an open source product that implements the Google BigTable paper, which, together with Hadoop and HDFs, can be used to store and process massive column family data. The official website is: http://hbase.apache.org
One, HBase access interface 1. Native Java API, the most routine and efficient way to access, is suitable for hadoop MapReduce job parallel batching hbase table data
2. HBase shell,hbase command-line tool, the simplest interface for hbase management use
3. Thrift Gateway, using Thrift serialization technology to support multiple languages such as C++,php,python, to access hbase table data online for other heterogeneous systems
4. Rest Gateway, which supports the rest-style HTTP API to access HBase, lifting language restrictions
5. Pig, you can use the Pig Latin streaming programming language to manipulate data in HBase, similar to hive, and ultimately compiled into a mapreduce job to process hbase table data for data statistics
6. Hive, the release version of the current hive is not yet joined to HBase support, but HBase will be supported in the next version of Hive 0.7.0, and can be accessed using SQL-like language to access HBase
If you use PHP to manipulate hbase, we recommend using Facebook's Open source thrift, the official website: http://thrift.apache.org/, an ice-like middleware for exchanging information between different system languages.
Second, installation thrift
Install Thrift,thrift on Hmaster machines on clusters that are already installed in Hadoop and HBase
1. Download Thrift
wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz
2. Unzip
Tar-xzf thrift-0.8.0.tar.gz
3. Compile and install:
If the source code is compiled, the first thing to do is to use the./boostrap.sh to create the file./configure, we downloaded the TAR package from the Configure file. (Can read the Readme file))
If you is building from the first time out of the source repository, you'll
Need to generate the Configure scripts. (This isn't 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
Thrift The default listener port is 9090
Using JPS to view the process, see the Thriftserver process:
Iii. test: 1. PHP Script Library Operations HBase
PHP accesses HBase's libraries via thrift in the THRIFT-0.8.0/LIB/PHP/SRC directory, which in fact contains the PHP extension source code that accesses hbase via thrift.
1) Copy the thrift-0.8.0/lib/php to the appropriate 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 # (according to your own directory settings) to generate the catalog file:/usr/local/hbase/gen-php/hbase file: hbase.php,hbase_types.php
Copy hbase.php,hbase_types.php to: Web directory/php/src/packages/hbase/
3) Test with PHP script:
[PHP]View PlainCopy print?
- <?php
- Ini_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/hbase/hbase.php ');
- $socket = New Tsocket (' 10.64.60.83 ', ' 9090 ');
- $socket->setsendtimeout (10000); //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 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: ',
- ' maxversions ' =
- )),
- 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 a description of a table
- $descriptors = $client->getcolumndescriptors ($tableName);
- Asort ($descriptors);
- foreach ($descriptors as $col) {
- Echo ( "column: {$col->name}, MaxVer: {$col->maxversions}\n");
- }
- modifying data for a table column
- $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 data for a table column
- $row _name = ' 2 ';
- $fam _col_name = ' score ';
- $arr = $client->get ($tableName, $row _name, $fam _col_name);
- $arr = array
- foreach ($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 array
- foreach ($arr as $k = = $TRowResult) {
- $k = 0; Non-use
- $TRowResult = Trowresult
- Var_dump ($TRowResult);
- }
- $transport->close ();
- ?>
See all the tables in the project through the browser and prove that PHP can access hbase through thrift.
2. Using PHP extensions to use thrift
We use PHP's own phpize to generate thtift PHP extensions. The source code structure of the extension:
[Email protected]:/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
Then configure the generated thrift_protocol.so file to php.ini and restart the Apache service.
PHP operates HBase via thrift