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

时间:2013-01-06 09:26:31

标签: python hadoop hbase thrift

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

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

2 个答案:

答案 0 :(得分:3)

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

为了完整起见,我粗略地做了以下工作以使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')]

答案 1 :(得分:-1)

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