python 操作 hbase

import happybase
#连接
connection = happybase.Connection('localhost')
connection.open()

创建一个table

connection.create_table(
    'my_table',
    {
        'cf1': dict(max_versions=10),
        'cf2': dict(max_versions=1, block_cache_enabled=False),
        'cf3': dict(),  # use defaults
    }
)

此时,我们再通过connection.tables()查看可以使用的table,结果为[‘my_table’]

创建的table即my_table包含3个列族:cf1、cf2、cf3

获取一个table实例

一个table被创建好之后,要想对其进行操作,首先要获取这个table实例

table = connection.table('my_table')

存储数据:Hbase里 存储的数据都是原始的字节字符串

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一次只能存储一行数据

如果row key已经存在,则变成了修改数据

#全局扫描一个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参数来设置结束扫描的row key


for key, value in table.scan(row_stop='www.test3.com'):
    print( key, value)

通过row_start和row_stop参数来设置开始和结束扫描的row key

for key, value in table.scan(row_start='www.test2.com', row_stop='www.test3.com'):
    print( key, value)

通过row_prefix参数来设置需要扫描的row key,还可以通过设置row key的前缀来进行局部扫描

for key, value in table.scan(row_prefix='www.test'):
   print key, value

检索一行数据

row = table.row('www.test4.com')
print (row)

检索多行数据

rows = table.rows(['www.test1.com', 'www.test4.com'])
print(rows)

检索多行数据,返回字典

rows_dict = dict(table.rows(['www.test1.com', 'www.test4.com']))
print (rows_dict)

通过指定列族来检索数据

row = table.row('www.test1.com', columns=['cf1'])
print( row)

#通过指定列族中的列来检索数据

row = table.row('www.test1.com', columns=['cf1:price', 'cf1:rating'])
print (row)

#通过指定时间戳来检索数据,时间戳必须是整数

row = table.row('www.test1.com', timestamp=1489070666)
print (row)

在返回的数据里面包含时间戳

row = table.row(row='www.test1.com', columns=['cf1:rating', 'cf1:price'], include_timestamp=True)
print (row)

检索某一个cell所有的版本

cells = table.cells(b'www.test1.com', column='cf1:price')
print( cells)

通过设置version参数来检索前n个版本

cells = table.cells(b'www.test1.com', column='cf1:price', versions=3)
print( cells)
# 删除一整行数据
table.delete('www.test4.com')

# 删除一个列族的数据
table.delete('www.test2.com', columns=['cf1'])
# 删除一个列族中几个列的数据
table.delete('www.test2.com', columns=['cf1:name', 'cf1:price'])

链接

posted @   luoganttcc  阅读(173)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示