python连接hbase

2021/6/20 17:20:14

本文主要是介绍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()


这篇关于python连接hbase的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程