PHP經過thrift2訪問HBASE

前一段時間須要在網頁上顯示HBASE查詢的結果,考慮用PHP來實現,在網上搜了一下,廣泛都是用thrift做爲接口來實現的。​php

參考博文:​html

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

用上述網址裏提供的PHP代碼,能夠訪問公司裏的一個HBASE集羣,可是另外一個集羣怎麼也訪問不了,上網查了一下,發現thrift有兩套HBASE的接口--thrift和thrift2,並且兩套接口並不兼容。ui

用thrift2的接口替換了上述網址裏的thrift接口,另一個HBASE集羣就也能夠訪問了。spa

thrift2接口PHP文件.net

http://yunpan.cn/cwSDFaSPuWYIN  訪問密碼 fd38code

http://yunpan.cn/cwSUnCGrsiwyy  訪問密碼 c5b8htm

其中包含兩個文件​:Hbase_types.php和THBaseService.php,blog

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 ?>
相關文章
相關標籤/搜索