最近项目中需要使用thrift和php来读写hbase中的相关数据,所以就整理了下相关的类,做了下测试.
现在自己用到的操作hbase的方式主要有以下几种:
1.hbase shell, 主要是配置后执行 shell 通过命令查看 hbase 中的数据,比如 count 'xxxx', scan 'xxxx' 等.
2.通过native java api , 自己封装了一个 restfull的api , 通过提供的api(http)方式来操作hbase
3.使用thrift 的序列化技术,thrift支持c++,php,python等语言,适合其他的异构系统操作hbase,这块刚刚尝试
4.使用hbasexplorer,之前写的一个图形化的客户端来操作hbase, http://www.cnblogs.com/scotoma/archive/2012/12/18/2824311.html
5. hive/pig , 这个现在还没真正的用过.
当前主要讲第三种方式 thrift, 这个是facebook开源出来的, 官方网站是 http://thrift.apache.org/ .
下载安装和启动,请看参考文章中的内容
查看是否跑成功...
使用php 类文件操作hbase, 生成类文件的方式,请看参考文章中的生产的方法,不过我自己测试的生成方法有bug,生成的 类文件中 namespace 是空的, 但是从官方源码库中生成的是 namespace hbase, 所以这里需要注意一下.
我调试了一个驱动类文件,放到了github上了,大家需要的可以下载使用.
https://github.com/xinqiyang/buddy/tree/master/vender/thrift
接下来进行测试操作,参考http://blog.csdn.net/hguisu/article/details/7298456 这里的测试类,写了个测试,并调试了下
open();//echo time: . $client -> time();$tables = $client->gettablenames();sort($tables);foreach ($tables as $name) { echo $name.\r\n;}//create a fc and then create a table$columns = array( new \hbase\columndescriptor(array( 'name' => 'id:', 'maxversions' => 10 )), new \hbase\columndescriptor(array( 'name' => 'name:' )), new \hbase\columndescriptor(array( 'name' => 'score:' )),);$tablename = student;/*try { $client->createtable($tablename, $columns);} catch (alreadyexists $ae) { var_dump( warn: {$ae->message}\n );}*/// get table descriptors$descriptors = $client->getcolumndescriptors($tablename);asort($descriptors);foreach ($descriptors as $col) { var_dump( column: {$col->name}, maxver: {$col->maxversions}\n );}//set clomn//add update column data$time = time();var_dump($time);$row = '2';$valid = foobar-.$time;$mutations = array( new \hbase\mutation(array( 'column' => 'score', 'value' => $valid )),);$mutations1 = array( new \hbase\mutation(array( 'column' => 'score:a', 'value' => $time, )),);$attributes = array ();//add row, write a row$row1 = $time;$client->mutaterow($tablename, $row1, $mutations1, $attributes);echo -------write row $row1 ---\r\n;//update row$client->mutaterow($tablename, $row, $mutations, $attributes);//get column data$row_name = $time;$fam_col_name = 'score:a';$arr = $client->get($tablename, $row_name, $fam_col_name, $attributes);// $arr = arrayforeach ($arr as $k => $v) { // $k = tcell echo ------ get one : value = {$v->value} ,
; echo ------ get one : timestamp = {$v->timestamp}
;}echo ----------\r\n;$arr = $client->getrow($tablename, $row_name, $attributes);// $client->getrow return a arrayforeach ($arr as $k => $trowresult) { // $k = 0 ; non-use // $trowresult = trowresult var_dump($trowresult);}echo ----------\r\n;/****** //no test public function scanneropenwithscan($tablename, \hbase\tscan $scan, $attributes); public function scanneropen($tablename, $startrow, $columns, $attributes); public function scanneropenwithstop($tablename, $startrow, $stoprow, $columns, $attributes); public function scanneropenwithprefix($tablename, $startandprefix, $columns, $attributes); public function scanneropents($tablename, $startrow, $columns, $timestamp, $attributes); public function scanneropenwithstopts($tablename, $startrow, $stoprow, $columns, $timestamp, $attributes); public function scannerget($id); public function scannergetlist($id, $nbrows); public function scannerclose($id);*/echo ----scanner get ------\r\n;$startrow = '1';$columns = array ('column' => 'score', );//$scan = $client->scanneropen($tablename, $startrow, $columns, $attributes);//$startandprefix = '13686667';//$scan = $client->scanneropenwithprefix($tablename,$startandprefix,$columns,$attributes);//$startrow = '1';//$stoprow = '2';//$scan = $client->scanneropenwithstop($tablename, $startrow, $stoprow, $columns, $attributes);//$arr = $client->scannerget($scan);$nbrows = 1000;$arr = $client->scannergetlist($scan, $nbrows);var_dump('count of result :'.count($arr));foreach ($arr as $k => $trowresult) { // code... //var_dump($trowresult);}$client->scannerclose($scan);//close transport$transport->close();
这里操作了 createtable , insert row , get table , update row,scan table 这些常用的,先熟悉下.
实际操作的时候,需要注意:
1.php的版本,需要支持命名空间,所以需要5.3以上的支持
2.安装thrift的php扩展,貌似这个没有实际用到,还是得使用相关的php文件,谁能写个扩展就好了.不知道性能是否能够提升.
3.对于scan的相关操作,测试了 start/stop, prefix的scan,感觉还是可以的.
4.感觉php的命名空间很挫,怎么办..\分割感觉就是那么的不地道......
接下来,有时间的话,会做下其他的几个操作,并进行压力测试,并将这个部署到集群中去.
大家有用thrift的欢迎交流,感谢hguisu写的这个文章(参考文章),让大家能够尽快的入门.
更新内容:
20130517 在集群上启动了thrift发现写入操作的时候,还是不稳定,有比较严重的超时现象,对于这块的操作,需要进行 php 操作类的优化. 其实感觉操作类还是写的太复杂的了.
参考文章:
http://blog.csdn.net/hguisu/article/details/7298456
http://www.bkjia.com/phpjc/440283.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/440283.htmltecharticle最近项目中需要使用thrift和php来读写hbase中的相关数据,所以就整理了下相关的类,做了下测试. 现在自己用到的操作hbase的方式主要有以下几...
