本地操作

  • 启动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

最新文章

  1. Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]
  2. OA系统部门结构树
  3. bzoj1532
  4. boost::thread boost库线程
  5. canvas图形处理和进阶用法
  6. Web前端学习——HTML
  7. rs(0)与rs("字段名")的区别
  8. scrapy_redis实现爬虫
  9. SVM问题汇总
  10. E3Upload项目总结
  11. Tween animation
  12. 使用RemObjects Pascal Script
  13. dhtmlx uploader使用
  14. android view知识点 总结
  15. hibernate_boolean类型的处理
  16. C\C++与Java中的static关键字
  17. Spring Chapter4 WebSocket 胡乱翻译 (二)
  18. Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
  19. Java MVC 增删改查 实例
  20. c++函数进阶

热门文章

  1. sublime 注释模版插件DocBlockr的使用
  2. andorid 列表视图 ListView 之BaseAdapter
  3. java8 数据结构的改变(二) 对ConcurrentHashMap影响
  4. SNP(单核苷酸多态性)准确性的验证,你造吗?
  5. POJ3694 Network - Tarjan + 并查集
  6. ES6 中 let and const
  7. 需求文件requirements.txt的创建及使用
  8. python线程池
  9. Java SE学习【一】
  10. 【Java】JABX实现对象与XML互转