HBase:原子'检查行不存在并创建'操作 [英] HBase: atomic 'check row does not exist and create' operation
问题描述
我建议这应该是常见的情况之一,但可能在搜索时使用错误的关键字。
我只需要用完全随机的密钥创建新表记录。假设我获得了具有良好随机性的密钥(几乎是随机的)。然而,我不能100%确定没有行存在。所以我需要做自动处理:
- 具有行键检查没有行存在。
- 如果存在行,则拒绝操作。
- 如果未退出,则创建行。 我在这个主题上找到的一条信息是关于的文章。
我将HBase行锁看作是合适的解决方案,但我希望在没有显式行锁定的情况下以更好的方式实现。
- ICV看起来不合适因为我确实希望密钥是随机的。
- 如果CAS可以处理'行不存在'的情况,但它看起来不行。 b $ b
- 显式行锁具有类似于区域拆分问题的缺点。
有人请添加有用的建议吗?
解决方案
优选的API是基于Java的,但实际上它更多是关于概念而不是实现。
对于这种情况恰好基于 checkAndPut() code>方法
。我打算做的是带密钥重复检查的新行插入和单个插入解决方案是完美的:
-
HTable checkAndPut()
方法可以检查某个列未设置(检查它是否为null
值)。
- 由于行无论如何都包含一些对所有
对象强制使用的'ID'字段(您可以使用任何其他字段,您总是为您的
对象设置),因此可以检查行是否存在。
传入
checkAndPut()
的对象将包含初始
对象状态,必填字段集。
- 由于行无论如何都包含一些对所有
好的,对于批量插入(我真正需要的),它碰巧太慢了, UUID用作行键而不对新行插入进行任何检查。对我来说,它好多了。在这种情况下唯一考虑的是真正好的随机生成器。标准Java java.util.UUID
类包含我需要的所有东西,包括它基于稍慢但非常强大 java.security.SecureRandom
生成器。
请注意:看起来HBase用户行锁定功能将由于与其使用相关的安全性/其他风险而被删除。 >
I suggest this should be one of common cases but probably I use wrong keywords when googling around.
I just need to create new table record with completely random key. Assume I obtained key with good randomness (almost random). However I can't be 100% sure no row yet exists. So what I need to do atomically:
- Having row key check no row exists yet.
- Reject operation if row exists.
- Create row if it does not exit.
Most useful piece of information I found on this topic is article about HBase row locks. I see HBase row locks as suitable solution but I'd like to do it better way without explicit row locking.
- ICV looks not suitable because I really do want key to be random.
- CAS would be great if they could work on 'row does not exists' condition but it looks they can't.
- Explicit row locks have disadvantages like issues on region split.
Could somebody please add useful advice? Preferable API is Java based but actually it is more about concept rather than implementation.
'Good enough' solution for this case happened to be based on checkAndPut()
method. What I intended to do is new row insertion with key duplication check and for individual inserts solution is perfect:
HTable checkAndPut()
method can check certain column is not set (check it fornull
value).- As rows anyway contain some 'ID' field which is mandatory for all objects (you can use any other field that you always set for your object) it is possible to check if row exists.
Put
object passed tocheckAndPut()
is to contain initial object state with mandatory field set.
Well, for bulk insertion (what I really needed) it happened to be too slow so I moved to UUID used as row keys without any checks on new row insertion. For me it is much better. The only consideration in this case is really good random generator. Standard Java java.util.UUID
class contains everything I need including it is based on somewhat slow but pretty strong java.security.SecureRandom
generator.
Just note: it looks like HBase user row locking feature is going to be dropped due to security / other risks related to its usage.
这篇关于HBase:原子'检查行不存在并创建'操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!