happybase和hbase的原子批处理插入

ggazkfy8  于 2021-06-09  发布在  Hbase
关注(0)|答案(2)|浏览(578)

使用python中的happybase api for hbase,可以通过以下方式执行批插入:

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() 方法需要 transaction 以及 wal 作为参数。在批处理中途失败的情况下,是否可以将这些配置为回滚成功保存的行?
happybase是否会在这里抛出一个异常,允许我记录行键并执行批删除?

k5ifujac

k5ifujac1#

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

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写入预写日志,只会写入memstore,但是其结果是,如果regionserver出现故障,则会丢失数据。
http://hbase.apache.org/0.94/book/perf.writing.html 第11.7.5节

0ve6wy6x

0ve6wy6x2#

你是否遵循了happybase文档中关于批量突变的教程?你好像把一些东西弄混了。https://happybase.readthedocs.org/en/latest/user.html#performing-批量突变
批处理纯粹是一种性能优化:它们避免了为存储/删除的每一行往返到thrift服务器,这可能会导致显著的加速。
上下文管理器行为 with block)是一个纯粹的客户端便利api,它使应用程序代码更易于编写和推理,如上面链接的用户指南中的许多示例所述。如果 with 块成功完成所有突变都会一次性发送到服务器。
然而。。。这是唯一的幸福之路。如果某个python异常从 with 阻止?这就是 transaction 旗帜进场:如果 True ,则不向服务器发送任何数据,如果 False ,任何挂起的数据都将被刷新。哪种行为更受欢迎很大程度上取决于您的用例。

相关问题