以下是我的用例:我有一个带有哈希+范围键的 Dynamo 表。当我将新项目放入表中时,我想进行唯一性检查。有时我想保证哈希值是唯一的(忽略范围)。其他时候我想允许重复的哈希值,但保证哈希值和范围的组合是唯一的。我怎样才能做到这一点?
我尝试了 attribute_not_exists。它似乎处理第二种情况,它检查哈希+键组合。这是一个 PHP 示例:
$client->putItem(array(
'TableName' => 'test',
'Item' => array(
'hash' => array('S' => 'abcdefg'),
'range' => array('S' => 'some other value'),
'whatever' => array('N' => 233)
),
'ConditionExpression' => 'attribute_not_exists(hash)'
));
奇怪的是,如果我使用似乎并不重要attribute_not_exists(hash)
or attribute_not_exists(range)
。他们似乎都在做同样的事情。这是它应该如何工作的吗?
知道如何处理我只想检查的情况hash
为了独特性?
你不能。 DynamoDB 中的所有项目均按其索引hash
or hash
+range
(取决于您的桌子)。
到目前为止发生的事情的总结:
- 单个哈希键可以有多个范围键。
- 每个项目都有一个
hash
and a range
key
- 你正在制作一个
PutItem
请求和must提供两个hash
and range
- 您正在提供一个
ConditionExpression
with attribute_not_exists
在任一hash
or range
属性名称
- The
attribute_not_exists
条件只是检查具有该名称的属性是否存在,它不关心值
让我们来看一个例子。让我们从一个hash
+range
包含此数据的关键表:
-
hash=A,range=1
hash=A,range=2
有四种可能的情况:
如果您尝试将一个项目与hash=A,range=3
and attribute_not_exists(hash)
, the PutItem
会成功,因为attribute_not_exists(hash)
评估为true
。不存在带有密钥的项目hash=A,range=3
满足条件attribute_not_exists(hash)
.
如果您尝试将一个项目与hash=A,range=3
and attribute_not_exists(range)
, the PutItem
会成功,因为attribute_not_exists(range)
评估为true
。不存在带有密钥的项目hash=A,range=3
满足条件attribute_not_exists(range)
.
如果您尝试将一个项目与hash=A,range=1
and attribute_not_exists(hash)
, the PutItem
会失败,因为attribute_not_exists(hash)
评估为false
。存在带有键的项目hash=A,range=1
则不满足条件attribute_not_exists(hash)
.
如果您尝试将一个项目与hash=A,range=1
and attribute_not_exists(range)
, the PutItem
会失败,因为attribute_not_exists(range)
评估为false
。存在带有键的项目hash=A,range=1
则不满足条件attribute_not_exists(range)
.
这意味着将会发生以下两种情况之一:
- The
hash
+range
pair exists in the database.
-
attribute_not_exists(hash)
必须是true
-
attribute_not_exists(range)
必须是true
- The
hash
+range
pair does not exist in the database.
-
attribute_not_exists(hash)
必须是false
-
attribute_not_exists(range)
必须是false
在这两种情况下,无论将其放在散列键还是范围键上,都会得到相同的结果。这hash
+range
键标识整个表中的单个项目,并且正在根据该项目评估您的条件。
您正在有效地执行“如果有此项目,则放置此项目hash
+range
密钥尚不存在”.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)