Python之操作HBASE数据库

时间:2022-04-08 08:26:43

目前有两个库可以操作HBASE:hbase-thrift 和  happybase

happybase使用起来比较简单方便,因此重点学习该库,hbase-thrift只做简要介绍。

(一)hbase-thrift

1、使用前先添加库和依赖库:

pip install thrift
pip install hbase-thrift
pip install google-cloud
pip install google-cloud-vision
pip install kazoo

2、连接数据库的配置信息:

#先在Linux上启动HBASE server
#/opt/cloudera/parcels/CDH/lib/hbase/bin/hbase-daemon.sh --config /opt/cloudera/parcels/CDH/lib/hbase/conf foreground_start thrift --infoport 9096 -p 9091
#再运行该python脚本连接服务器

from thrift.transport import TSocket
from hbase import Hbase
from hbase.ttypes import *

host = "xxx.xxx.xxx.xxx"
port = 9091
framed = False

socket = TSocket.TSocket(host, port)
if framed:
    transport = TTransport.TFramedTransport(socket)
else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)

3、操作数据库

print ("Thrift2 Demo")
print ("This demo assumes you have a table called \"example\" with a column family called \"family1\"")

#打开连接
transport.open()

# 获取所有表名
tableNames = client.getTableNames()
print('tableNames:', tableNames)

#关闭连接
transport.close()


#################################################
# #结果为:
# Thrift2 Demo
# This demo assumes you have a table called "example" with a column family called "family1"
# tableNames: ['lrx_hbase_test', 'lrx_hbase_test2', 'lrx_hbase_test3', 'lrx_test']

 (二)happybase