python操作Hbase

本地操作

  • 启动thrift服务:./bin/hbase-daemon.sh start thrift

  • hbase模块产生:

    • 下载thrfit源码包:thrift-0.8.0.tar.gz

    • 解压安装

    • ./configure

    • make

    • make install

  • 在thrift-0.8.0目录中,lib/py/build/lib.linux-x86_64-2.6/目录下存在thrift的python模块,拷贝出来即可

  • 生成hbase模块

    • 下载源码包:hbase-0.98.24-src.tar.gz

    • 解压,进入下面目录:hbase-0.98.24/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift

    • thrift --gen py Hbase.thrift

  • 创建表格

    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol

    from hbase import Hbase
    from hbase.ttypes import *

    transport = TSocket.TSocket('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

    protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)

    transport.open()

    base_info_contents = ColumnDescriptor(name='meta-data',maxVersions=1)
    other_info_contents = ColumnDescriptor(name='flags',maxVersions=1)

    client.createTable('new_music_table', [base_info_contents, other_info_contents])

    print client.getTableNames()
  • 写数据

    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol

    from hbase import Hbase
    from hbase.ttypes import *

    transport = TSocket.TSocket('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

    protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)

    transport.open()

    tableName = 'new_music_table'
    rowKey = '1100'

    mutations = [Mutation(column="meta-data:name" , value="wangqingshui"),\
    Mutation(column="meta-data:tag" , value="pop"),\
    Mutation(column="meta-data:is_valid" , value="TRUE")]
    client.mutateRow(tableName,rowKey,mutations,None)
  • 读数据

    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol

    from hbase import Hbase
    from hbase.ttypes import *

    transport = TSocket.TSocket('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

    protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)

    transport.open()

    tableName = 'new_music_table'
    rowKey = '1100'

    result = client.getRow(tableName,rowKey,None)
    for r in result:
       print 'the row is ',r.row
       print 'the name is ',r.columns.get('meta-data:name').value
       print 'the name is ',r.columns.get('meta-data:is_valid').value
    • 读多条数据

    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol

    from hbase import Hbase
    from hbase.ttypes import *

    transport = TSocket.TSocket('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

    protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)

    transport.open()

    tableName = 'new_music_table'

    scan = TScan()
    id = client.scannerOpenWithScan(tableName , scan , None)
    result = client.scannerGetList(id,10)

    for r in result:
       print '==============='
       print 'the row is ' , r.row
       for k,v in r.columns.items():
           print "\t".join([k,v.value])

    # 执行结果
    ===============
    the row is  1100
    meta-data:name  wangqingshui
    meta-data:is_valid      TRUE
    meta-data:tag   pop
    ===============
    the row is  2200
    meta-data:name  langdechuanshuo
上一篇:ambari安装集群下python连接hbase之安装thrift


下一篇:CORS跨域cookie传递