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()
上一篇:linux CentOS7下快速静默安装Oracle11GR2数据库并创建实例


下一篇:IOS UITextField &UITextView