PHP操作Hbase代码
1 <?php
2
3 ini_set('display_errors', E_ALL);
4 $GLOBALS['THRIFT_ROOT'] = './php/src';
5
6 require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
7 require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );
8 require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );
9 require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );
10 require_once( $GLOBALS['THRIFT_ROOT'] . '/packages/Hbase/Hbase.php' );
11
12 $socket = new TSocket('10.64.60.83', '9090');
13
14 $socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)
15 $socket->setRecvTimeout(20000); // Twenty seconds
16 $transport = new TBufferedTransport($socket);
17 $protocol = new TBinaryProtocol($transport);
18 $client = new HbaseClient($protocol);
19
20 $transport->open();
21
22 //获取表列表
23 $tables = $client->getTableNames();
24 sort($tables);
25 foreach ($tables as $name) {
26
27 echo( " found: {$name}\n" );
28 }
29
30 //创建新表student
31 $columns = array(
32 new ColumnDescriptor(array(
33 'name' => 'id:',
34 'maxVersions' => 10
35 )),
36 new ColumnDescriptor(array(
37 'name' => 'name:'
38 )),
39 new ColumnDescriptor(array(
40 'name' => 'score:'
41 )),
42 );
43
44 $tableName = "student";
45 try {
46 $client->createTable($tableName, $columns);
47 } catch (AlreadyExists $ae) {
48 echo( "WARN: {$ae->message}\n" );
49 }
50 //获取表的描述
51
52 $descriptors = $client->getColumnDescriptors($tableName);
53 asort($descriptors);
54 foreach ($descriptors as $col) {
55 echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
56 }
57
58 //修改表列的数据
59 $row = '2';
60 $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";
61 $mutations = array(
62 new Mutation(array(
63 'column' => 'score',
64 'value' => $valid
65 )),
66 );
67 $client->mutateRow($tableName, $row, $mutations);
68
69
70 //获取表列的数据
71 $row_name = '2';
72 $fam_col_name = 'score';
73 $arr = $client->get($tableName, $row_name, $fam_col_name);
74 // $arr = array
75 foreach ($arr as $k => $v) {
76 // $k = TCell
77 echo ("value = {$v->value} , <br> ");
78 echo ("timestamp = {$v->timestamp} <br>");
79 }
80
81 $arr = $client->getRow($tableName, $row_name);
82 // $client->getRow return a array
83 foreach ($arr as $k => $TRowResult) {
84 // $k = 0 ; non-use
85 // $TRowResult = TRowResult
86 var_dump($TRowResult);
87 }
88
89 $transport->close();
90 ?>
2
3 ini_set('display_errors', E_ALL);
4 $GLOBALS['THRIFT_ROOT'] = './php/src';
5
6 require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
7 require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );
8 require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );
9 require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );
10 require_once( $GLOBALS['THRIFT_ROOT'] . '/packages/Hbase/Hbase.php' );
11
12 $socket = new TSocket('10.64.60.83', '9090');
13
14 $socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)
15 $socket->setRecvTimeout(20000); // Twenty seconds
16 $transport = new TBufferedTransport($socket);
17 $protocol = new TBinaryProtocol($transport);
18 $client = new HbaseClient($protocol);
19
20 $transport->open();
21
22 //获取表列表
23 $tables = $client->getTableNames();
24 sort($tables);
25 foreach ($tables as $name) {
26
27 echo( " found: {$name}\n" );
28 }
29
30 //创建新表student
31 $columns = array(
32 new ColumnDescriptor(array(
33 'name' => 'id:',
34 'maxVersions' => 10
35 )),
36 new ColumnDescriptor(array(
37 'name' => 'name:'
38 )),
39 new ColumnDescriptor(array(
40 'name' => 'score:'
41 )),
42 );
43
44 $tableName = "student";
45 try {
46 $client->createTable($tableName, $columns);
47 } catch (AlreadyExists $ae) {
48 echo( "WARN: {$ae->message}\n" );
49 }
50 //获取表的描述
51
52 $descriptors = $client->getColumnDescriptors($tableName);
53 asort($descriptors);
54 foreach ($descriptors as $col) {
55 echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
56 }
57
58 //修改表列的数据
59 $row = '2';
60 $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";
61 $mutations = array(
62 new Mutation(array(
63 'column' => 'score',
64 'value' => $valid
65 )),
66 );
67 $client->mutateRow($tableName, $row, $mutations);
68
69
70 //获取表列的数据
71 $row_name = '2';
72 $fam_col_name = 'score';
73 $arr = $client->get($tableName, $row_name, $fam_col_name);
74 // $arr = array
75 foreach ($arr as $k => $v) {
76 // $k = TCell
77 echo ("value = {$v->value} , <br> ");
78 echo ("timestamp = {$v->timestamp} <br>");
79 }
80
81 $arr = $client->getRow($tableName, $row_name);
82 // $client->getRow return a array
83 foreach ($arr as $k => $TRowResult) {
84 // $k = 0 ; non-use
85 // $TRowResult = TRowResult
86 var_dump($TRowResult);
87 }
88
89 $transport->close();
90 ?>