以下是我的用例:我有一个带有散列+范围键的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)'
));
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我使用attribute_not_exists(hash)或者似乎并不重要attribute_not_exists(range).他们似乎都做了完全相同的事情.这是它应该如何工作?
知道如何处理我只想检查hash唯一性的情况吗?
mko*_*bit 43
你不能.DynamoDB中的所有项目都由其hash或hash+ range(取决于您的表)索引.
到目前为止发生了什么的总结:
hash和一个range键PutItem请求,并且必须提供hash和rangeConditionExpressionwithattribute_not_existshashrangeattribute_not_exists条件只是检查是否具有该名称的属性存在,它并不关心价值让我们来看一个例子.让我们从带有这些数据的hash+ rangekey表开始:
hash=A,range=1 hash=A,range=2有四种可能的情况:
如果你试图用hash=A,range=3和放置一个项目attribute_not_exists(hash),那么PutItem将成功,因为attribute_not_exists(hash)评估为true.没有项目存在hash=A,range=3满足条件的键attribute_not_exists(hash).
如果你试图用hash=A,range=3和放置一个项目attribute_not_exists(range),那么PutItem将成功,因为attribute_not_exists(range)评估为true.没有项目存在hash=A,range=3满足条件的键attribute_not_exists(range).
如果您尝试使用hash=A,range=1和放置项目attribute_not_exists(hash),PutItem则会因为attribute_not_exists(hash)求值而失败false.存在具有hash=A,range=1不满足条件的键的项attribute_not_exists(hash).
如果您尝试使用hash=A,range=1和放置项目attribute_not_exists(range),PutItem则会因为attribute_not_exists(range)求值而失败false.存在具有hash=A,range=1不满足条件的键的项attribute_not_exists(range).
这意味着将发生以下两件事之一:
hash+ range对存在于数据库中.
attribute_not_exists(hash) 一定是 trueattribute_not_exists(range) 一定是 truehash+ range对不存在于数据库中.
attribute_not_exists(hash) 一定是 falseattribute_not_exists(range) 一定是 false在这两种情况下,无论是将其放在哈希值还是范围键上,都会得到相同的结果.该hash+ range键标识在整个表中的单个项目,你的病情正在对项目进行评估.
如果具有此hash+ range键的项目尚不存在,您实际上正在执行"放置此项目".
dz9*_*902 11
对于 Google 员工:
attribute_not_exists检查是否与相同的主键的项作为要被插入的项存在attribute_not_existswith主键(或分区键,或范围键),由于键必须存在,check(b)总会通过,只有check(a)有效推理:
attribute_not_exists表明它检查是否存在的属性上的项目注意:要防止新项目替换现有项目,请使用包含 attribute_not_exists 函数的条件表达式,并将属性名称用作表的分区键。由于每条记录都必须包含该属性,因此 attribute_not_exists 函数仅在不存在匹配项时才会成功。
小心保留关键字。
attribute_not_exists如果提供的 attributeName 与保留列表中的单词匹配,则将无法按预期工作。hash和range都是保留的,因此需要使用 来解决该问题ExpressionAttributeNames。
以下示例允许重复的分区键,并且仅当表中已存在具有提供的分区和排序键的项目时才会失败。
$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(#h) AND attribute_not_exists(#r)',
'ExpressionAttributeNames' => array('#h' => 'hash', '#r' => 'range')
));
Run Code Online (Sandbox Code Playgroud)
这将确保指定的分区键hash是唯一的。
$client->putItem(
'TableName' => 'test',
'Item' => array(
'hash' => array('S' => 'abcdefg'),
'range' => array('S' => 'some other value'),
'whatever' => array('N' => 233)
),
'ConditionExpression' => 'attribute_not_exists(#h)',
'ExpressionAttributeNames' => array('#h' => 'hash')
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18973 次 |
| 最近记录: |