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

歡迎訪問 生活随笔!

生活随笔

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

python

python 操作 hbase

發(fā)布時(shí)間:2025/4/5 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 操作 hbase 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import happybase #連接 connection = happybase.Connection('localhost') connection.open()

創(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)容,希望文章能夠幫你解決所遇到的問題。

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