happybase和hbase的原子批处理插入

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

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

  1. import happybase
  2. connection = happybase.Connection()
  3. table = connection.table('table-name')
  4. batch = table.batch()
  5. # put several rows to this batch via batch.put()
  6. batch.send()

如果这批货半途而废,会发生什么情况?已保存的行是否仍保存,未保存的行是否仍保存?
我在happybase github中注意到 table.batch() 方法需要 transaction 以及 wal 作为参数。在批处理中途失败的情况下,是否可以将这些配置为回滚成功保存的行?
happybase是否会在这里抛出一个异常,允许我记录行键并执行批删除?

k5ifujac

k5ifujac1#

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

  1. The `transaction` argument specifies whether the returned
  2. :py:class:`Batch` instance should act in a transaction-like manner when
  3. used as context manager in a ``with`` block of code. The `transaction`
  4. flag cannot be used in combination with `batch_size`.
  5. The `wal` argument determines whether mutations should be
  6. written to the HBase Write Ahead Log (WAL). This flag can only
  7. be used with recent HBase versions. If specified, it provides
  8. 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 ,任何挂起的数据都将被刷新。哪种行为更受欢迎很大程度上取决于您的用例。

相关问题