总结
豆包 AI 助手文章总结

php操作hbase例证

php中文网
发布: 2016-06-13 10:55:08
原创
1468人浏览过

php操作hbase例子

1 $globals['thrift_root'] = '/home/thrift';
2 require_once $globals['thrift_root'].'/thrift.php';
3 require_once $globals['thrift_root'].'/protocol/tbinaryprotocol.php';
4 require_once $globals['thrift_root'].'/transport/tsocket.php';
5 require_once $globals['thrift_root'].'/transport/thttpclient.php';
6 require_once $globals['thrift_root'].'/transport/tbufferedtransport.php';
7 require_once $globals['thrift_root'].'/packages/hbase/hbase.php';
8 require_once($globals['thrift_root'].'/packages/hadoopfs/thrifthadoopfilesystem.php');
9
10 $socket = new tsocket(192.168.1.4, 9091);
11 $socket->setsendtimeout(20000);
12 $socket->setrecvtimeout(20000);
13 $transport = new tbufferedtransport($socket);
14 $protocol = new tbinaryprotocol($transport);
15 $hbase = new hbaseclient($protocol);
16 $transport->open();
17
18 // 在这里实现功能
19
20 $transport->close();
21
22 // 多记录批量提交(200提交一次时测试小记录大概在5000/s左右):
23 $rows = array('timestamp'=>$timestamp, 'columns'=>array('txt:col1'=>$col1, 'txt:col2'=>$col2, 'txt:col3'=>$col3));
24 $records = array(rowkey=>$rows,...);
25 $batchrecord = array();
26 foreach ($records as $rowkey => $rows) {
27 $timestamp = $rows['timestamp'];
28 $columns = $rows['columns'];
29 // 生成一条记录
30 $record = array();
31 foreach($columns as $column => $value) {
32 $col = new mutation(array('column'=>$column, 'value'=>$value));
33 array_push($record, $col);
34 }
35 // 加入记录数组
36 $batchtmp = new batchmutation(array('row'=>$rowkey, 'mutations'=>$record));
37 array_push($batchrecord, $batchtmp);
38 }
39 $ret = $hbase->mutaterows('test', $batchrecord);
40
41 // 单记录提交(1000/s左右)
42 $mutation = array(new mutation(array('column'=>'txt:col1', 'value'=>$col1)),
43 new mutation(array('column'=>'txt:col2', 'value'=>$col2)),
44 new mutation(array('column'=>'txt:col3', 'value'=>$col3)));
45 $hbase->mutaterow('test', $rowkey, $mutation);
46
47 // 扫描记录
48 $result = $hbase->scanneropenwithstop($table, $startkey, $endkey, $columns);
49 while (true) {
50 $record = $hbase->scannerget($result);
51 if ($record == null) {
52 break;
53 }
54 $recordarray = array();
55 foreach($record as $trowresult) {
56 $row = $trowresult->row;
57 $column = $trowresult->columns;
58 foreach($column as $family_column=>$cell) {
59 $recordarray[$family_column] = $cellval;
60 }
61 $resultarray[] = $recordarray;
62 }
63 }
64 print_r($resultarray);
65
66 // 以下记录从别人处(佛祖球球)拷贝,把几个接口补全,(未测试...)
67
68 // 列出hbase 裡的所有 table
69 echo( "listing tables...\n" );
70 $tables = $client->gettablenames();
71 sort( $tables );
72 foreach ( $tables as $name )
73 {
74 echo $name."\n";
75 }
76
77 // 刪除table
78 $name = "test2";
79 if($client->istableenabled($name))
80 {
81 echo "关闭".$name."资料表\n";
82 $client->disabletable($name);
83 }
84 echo "刪除中...\n";
85 $client->deletetable($name);
86 echo "刪除完成";
87
88 // 新增table
89 $columns = array(new columndescriptor(array('name' => 'name:')),
90 new columndescriptor(array('name'=> 'scores:',)));
91 $t = "test2";
92 echo("creating table: {$t}\n");
93 try
94 {
95 $client->createtable( $t, $columns );
96 }
97 catch (alreadyexists $ae)
98 {
99 echo( "warn: {$ae->message}\n" );
100 }
101
102 //列出table內的column family
103 $t = "results";
104 echo("column families in {$t}:\n");
105 $descriptors = $client->getcolumndescriptors( $t );
106 asort( $descriptors );
10

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号