Python之操作HBASE数据库
⽬前有两个库可以操作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脚本连接服务器
ansport import TSocket
from hbase import Hbase
pes import *
host = "x"
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 = TableNames()
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
# pip install thrift
# pip install happybase
# 先在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脚本连接服务器
import happybase
from conf import setting
# 创建连接,通过参数size来设置连接池中连接的个数
connection = happybase.Connection(**setting.HBASE)
# 打开传输,⽆返回值
connection.open()
# 创建表,⽆返回值
# ate_table('lrx_test',
#                        {
#                            'data':dict()
#                        })
# 获取⼀个表对象,返回⼀个happybase.table.Table对象(返回⼆进制表名)
table0 = connection.table('lrx_test')
print('表对象为:')
print(table0) #<happybase.table.Table name=b'lrx_test'>
# 获取表实例,返回⼀个happybase.table.Table对象(返回表名)
table = happybase.Table('lrx_test',connection)
print('表实例为:')
print(table) #<happybase.table.Table name='lrx_test'>
# 插⼊数据,⽆返回值 ----在row1⾏,data:1列插⼊值value1
for i in range(5):
table.put('row%s' %i,{'data:%s'%i:'%s' %i} )
table.put('row5',{'data:5':'value1'})
# 获取单元格数据,返回⼀个list
content = lls('row1','data:1')
print (content) #[b'value1', b'value1']
# 获取计数器列的值,返回当前单元格的值
# content2 = unter_get('row2','data:2')
# print(content2) #0
# 获取⼀个扫描器,返回⼀个generator
scanner = table.scan()
for k,v in scanner:
print(k,v)
>>>>>>>>###
# #结果为:
# #b'row0' {b'data:0': b'0'}
# b'row1' {b'data:1': b'value1'}
# b'row2' {b'data:2': b'2'}
# b'row3' {b'data:3': b'3'}
# b'row4' {b'data:4': b'4'}
print(scanner) #<generator object Table.scan at 0x000001E17CCDAF10> # 获取⼀⾏数据,返回⼀个dict
info = w('row2')
info1={}
for k,v in info.items():
info1[k.decode()]=v.decode()
print(info1)
#获取表名
table = connection.tables()
print(table)
# 关闭传输,⽆返回值
connection.close()
>thrift

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论