PHP通过thrift2访问HBASE

前一段时间需要在网页上显示HBASE查询的结果,考虑用PHP来实现,在网上搜了一下,普遍都是用thrift作为接口来实现的。​

参考博文:​

http://www.cnblogs.com/scotoma/archive/2013/05/16/3081236.html

用上述网址里提供的PHP代码,可以访问公司里的一个HBASE集群,但是另一个集群怎么也访问不了,上网查了一下,发现thrift有两套HBASE的接口--thrift和thrift2,而且两套接口并不兼容。

用thrift2的接口替换了上述网址里的thrift接口,另外一个HBASE集群就也可以访问了。

thrift2接口PHP文件

http://yunpan.cn/cwSDFaSPuWYIN  访问密码 fd38

http://yunpan.cn/cwSUnCGrsiwyy  访问密码 c5b8

其中包含两个文件​:Hbase_types.php和THBaseService.php,

Hbase_types.php中定义了接口中使用的变量的类型

THBaseService.php中则定义了各种访问HBASE的接口

 

thrift和thrift2接口的差异较大,可参见以下博文

http://blog.csdn.net/guxch/article/details/12163047

 

遗憾的是上述博文并没有使用thrift2接口的具体例子,导致我在使用这些接口的时候也碰了不少钉子。

下面列出一些thrift2访问HBASE的PHP代码,以供参考,不妥的地方敬请指正:

 1 <?php
 2 /***
 3 Thrift Test
 4 
 5 */
 6 
 7 ini_set('display_error', E_ALL);
 8 
 9 $GLOBALS['THRIFT_ROOT'] = './lib/php/src';
10 
11 /* Dependencies. In the proper order. */
12 require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
13 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
14 require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
15 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
16 require_once $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/THBaseService.php'; 
17 
18 //define host and port
19 $host = 'xxx.xxx.xxx.xxx';
20 $port = 9090;
21 
22 $socket = new TSocket($host, $port);
23 $transport = new TBufferedTransport($socket);
24 $protocol = new TBinaryProtocol($transport);
25 
26 // Create a client
27 $client = new THBaseServiceClient($protocol);
28 $transport->open();
29 
30 //HOW TO GET
31 $tableName = "test_table";
32 
33 $column_1 = new TColumn();
34 $column_1->family = 'cf1';
35 $column_1->qualifier = 'q1';
36 
37 $column_2 = new TColumn();
38 $column_2->family = 'cf1';
39 $column_2->qualifier = 'q2';
40 
41 $columnArray = array($column_1, $column_2);
42 
43 $get = new TGet();
44 $get->row = 'a';
45 $get->columns = $columnArray;
46 
47 $arr = $client->get($tableName, $get);
48 
49 $results = $arr->columnValues;
50 foreach($results as $result)
51 {
52     $qualifier = (string)$result->qualifier;
53     $value = $result->value;
54     print_r($qualifier);
55     print_r($value);
56 }
57 
58 //HOW TO SCAN
59 $scan = new TScan();
60 $scan->startRow = 'a';
61 $scan->stopRow = 'z';
62 $scan->columns = $columnArray;
63 $num = 1000;
64 $scanRets = $client->getScannerRows($scanId, $num);
65 
66 foreach($scanRets as $scanRet)
67 {
68   $scan_row = $scanRet->row;
69   $scan_cols = $scanRet->columnValues;
70   print_r($scan_row);
71   print_r($scan_cols);
72 }
73 
74 $client->closeScanner($scanId);
75 $transport->close();*/
76 
77 ?>

 

转载于:https://www.cnblogs.com/tenghuan/p/4542372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值