PHP operates HBase via thrift

Source: Internet
Author: User
Tags fam vars hadoop mapreduce

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?
  1. <?php
  2. Ini_set (' display_errors ', e_all);
  3. $GLOBALS [' thrift_root '] = './php/src ';
  4. Require_once ( $GLOBALS [' Thrift_root '].  '/thrift.php ');
  5. Require_once ( $GLOBALS [' Thrift_root '].  '/transport/tsocket.php ');
  6. Require_once ( $GLOBALS [' Thrift_root '].  '/transport/tbufferedtransport.php ');
  7. Require_once ( $GLOBALS [' Thrift_root '].  '/protocol/tbinaryprotocol.php ');
  8. Require_once ( $GLOBALS [' Thrift_root '].  '/packages/hbase/hbase.php ');
  9. $socket = New Tsocket (' 10.64.60.83 ', ' 9090 ');
  10. $socket->setsendtimeout (10000); //Ten seconds (too long for production, but this is just a demo;)
  11. $socket->setrecvtimeout (20000); //Twenty seconds
  12. $transport = new Tbufferedtransport ($socket);
  13. $protocol = new Tbinaryprotocol ($transport);
  14. $client = new Hbaseclient ($protocol);
  15. $transport->open ();
  16. Get table List
  17. $tables = $client->gettablenames ();
  18. Sort ($tables);
  19. foreach ($tables as $name) {
  20. Echo ( "found: {$name}\n");
  21. }
  22. Create a new table student
  23. $columns = Array (
  24. New Columndescriptor (array (
  25. ' name ' = ' ID: ',
  26. ' maxversions ' =
  27. )),
  28. New Columndescriptor (array (
  29. ' name ' = ' = ' name: '
  30. )),
  31. New Columndescriptor (array (
  32. ' name ' = ' score: '
  33. )),
  34. );
  35. $tableName = "Student";
  36. try {
  37. $client->createtable ($tableName, $columns);
  38. } catch (Alreadyexists $ae) {
  39. Echo ( "WARN: {$ae->message}\n");
  40. }
  41. Get a description of a table
  42. $descriptors = $client->getcolumndescriptors ($tableName);
  43. Asort ($descriptors);
  44. foreach ($descriptors as $col) {
  45. Echo ( "column: {$col->name}, MaxVer: {$col->maxversions}\n");
  46. }
  47. modifying data for a table column
  48. $row = ' 2 ';
  49. $valid = "foobar-\xe7\x94\x9f\xe3\x83\x93";
  50. $mutations = Array (
  51. New Mutation (array (
  52. ' column ' = ' score ',
  53. ' value ' = $valid
  54. )),
  55. );
  56. $client->mutaterow ($tableName, $row, $mutations);
  57. Get data for a table column
  58. $row _name = ' 2 ';
  59. $fam _col_name = ' score ';
  60. $arr = $client->get ($tableName, $row _name, $fam _col_name);
  61. $arr = array
  62. foreach ($arr as $k = = $v) {
  63. $k = Tcell
  64. Echo ("value = {$v->value}, <br>");
  65. Echo ("timestamp = {$v->timestamp} <br>");
  66. }
  67. $arr = $client->getrow ($tableName, $row _name);
  68. $client->getrow return a array
  69. foreach ($arr as $k = = $TRowResult) {
  70. $k = 0; Non-use
  71. $TRowResult = Trowresult
  72. Var_dump ($TRowResult);
  73. }
  74. $transport->close ();
  75. ?>

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

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.