python 操作 hbase
創(chuàng)建一個(gè)table
connection.create_table('my_table',{'cf1': dict(max_versions=10),'cf2': dict(max_versions=1, block_cache_enabled=False),'cf3': dict(), # use defaults} )此時(shí),我們?cè)偻ㄟ^connection.tables()查看可以使用的table,結(jié)果為[‘my_table’]
創(chuàng)建的table即my_table包含3個(gè)列族:cf1、cf2、cf3
獲取一個(gè)table實(shí)例
一個(gè)table被創(chuàng)建好之后,要想對(duì)其進(jìn)行操作,首先要獲取這個(gè)table實(shí)例
table = connection.table('my_table')存儲(chǔ)數(shù)據(jù):Hbase里 存儲(chǔ)的數(shù)據(jù)都是原始的字節(jié)字符串
table = connection.table('my_table')cloth_data = {'cf1:content': u'jeans', 'cf1:price': '299', 'cf1:rating': '98%'} hat_data = {'cf1:content': u'cap', 'cf1:price': '88', 'cf1:rating': '99%'} shoe_data = {'cf1:content': u'nike', 'cf1:price': '988', 'cf1:rating': '100%'} author_data = {'cf2:name': u'LiuLin', 'cf2:date': '2017-03-09'}table.put(row='www.test1.com', data=cloth_data) table.put(row='www.test2.com', data=hat_data) table.put(row='www.test3.com', data=shoe_data) table.put(row='www.test4.com', data=author_data)使用put一次只能存儲(chǔ)一行數(shù)據(jù)
如果row key已經(jīng)存在,則變成了修改數(shù)據(jù)
#全局掃描一個(gè)table
for key, value in table.scan():print (key, value) b'www.test1.com' {b'cf1:content': b'jeans', b'cf1:price': b'299', b'cf1:rating': b'98%'} b'www.test2.com' {b'cf1:content': b'cap', b'cf1:price': b'88', b'cf1:rating': b'99%'} b'www.test3.com' {b'cf1:content': b'nike', b'cf1:price': b'988', b'cf1:rating': b'100%'} b'www.test4.com' {b'cf2:date': b'2017-03-09', b'cf2:name': b'LiuLin'}#通過row_stop參數(shù)來設(shè)置結(jié)束掃描的row key
for key, value in table.scan(row_stop='www.test3.com'):print( key, value)通過row_start和row_stop參數(shù)來設(shè)置開始和結(jié)束掃描的row key
for key, value in table.scan(row_start='www.test2.com', row_stop='www.test3.com'):print( key, value)通過row_prefix參數(shù)來設(shè)置需要掃描的row key,還可以通過設(shè)置row key的前綴來進(jìn)行局部掃描
for key, value in table.scan(row_prefix='www.test'):print key, value檢索一行數(shù)據(jù)
row = table.row('www.test4.com') print (row)檢索多行數(shù)據(jù)
rows = table.rows(['www.test1.com', 'www.test4.com']) print(rows)檢索多行數(shù)據(jù),返回字典
rows_dict = dict(table.rows(['www.test1.com', 'www.test4.com'])) print (rows_dict)通過指定列族來檢索數(shù)據(jù)
row = table.row('www.test1.com', columns=['cf1']) print( row)#通過指定列族中的列來檢索數(shù)據(jù)
row = table.row('www.test1.com', columns=['cf1:price', 'cf1:rating']) print (row)#通過指定時(shí)間戳來檢索數(shù)據(jù),時(shí)間戳必須是整數(shù)
row = table.row('www.test1.com', timestamp=1489070666) print (row)在返回的數(shù)據(jù)里面包含時(shí)間戳
row = table.row(row='www.test1.com', columns=['cf1:rating', 'cf1:price'], include_timestamp=True) print (row)檢索某一個(gè)cell所有的版本
cells = table.cells(b'www.test1.com', column='cf1:price') print( cells)通過設(shè)置version參數(shù)來檢索前n個(gè)版本
cells = table.cells(b'www.test1.com', column='cf1:price', versions=3) print( cells) # 刪除一整行數(shù)據(jù) table.delete('www.test4.com')# 刪除一個(gè)列族的數(shù)據(jù) table.delete('www.test2.com', columns=['cf1']) # 刪除一個(gè)列族中幾個(gè)列的數(shù)據(jù) table.delete('www.test2.com', columns=['cf1:name', 'cf1:price'])鏈接
總結(jié)
以上是生活随笔為你收集整理的python 操作 hbase的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 向spark standalone集群提
- 下一篇: 因子分解机 Factorization