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)