HBase-shell及happyhbase

在这里插入图片描述

HappyBase操作HBase

  • 启动HBase thrift server :
hbase-daemon.sh start thrift
  • 安装happy base
pip install happybase
  • 如何使用HappyBase

  • 建立连接

import happybase
connection = happybase.Connection('somehost')
  • 当连接建立时, 会自动创建一个与 HBase Thrift server的socket链接. 可以通过参数禁止自动链接, 然后再需要连接是调用 Connection.open():
connection = happybase.Connection('somehost', autoconnect=False)
# before first use:
connection.open()
  • Connection 这个类提供了一个与HBase交互的入口, 比如获取HBase中所有的表: Connection.tables():
print(connection.tables())
  • 操作表
    Table类提供了大量API, 这些API用于检索和操作HBase中的数据。 在上面的示例中,我们已经使用Connection.tables()方法查询HBase中的表。 如果还没有任何表,可使用Connection.create_table()创建一个新表:
connection.create_table('users',{'cf1': dict()})
  • 创建表之后可以传入表名获取到Table类的实例:
table = connection.table('mytable')
  • 查询操作
# api
table.scan() #全表查询
table.row('row_key') # 查询一行
table.rows([row_keys]) # 查询多行
#封装函数
def scanQuery():
    # 创建和hbase的连接
    connection = happybase.Connection('192.168.19.137')
    #通过connection找到user表 获得table对象
    table = connection.table('user')
    filter = "ColumnPrefixFilter('username')"
    #row_start 指定起始rowkey 缩小查询范围
    #filter 添加过滤器
    for key,value in table.scan(row_start='rowkey_10',filter=filter):
        print(key,value)
    # 关闭连接
    connection.close()
def getQuery():
    connection = happybase.Connection('192.168.19.137')
    # 通过connection找到user表 获得table对象
    table = connection.table('user')
    result = table.row('rowkey_22',columns=['base_info:username'])
    #result = table.row('rowkey_22',columns=['base_info:username'])
    result = table.rows(['rowkey_22','rowkey_16'],columns=['base_info:username'])
    print(result)
    # 关闭连接
    connection.close()
  • 插入数据
#api
table.put(row_key, {'cf:cq':'value'})
def insertData():
    connection = happybase.Connection('192.168.19.137')
    # 通过connection找到user表 获得table对象
    table = connection.table('users')
    table.put('rk_01',{'cf1:address':'beijing'})
    # 关闭连接
    for key,value in table.scan():
        print(key,value)
    connection.close()
  • 删除数据
#api
table.delete(row_key, cf_list)

def deleteData():
    connection = happybase.Connection('192.168.19.137')
    # 通过connection找到user表 获得table对象
    table = connection.table('users')
    table.delete('rk_01',['cf1:username'])
    # 关闭连接
    for key,value in table.scan():
        print(key,value)
    connection.close()
  • 删除表
#api
conn.delete_table(table_name, True)
#函数封装
def delete_table(table_name):
    pretty_print('delete table %s now.' % table_name)
    conn.delete_table(table_name, True)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值