python连接hbase

1.happybase

  • 访问使用的是thrift,因此必须开启thrift(hbase thrift start)
  • 如果hbase-site.xml的hbase.regionserver.thrift.http设置为true,则该方式无法访问。会有No protocol version 的错误
# pip install happybase
# 官方文档 https://happybase.readthedocs.io/en/latest/api.html#connection
connection = happybase.Connection(host="localhost",port=9090)
print(connection.tables())
connection.close()

2.hbase-python

  • 访问使用的是zookeeper
# pip install hbase-python
# pip install kazoo
import hbase

zk = 'localhost:2181'

# 按rowkey读取一行
if __name__ == '__main__':
    with hbase.ConnectionPool(zk).connect() as conn:
        table = conn['stu']['test']  
        row = table.get('1')
        print(row)
    exit()


# scan
if __name__ == '__main__':
    with hbase.ConnectionPool(zk).connect() as conn:
        table = conn['stu']['test']
        for row in table.scan():
            print(row)
    exit()

#写入
if __name__ == '__main__':
    with hbase.ConnectionPool(zk).connect() as conn:
        table = conn['stu']['test']
        table.put(hbase.Row(
            '0001', {
                'cf:name': b'Lily',
                'cf:age': b'20'
            }
        ))
    exit()

#按文件写入
if __name__ == '__main__':
    with hbase.ConnectionPool(zk).connect() as conn:
        table = conn['mytest']['videos']
        table.write_file(video_file)  # default filename is "test_video.mp4"
    exit()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值