Hbase实战之Python调用happybase库

由于课程设计需要用到Hbase数据库,但是用不想用Java做,想用Python做,但是需要用到通信接口thrift,另外需要使用happybase库,确实很好用。本文基于实验室已经搭建好的Hadoop平台而写,使用Python调用happybase库。

1.thrift 是facebook开发并开源的一个二进制通讯中间件,通过thrift,我们可以用Python来操作Hbase
首先开启Hadoop平台的HadoopMaster的thrift服务,用Xshell连接HadoopMaster,用root用户登录,如果想关闭终端之后,thrift服务继续运行,可以用daemon模式运行

2.安装happybase和thrift

pip install happybase
pip install thrift

3.尝试连接Hbase

import happybase
connection = happybase.Connection('192.168.13.130')
print connection.tables()

此时会出现下面的错误:
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol ‘c’
解决的办法请参考这个连接:
http://stackoverflow.com/questions/39220102/error-import-impyla-library-on-windows
即将 C:\Python27\Lib\site-packages\thriftpy\parser\parser.py , line 488

if url_scheme == '':
修改为
if len(url_scheme) <= 1:

4.happybase的使用
请参考http://happybase.readthedocs.io/en/latest/index.html
在此做一下简单的使用介绍
(1)建立连接

import happybase
connection = happybase.Connection('192.168.13.130')

(2)连接建立好之后查看可以使用的table

print connection.tables()

(3)创建一个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
(4)获取一个table实例
一个table被创建好之后,要想对其进行操作,首先要获取这个table实例

table = connection.table('my_table')

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

cloth_data = {'cf1:content': u'牛仔裤', 'cf1:price': '299', 'cf1:rating': '98%'}
hat_data = {'cf1:content': u'鸭舌帽', 'cf1:price': '88', 'cf1:rating': '99%'}
shoe_data = {'cf1:content': u'耐克', '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)

(6)使用with来管理batch

with table.batch() as bat:
    bat.put('www.test5.com', {'cf1:price': '999', 'cf2:title': 'Hello Python', 'cf2:length': '34', 'cf3:code': 'A43'})
    bat.put('www.test6.com', {'cf1:content': u'剃须刀', 'cf1:price': '168', 'cf1:rating': '97%'})
    bat.put('www.test7.com', {'cf3:function': 'print'})

(7)扫描一个table里的数据,全局扫描一个table

for key, value in table.scan():
    print key, value

(8)检索数据,检索一行数据

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

直接返回该row key的值(以字典的形式),结果为:

{'cf2:name': 'LiuLin', 'cf2:date': '2017-03-09'}

检索多行数据

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

(9)更好地检索数据,通过指定列族来检索数据

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

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

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

在Hbase里,每一个cell都有一个时间戳timestamp,可以通过时间戳来检索数据
通过指定时间戳来检索数据,时间戳必须是整数

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

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值