mac下通过python操作hbase数据库 - Go语言中文社区

mac下通过python操作hbase数据库


在开始之前请安装好hadoop hbase,安装方法请参考:https://blog.csdn.net/sunxiaoju/article/details/86183405

1、首先需要安装Thrift,通过命令brew install thrift安装,如下图所示:

2、然后从https://archive.apache.org/dist/hbase/2.0.4/位置下载源码包,注意版本号要对应机器上所装的hbase的版本,如下图所示:

3、然后将下载的压缩包通过tar -xzvf hbase-2.0.4-src.tar.gz命令解压,cd到解压目录,然后通过find . -name Hbase.thrift命令查找Hbase.thrift文件,如下图所示:

4、然后通过cp ./hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift ../将该文件拷贝到上一层目录,如下图所示:

5、然后执行thrift -gen py Hbase.thrift,如下图所示:

6、此时会生成一个gen-py的目录,该目录就是hbase的python包,如下图所示:

7、然后通过cp -r hbase ~/sunxj/student8/将hbase的包拷贝到所需要的目录中,如下图所示:

9、通过hbase脚本启动thrift,命令为:hbase-daemon.sh start thrift,如下图所示:

10、此时通过jps可以查看到ThriftServer进程已经启动成功,如下图所示:

11、查看thrift的版本:thrift -version,如下图所示:


12、然后在https://archive.apache.org/dist/thrift/位置选择相应的版本下载,如下图所示:

13、解压找到thrift-0.12.0/lib/py/src目录就是python所需的代码,将此目录中的代码拷贝到thrift(此目录通过mkdir新建),然后通过cp -r thrift-0.12.0/lib/py/src/* thrift/将代码拷贝过来,如下图所示:

14、此时的目录结构如下:

15、编写python代码,文件名为:create_hbase_table.py,如下代码所示:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('localhost',9090)
transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)

transport.open()

base_info_contents = ColumnDescriptor(name='meta_data:',maxVersions=1)
other_info_contents = ColumnDescriptor(name='flags:',maxVersions=1)

client.createTable('new_music_table',[base_info_contents,other_info_contents])

print client.getTableNames()

16、然后执行python create_hbase_table.py,运行结果如下图所示:

注意:如果出现找不到thrift包时,那是由于在hbase中的HBase.py中有引入,所需要么把thrift目录拷贝到hbase中,要么把thrift放到python的/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages目录中,此目录是mac系统的

17、此时打印出了hbase里的数据库,其中new_music_table是新建的数据库,然后进入hbase shell通过describe 'new_music_table'命令查看,如下图所示:

18、说明通过python创建成功了。

19、然后插入数据的python代码文件为:insert_hbase_table.py ,代码如下:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('localhost',9090)
transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)

transport.open()

tableName='new_music_table'
rowkey='1024'
mutations=[Mutation(column='meta_data:name',value='beijinghuanyingni'),Mutation(column='meta_data:tag',value='pop'),Mutation(column='flags:is_valid',value='true')]
client.mutateRow(tableName,rowkey,mutations,None)

20、然后通过python insert_hbase_table.py 执行,如下图所示:

21、此时在hbase shell中使用scan new_music_table查看数据,如下图所示:

22、然后将这些信息通过python代码读取,代码文件为:query_hbase_table.py,代码如下:


from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('localhost',9090)
transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)

transport.open()

tableName='new_music_table'
rowkey='1024'

result=client.getRow(tableName,rowkey,None)

for r in result:
        print 'the row is: ',r.row
        print 'the meta_data:name is: ',r.columns.get('meta_data:name').value
        print 'the meta_data:tag is: ',r.columns.get('meta_data:tag').value
        print 'the flags:is_valid is: ',r.columns.get('flags:is_valid').value

23、执行python query_hbase_table.py 运行结果如下图所示:

24、到此完结

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sunxiaoju/article/details/102974759
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 18:27:51
  • 阅读 ( 1368 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢