1

使用 Python 中 HBase 的 HappyBase API,可以通过以下方式执行批量插入:

import happybase
connection = happybase.Connection()
table = connection.table('table-name')
batch = table.batch()
# put several rows to this batch via batch.put()
batch.send()

如果这批中途失败会发生什么?已经保存的行会保持保存而那些没有保存吗?

我在 HappyBase github 中注意到该table.batch()方法采用transactionwal作为参数。是否可以将这些配置为在批处理中途失败的情况下回滚成功保存的行?

happybase 会在这里抛出异常,这将允许我记下行键并执行批量删除吗?

4

2 回答 2

2

您是否按照 Happybase 文档中有关批量突变的教程进行操作?看起来你在这里混淆了一些东西。https://happybase.readthedocs.org/en/latest/user.html#performing-batch-mutations

批处理纯粹是一种性能优化:它们避免了存储/删除的每一行到 Thrift 服务器的往返,这可能会导致显着的加速。

正如上面链接的用户指南中的许多示例所解释的,上下文管理器行为(with块)是一个纯粹的客户端便利 API,它使应用程序代码更易于编写和推理。如果with块成功完成,所有突变都会一次性发送到服务器。

然而……那只是幸福的道路。with如果在块的某个地方引发了一些 Python 异常,该怎么办?这就是transaction标志发挥作用的地方: if True,根本没有数据发送到服务器, if False,无论如何都会刷新任何待处理的数据。首选哪种行为很大程度上取决于您的用例。

于 2015-06-25T13:27:18.280 回答
1

我不知道python或happybase。我知道事务是在库中作为后备策略实现的。由于 Hbase 除了行内突变之外没有任何事务支持,因此库只能通过回滚刚刚执行的操作来模拟事务。我认为代码中的这个 Batch 类可以做到这一点。

    The `transaction` argument specifies whether the returned
    :py:class:`Batch` instance should act in a transaction-like manner when
    used as context manager in a ``with`` block of code. The `transaction`
    flag cannot be used in combination with `batch_size`.
    The `wal` argument determines whether mutations should be
    written to the HBase Write Ahead Log (WAL). This flag can only
    be used with recent HBase versions. If specified, it provides
    a default for all the put and delete operations on this batch.

https://github.com/wbolster/happybase/blob/master/happybase/table.py 460-480行

wal也是一种性能参数。如果没有将操作写入 WAL,它会更快。来自 hbase 文档;

关闭此功能意味着 RegionServer 不会将 Put 写入 Write Ahead Log,而只会写入 memstore,但后果是如果 RegionServer 发生故障,则会丢失数据。

http://hbase.apache.org/0.94/book/perf.writing.html第 11.7.5 节

于 2015-06-01T06:46:34.247 回答