【Python】通过Thrift连接HBase


本博客文章如无特别说明,均为原创!转载请注明出处:Big data enthusiast(http://www.lubinsu.com/)

本文链接地址:【Python】通过Thrift连接HBase(http://www.lubinsu.com/index.php/archives/384)

这次我们来通过Python连接hbase获取数据:Python连接HBase只能通过Thrift实现,happyhbase对这个功能进行了封装。使用起来还是挺方便的。
首先需要先安装happyhbase和thrift
E:\pyprojects\data_mining>pip install happybase
Collecting happybase
Downloading happybase-1.1.0.tar.gz (40kB)
100% |████████████████████████████████| 40kB 155kB/s
Requirement already satisfied: six in d:\programs\python\python 3.5\lib\site-packages (from happybase)
Collecting thriftpy>=0.3.8 (from happybase)
Downloading thriftpy-0.3.9.tar.gz (208kB)
100% |████████████████████████████████| 215kB 505kB/s
Collecting ply<4.0,>=3.4 (from thriftpy>=0.3.8->happybase)
Downloading ply-3.10.tar.gz (150kB)
100% |████████████████████████████████| 153kB 628kB/s
Building wheels for collected packages: happybase, thriftpy, ply
Running setup.py bdist_wheel for happybase ... done
Stored in directory: C:\Users\lubin\AppData\Local\pip\Cache\wheels\60\96\09\7df5bb12d43abe4bb4b767ccff29ca252f7688fcd1b5c2b467
Running setup.py bdist_wheel for thriftpy ... done
Stored in directory: C:\Users\lubin\AppData\Local\pip\Cache\wheels\ae\3b\73\082d28b917d2886b1c0d3e0dc6f3ca3919b7e1df519580873b
Running setup.py bdist_wheel for ply ... done
Stored in directory: C:\Users\lubin\AppData\Local\pip\Cache\wheels\ad\dd\ad\8ce1991a7b380dfe23d6cc81a4de5c2775bc728b5a0a7721aa
Successfully built happybase thriftpy ply
Installing collected packages: ply, thriftpy, happybase
Successfully installed happybase-1.1.0 ply-3.10 thriftpy-0.3.9

E:\pyprojects\data_mining>pip install thrift
Collecting thrift
Downloading thrift-0.10.0.zip (87kB)
100% |████████████████████████████████| 92kB 313kB/s
Requirement already satisfied: six>=1.7.2 in d:\programs\python\python 3.5\lib\site-packages (from thrift)
Building wheels for collected packages: thrift
Running setup.py bdist_wheel for thrift ... done
Stored in directory: C:\Users\lubin\AppData\Local\pip\Cache\wheels\e7\f1\d3\b472914d95caa1781fb29b1257b85808324b0bfd1838961752
Successfully built thrift
Installing collected packages: thrift
Successfully installed thrift-0.10.0

安装完后直接上代码:

"""
hbase 连接
"""
import happybase

if __name__ == '__main__':
    connection = happybase.Connection('myd3')
    table = connection.table('test')
    print((table.row('1')['f1:column1'.encode()]).decode())

 

如果执行的时候报错:
py.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol ‘c’
 
则将 C:/Python27/Lib/site-packages/thriftpy/parser/parser.py …
if url_scheme == ”:
修改为
if len(url_scheme) <= 1:
 
再次执行:
 
EOF

发表评论

电子邮件地址不会被公开。 必填项已用*标注