• 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 " ".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
  • 相关阅读:
    Dark 运算符
    Dark 数据类型
    分支管理
    Git 远程仓库
    DELPHI实现百度开放平台
    win2008使用FireDac连接ORACLE数据库问题
    20160115学习日志
    20160113第一个ANDRIOD开发日志
    struts2漏洞与修复
    DELPHI XE5 与SQLITE
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10465888.html
Copyright © 2020-2023  润新知