首页 文章

HBase中的mutateRow()通过Thrift需要未记录的第四个参数

提问于
浏览
1

当我尝试通过Thrift(特别是Python)对HBase进行插入/更新时,mutateRow()需要第四个参数“attributes” . Thrift说这个列是一个字符串 - >字符串映射 . 没有任何示例和在线讨论提到第四列,甚至提供相同,精确版本的HBase的Thrift示例也没有 .

如果可以,请仅提供创建表,定义列族,插入行和转储数据的完整示例 .

2 回答

  • -1

    没问题 . 而且,我实际上转储了修改列的最后三个版本,而不仅仅是转储已创建列的值,只是因为它很酷 .

    为了完整起见,我粗略地做了以下工作以使Thrift工作:

    • 下载并构建Thrift(使用SVN .. 2012-11-15 / 1429368) .

    • Ran "thrift -gen py <thrift file>"来自我想要创建Python接口文件的路径 .

    • 通过PIP安装"thrift"包 .

    我从生成的文件的根目录运行以下代码 .

    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    
    from hbase import Hbase
    from hbase.ttypes import *
    
    from random import randrange
    from pprint import pprint
    
    socket = TSocket.TSocket('localhost', 9090)
    transport = TTransport.TBufferedTransport(socket)
    transport.open()
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Hbase.Client(protocol)
    
    table_name = 'test_table'
    row_key = 'test_row1'
    colfamily1 = 'test_colfamily1'
    column1 = 'test_col1'
    fullcol1 = ('%s:%s' % (colfamily1, column1))
    value = ('%d' % randrange(1000, 9999))
    
    num_versions = 3
    
    try:
        desc = ColumnDescriptor(colfamily1)
        client.createTable(table_name, [desc])
    except AlreadyExists:
        pass
    
    client.mutateRow(table_name, row_key, [Mutation(column=fullcol1, value=value)], {})
    results = client.getVer(table_name, row_key, fullcol1, num_versions, {})
    
    pprint(results)
    

    输出:

    $ python test.py 
    [TCell(timestamp=1357463438825L, value='9842')]
    $ python test.py 
    [TCell(timestamp=1357463439700L, value='9166'),
     TCell(timestamp=1357463438825L, value='9842')]
    $ python test.py 
    [TCell(timestamp=1357463440359L, value='2978'),
     TCell(timestamp=1357463439700L, value='9166'),
     TCell(timestamp=1357463438825L, value='9842')]
    
  • 3

    您应该使用HappyBase从Python中使用HBase,而不是使用低级Thrift API . 有关代码示例,请参阅https://github.com/wbolster/happybase以及http://happybase.readthedocs.org/en/latest/文档中的教程 . 它包含完全符合您要求的样品 .

相关问题