微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐