一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

python如何thrift框架操作hbase数据库及shell操作

时间:2015-03-15 编辑:简简单单 来源:一聚教程网

以前都是用mongodb的,但是量大了,mongodb显得不那么靠谱,改成hbase撑起一个量级。

HBase是Apache Hadoop的数据库,能够对大型数据提供随机、实时的读写访问。HBase的目标是存储并处理大型的数据。HBase是一个开源的,分布式的,多版本的,面向列的存储模型。它存储的是松散型数据。

HBase提供了丰富的访问接口。

HBase Shell
Java clietn API
Jython、Groovy DSL、Scala
REST
Thrift(Ruby、Python、Perl、C++…)
MapReduce
Hive/Pig

hbase(main):001:0>

#创建表

hbase(main):002:0* create 'blog','info','content'

0 row(s) in 2.0290 seconds

#查看表

hbase(main):003:0> list

TABLE                                                                          

blog                                                                           

test_standalone                                                                

2 row(s) in 0.0270 seconds

#增添数据

hbase(main):004:0> put 'blog','1','info:editor','liudehua'

0 row(s) in 0.1340 seconds


hbase(main):005:0> put 'blog','1','info:address','bj'

0 row(s) in 0.0070 seconds

hbase(main):006:0> put 'blog','1','content:header','this is header'

0 row(s) in 0.0070 seconds


hbase(main):007:0>

hbase(main):008:0*

hbase(main):009:0* get 'blog','1'

COLUMN                CELL                                                     

 content:header       timestamp=1407464302384, value=this is header            

 info:address         timestamp=1407464281942, value=bj                        

 info:editor          timestamp=1407464270098, value=liudehua                  

3 row(s) in 0.0360 seconds

hbase(main):010:0> get 'blog','1','info'

COLUMN                                                 CELL                                                                                                                                                        
 info:address                                          timestamp=1407464281942, value=bj                                                                                                                           
 info:editor                                           timestamp=1407464270098, value=liudehua                                                                                                                     
2 row(s) in 0.0120 seconds


#这里是可以按照条件查询的。

hbase(main):012:0* scan 'blog'

ROW                                                    COLUMN+CELL                                                                         

 1                                                     column=content:header, timestamp=1407464302384, value=this is header                                                                                        
 1                                                     column=info:address, timestamp=1407464281942, value=bj                                                                                                      
 1                                                     column=info:editor, timestamp=1407464270098, value=liudehua                                                                                                 
1 row(s) in 0.0490 seconds


hbase(main):013:0>

hbase(main):014:0* put 'blog','1','content:header','this is header2'

0 row(s) in 0.0080 seconds

hbase(main):015:0>

hbase(main):016:0*

hbase(main):017:0* put 'blog','1','content:header','this is header3'

0 row(s) in 0.0050 seconds

hbase(main):018:0> scan 'blog'

ROW                                                    COLUMN+CELL                                                                                                                                                 
 1                                                     column=content:header, timestamp=1407464457128, value=this is header3                                                                                       
 1                                                     column=info:address, timestamp=1407464281942, value=bj                                                                                                      
 1                                                     column=info:editor, timestamp=1407464270098, value=liudehua                                                                                                 
1 row(s) in 0.0180 seconds

hbase(main):020:0> get 'blog','1','content:header'

COLUMN                                                 CELL                                                                                                                                                        
 content:header                                        timestamp=1407464457128, value=this is header3                                                                                                              
1 row(s) in 0.0090 seconds

hbase(main):021:0>



#可以看到历史版本记录

hbase(main):022:0* get 'blog','1',{COLUMN => 'content:header',VERSIONS => 2}

COLUMN                                                 CELL                                                                                

 content:header                                        timestamp=1407464457128, value=this is header3                                                                                                              
 content:header                                        timestamp=1407464454648, value=this is header2                                                                                                              
2 row(s) in 0.0100 seconds

#可以看到历史版本记录

hbase(main):023:0> get 'blog','1',{COLUMN => 'content:header',VERSIONS => 3}

COLUMN                                                 CELL                                                                                

 content:header                                        timestamp=1407464457128, value=this is header3                                                                                                              
 content:header                                        timestamp=1407464454648, value=this is header2                                                                                                              
 content:header                                        timestamp=1407464302384, value=this is header                                                                                                               
3 row(s) in 0.0490 seconds

hbase(main):024:0>


base用java来操作是最方便,也效率最高的方式。但java并非轻量级,不方便在任何环境下调试。而且不同的开发人员熟悉的语言不一样,开发效率也不一样。hbase 通过thrift,还可以用python,ruby,cpp,perl等语言来操作。


thrift是facebook开发开源的类似google的protobuf的远程调用组件。但protobuf只有数据的序列化,且只支持二进制协议,没有远程调用部分。protobuf原生支持cpp,python,java,另外还有第三方实现的objectc,ruby等语言。而thrift是实现了序列化,传输,协议定义,远程调用等功能,跨语言能力更多。某些方面二者可以互相替代,但一些方面则各有适用范围。

thrift的安装及thrift python的相关模块 ~

 代码如下 复制代码
http://www.apache.org/dist//thrift/0.9.1/thrift-0.9.1.tar.gz
tar zxvf thrift-0.8.0.tar.gz
cd thrift-0.8.0
./configure -with-cpp=no
make
sudo make install
sudo pip install thrift



这里是可以生成python的thrift和hbase模块 ~


thrift -gen py /home/ubuntu/hbase-0.98.1/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

wKiom1PkaziSW8boAAFfmCFA2DA129.jpg

 代码如下 复制代码
from thrift.transport import TSocket
 
from thrift.protocol import TBinaryProtocol
 
from hbase import Hbase
 
transport=TSocket.TSocket('localhost',9090)
 
protocol=TBinaryProtocol.TBinaryProtocol(transport)
 
client=Hbase.Client(protocol)
 
transport.open()
 
client.getTableNames()



hbase 0.98的版本 貌似没有thrift的相关组建,我这里的用的是0.94版本搞定的。

 

热门栏目