1、在hbase shell中添加一行数据,命令如下:
put 'tablename','row','colfamily:colname','value'
向emp表中添加几行数据:
put 'emp','1','personal data:name','lsj'
put 'emp','1','personal data:city','zz'
put 'emp','1','professional data:designation','programmer'
put 'emp','1','professional data:salary','5000'
插入两行数据之后,查看表中的数据,如下:
2、使用python thrift API方式插入数据,代码如下:
# coding=utf-8
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
# 主机地址及端口号,端口号默认为9090
host = '192.168.83.135'
port = 9090
# 初始化链接
transport = TBufferedTransport(TSocket(host, port))
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# 创建客户端
client = Hbase.Client(protocol)
client.mutateRow('empbypy','1',[Mutation(column='personal data:city',value='zz')])
client.mutateRow('empbypy','1',[Mutation(column='personal data:name',value='lsj')])
client.mutateRow('empbypy','1',[Mutation(column='professional data:designation',value='programmer')])
client.mutateRow('empbypy','1',[Mutation(column='professional data:salary',value='5000')])
transport.close()
程序运行之后,在hbase shell 中查看如下:
后期修改:在thrift官网下载的hbase包已经不能用了,只需要使用pip安装hbase-thrift即可。
3、更新表数据与插入表数据一样,都使用put命令,如下:
put ‘tablename’,’row’,’colfamily:colname’,’newvalue’
更新emp表中row为1,将列为personal data:city的值更改为bj
put ‘emp’,’1’,’personal data:city’,’bj’
4、使用python thrift API更新数据,代码如下:
# coding=utf-8
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
# 主机地址及端口号,端口号默认为9090
host = '192.168.83.135'
port = 9090
# 初始化链接
transport = TBufferedTransport(TSocket(host, port))
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# 创建客户端
client = Hbase.Client(protocol)
client.mutateRow('empbypy','1',[Mutation(column='personal data:city',value='bj')])
transport.close()