Hbase(5)——python用happybase操作Hbase

首先要在jvm上开启hbase服务
并且在jvm上的9090端口开启thrift服务:hbase thrift start-port:9090
在这里插入图片描述

在win上的python环境中装上happybase库:pip3 install happybase
建立py文件:

import happybase

hostname = 'xx.xx.xx.xx'
table_name = 'hbase_test:basketball1'
column_family = 'Lakers'
row_key = 'California'

conn = happybase.Connection(hostname)

host_name写你的虚拟机的ip地址,用connection方法去连接你的虚拟机的Hbase
接下来自己写一些想完成的任务,比如最简单的找出自己的某一个表里的内容的函数:

def show_rows(table, row_keys=None):
    if row_keys:
        print('show value of row named %s' % row_keys)
        if len(row_keys) == 1:
            print (table.row(row_keys[0]))
        else:
            print(table.rows(row_keys))
    else:
        print('show all row values of table named %s' % table.name)
        for key, value in table.scan():
            print (key, value)

def main():
    table = conn.table(table_name)
    show_rows(table)
    
if __name__ == "__main__":
  	main()

实行后如下:
在这里插入图片描述
查看自己的Hbase数据库证实成功
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_48445640/article/details/109086149
今日推荐