用happybase happy地查詢hbase數(shù)據(jù)

用happybase進行hbase中數(shù)據(jù)的增刪改查

前提:已經(jīng)安裝happybase庫(pip install happybase),已有hbase環(huán)境并開啟thrift通訊端口(nohup hbase thrift start &),thrift默認(rèn)端口為9090,10.10.30.200為hbase主機ip

查詢

scan方法:

scan(self, row_start=None, row_stop=None, row_prefix=None,
         columns=None, filter=None, timestamp=None,
         include_timestamp=False, batch_size=1000, scan_batching=None,
         limit=None, sorted_columns=False, reverse=False)

參數(shù):

row_start、row_stop:起始和終止rowkey,查詢兩rowkey間的數(shù)據(jù)

row_prefix:rowkey前綴。注:使用row_prefix的時候,row_start和row_stop不能使用

filter:要使用的過濾器(hbase 0.92版本及以上生效)

timestamp:按指定時間戳查詢

reverse:默認(rèn)為False。為True時,scan結(jié)果按rowkey倒序排列

e.g:

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#獲取table實例
t = connection.table('iot_flow_history')
#按row前綴遍歷查詢數(shù)據(jù),因該工程存入hbase的rowkey為id-timestamp,使用rowkey查詢十分不方便,故使用row_prefix進行查詢
for key,value in t.scan(row_prefix='1100HD72004016A16'):
    print key,value
增加&編輯

put方法:

put(self, row, data, timestamp=None, wal=True)

e.g:

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#獲取table實例
t = connection.table('iot_flow_history')

#該表已有column familylz,列id和realValue
student = {"lz:id":'test',"lz:realValue":"18"}
t.put(row="test",data = student)

△ 如put中的rowkey已存在,則為修改數(shù)據(jù)

刪除

delete方法:

delete(self, row, columns=None, timestamp=None, wal=True)

row:刪除rowkey為row的數(shù)據(jù)

columns:指定columns參數(shù)時,刪除

e.g:

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#.delete('test')
t = connection.table('iot_flow_history')
#刪除rowkey為test的數(shù)據(jù)
t.delete('test')

刪除rowkey為student2的name數(shù)據(jù):

mark
t = connection.table('students')
t.delete(row='student2',columns=["account:name"])

刪除成功:

mark
批量處理

batch方法:

batch(self, timestamp=None, batch_size=None, transaction=False,
          wal=True)

1、批量操作

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#.delete('test')
t = connection.table('iot_flow_history')

student111 = {"account:name":"xiaohuang","address:zipcode":"222222"}
student222 = {"account:name":"xiaolv","address:zipcode":"333333"}
t = connection.table('students')
bat = t.batch()

#添加student111和student222,也可以進行批量刪除
bat.put('student111',data = student111)
bat.put('student222',data = student222)
#bat.delete('student111')
bat.send()
mark

2、使用with管理批量

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#.delete('test')
t = connection.table('iot_flow_history')

student333 = {"account:name":"xiaolan","address:zipcode":"323232"}
t = connection.table('students')

#添加student333并刪除student222
with t.batch() as bat:
    bat.put('student333',data = student333)
    bat.delete('student222')
檢索

row方法及rows()方法,檢索指定rowkey的數(shù)據(jù)

檢索一條:

r = t.row('student444')

檢索多條:

r = t.rows(['student444','student333'])

返回結(jié)果:

mark
查詢指定cell的數(shù)據(jù)
cells(self, row, column, versions=None, timestamp=None,
          include_timestamp=False)

e.g:

import happybase

connection = happybase.Connection('10.10.30.200',9090)
#.delete('test')
t = connection.table('iot_flow_history')

#查詢rowkey為student444,column為account:name的cell的數(shù)據(jù),包含時間戳
print t.cells('student444','account:name',include_timestamp=True)

結(jié)果:

[('xiaolan', 1511343966713L)]

暫時就這些0v0

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容