日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python之操作HBASE数据库

發(fā)布時(shí)間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python之操作HBASE数据库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目前有兩個(gè)庫可以操作HBASE:hbase-thrift 和??happybase

happybase使用起來比較簡單方便,因此重點(diǎn)學(xué)習(xí)該庫,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、連接數(shù)據(jù)庫的配置信息:

#先在Linux上啟動(dòng)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 #再運(yùn)行該python腳本連接服務(wù)器from thrift.transport import TSocket from hbase import Hbase from hbase.ttypes import *host = "xxx.xxx.xxx.xxx" port = 9091 framed = Falsesocket = TSocket.TSocket(host, port) if framed:transport = TTransport.TFramedTransport(socket) else:transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)

3、操作數(shù)據(jù)庫

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)#關(guān)閉連接 transport.close()################################################# # #結(jié)果為: # 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上啟動(dòng)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 & # 再運(yùn)行python腳本連接服務(wù)器 import happybase from conf import setting # 創(chuàng)建連接,通過參數(shù)size來設(shè)置連接池中連接的個(gè)數(shù) connection = happybase.Connection(**setting.HBASE) # 打開傳輸,無返回值 connection.open() # 創(chuàng)建表,無返回值 # connection.create_table('lrx_test', # { # 'data':dict() # }) # 獲取一個(gè)表對(duì)象,返回一個(gè)happybase.table.Table對(duì)象(返回二進(jìn)制表名) table0 = connection.table('lrx_test') print('表對(duì)象為:') print(table0) #<happybase.table.Table name=b'lrx_test'> # 獲取表實(shí)例,返回一個(gè)happybase.table.Table對(duì)象(返回表名) table = happybase.Table('lrx_test',connection) print('表實(shí)例為:') print(table) #<happybase.table.Table name='lrx_test'> # 插入數(shù)據(jù),無返回值 ----在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'})# 獲取單元格數(shù)據(jù),返回一個(gè)list content = table.cells('row1','data:1') print (content) #[b'value1', b'value1'] # 獲取計(jì)數(shù)器列的值,返回當(dāng)前單元格的值 # content2 = table.counter_get('row2','data:2') # print(content2) #0 # 獲取一個(gè)掃描器,返回一個(gè)generator scanner = table.scan() for k,v in scanner:print(k,v) ########################################### # #結(jié)果為: # #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> # 獲取一行數(shù)據(jù),返回一個(gè)dict info = table.row('row2') info1={} for k,v in info.items():info1[k.decode()]=v.decode() print(info1) #獲取表名 table = connection.tables() print(table) # 關(guān)閉傳輸,無返回值 connection.close()

?

轉(zhuǎn)載于:https://www.cnblogs.com/yanwuliu/p/10695892.html

總結(jié)

以上是生活随笔為你收集整理的Python之操作HBASE数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。