DynamoDB-如果哈希(或哈希和范围组合)不存在,则放置项目 [英] DynamoDB - Put item if hash (or hash and range combination) doesn't exist
问题描述
这是我的用例:我有一个带有哈希+范围键的Dynamo表。当我在表中放置新项目时,我想进行一次唯一性检查。有时我想保证哈希是唯一的(忽略范围)。其他时候我想允许重复的哈希,但是要保证哈希和范围组合是唯一的。我该怎么做?
我尝试了attribute_not_exists。似乎可以处理第二种情况,即检查哈希+组合键。这是一个PHP示例:
$ client-&put; putItem(array(
' TableName'=>'test',
'Item'=> array(
'hash'=> array('S'=>'abcdefg'),
'range '=> array('S'=>'一些其他值'),
'无论'=> array('N'=> 233)
),
' ConditionExpression'=>'attribute_not_exists(hash)'
));
奇怪的是,我使用 attribute_not_exists(hash)似乎并不重要)
或 attribute_not_exists(范围)
。他们似乎都做着完全相同的事情。
任何想法如何处理我只想检查 hash
的情况
不能。 DynamoDB中的所有项目都按其散列
或散列
+ 范围$ c进行索引$ c>(取决于您的表)。
到目前为止的情况的摘要:
- 单个哈希键可以具有多个范围键。
- 每个项目都具有
hash
和范围
键 - 您正在发出
PutItem
请求,并必须同时提供哈希
和范围
- 在
散列
或<上提供ConditionExpression
和attribute_not_exists
code> range 属性名称 -
attribute_not_exists
条件仅是检查是否具有该属性名称存在,它不在乎值
我们来看一个示例。让我们从 hash
+ range
键表开始,并获取以下数据:
-
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(范围)
,PutItem
将成功,因为attribute_not_exists(range)
计算结果为true
。项hash = A,range = 3
满足attribute_not_exists(range)
条件的项不存在。 -
如果您尝试放置带有
hash = A的项目,范围= 1
和attribute_not_exists(哈希)
,PutItem
将失败,因为attribute_not_exists(hash)
计算为false
。存在具有键hash = A,range = 1
的项目,该项目不满足attribute_not_exists(hash)
的条件。 -
如果您尝试放置带有
hash = A,range = 1
和<$ c的项目$ c> attribute_not_exists(range),PutItem
将失败,因为attribute_not_exists(range)
计算结果为false
。存在具有键hash = A,range = 1
的项目,该项目不满足attribute_not_exists(range)
的条件。
这意味着将发生以下两种情况之一:
- 数据库中存在
hash
+range
对。
-
attribute_not_exists(hash)
必须为true
-
attribute_not_exists(range)
必须为true
-
-
哈希
+范围
对数据库中不存在。
-
attribute_not_exists(hash)
必须为false
-
attribute_not_exists(range)
必须为false
-
在两种情况下,无论将其放在哈希表还是范围键。 hash
+ range
键在整个表中标识单个项目,并且您的条件正在对该项目进行评估。
如果某项具有哈希值
+ <$ c,则表示您正在有效地执行放入该项$ c> range 键不存在。
Here are my use cases: I have a Dynamo table with a hash + range key. When I put new items in the table, I want to do a uniqueness check. Sometimes I want to guarantee that the hash is unique (ignoring the range). Other times I want to allow duplicate hashes, but guarantee that the hash and range combination is unique. How can I accomplish this?
I experimented with attribute_not_exists. It seems to handle the second case, where it checks the hash + key combination. Here's a PHP sample:
$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)'
));
Oddly, it doesn't seem to matter if I use attribute_not_exists(hash)
or attribute_not_exists(range)
. They both seem to do exactly the same thing. Is this how it's supposed to work?
Any idea how to handle the case where I only want to check hash
for uniqueness?
You can't. All items in DynamoDB are indexed by either their hash
or hash
+range
(depending on your table).
A sort of summary of what is going on so far:
- A single hash key can have multiple range keys.
- Every item has both a
hash
and arange
key - You are making a
PutItem
request and must provide both thehash
andrange
- You are providing a
ConditionExpression
withattribute_not_exists
on either thehash
orrange
attribute name - The
attribute_not_exists
condition is merely checking if an attribute with that name exists, it doesn't care about the value
Let's walk through an example. Let's start with a hash
+range
key table with this data:
hash=A,range=1
hash=A,range=2
There are four possible cases:
If you try to put an item with
hash=A,range=3
andattribute_not_exists(hash)
, thePutItem
will succeed becauseattribute_not_exists(hash)
evaluates totrue
. No item exists with keyhash=A,range=3
that satisfies the condition ofattribute_not_exists(hash)
.If you try to put an item with
hash=A,range=3
andattribute_not_exists(range)
, thePutItem
will succeed becauseattribute_not_exists(range)
evaluates totrue
. No item exists with keyhash=A,range=3
that satisfies the condition ofattribute_not_exists(range)
.If you try to put an item with
hash=A,range=1
andattribute_not_exists(hash)
, thePutItem
will fail becauseattribute_not_exists(hash)
evaluates tofalse
. An item exists with keyhash=A,range=1
that does not satisfy the condition ofattribute_not_exists(hash)
.If you try to put an item with
hash=A,range=1
andattribute_not_exists(range)
, thePutItem
will fail becauseattribute_not_exists(range)
evaluates tofalse
. An item exists with keyhash=A,range=1
that does not satisfy the condition ofattribute_not_exists(range)
.
This means that one of two things will happen:
- The
hash
+range
pair exists in the database.attribute_not_exists(hash)
must betrue
attribute_not_exists(range)
must betrue
- The
hash
+range
pair does not exist in the database.attribute_not_exists(hash)
must befalse
attribute_not_exists(range)
must befalse
In both cases, you get the same result regardless of whether you put it on the hash or the range key. The hash
+range
key identifies a single item in the entire table, and your condition is being evaluated on that item.
You are effectively performing a "put this item if an item with this hash
+range
key does not already exist".
这篇关于DynamoDB-如果哈希(或哈希和范围组合)不存在,则放置项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!