DynamoDB-如果哈希(或哈希和范围组合)不存在,则放置项目 [英] DynamoDB - Put item if hash (or hash and range combination) doesn't exist

查看:111
本文介绍了DynamoDB-如果哈希(或哈希和范围组合)不存在,则放置项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的用例:我有一个带有哈希+范围键的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中的所有项目都按其散列散列 + 范围(取决于您的表)。



到目前为止的情况的摘要:




  • 单个哈希键可以具有多个范围键。

  • 每个项目都具有 hash 范围

  • 您正在发出 PutItem 请求,并必须同时提供哈希范围

  • 散列或<上提供 ConditionExpression attribute_not_exists code> range 属性名称

  • attribute_not_exists 条件仅是检查是否具有该属性名称存在,它不在乎值



我们来看一个示例。让我们从 hash + range 键表开始,并获取以下数据:


  1. hash = A,range = 1

  2. hash = A,range = 2

有四种可能的情况:


  1. 如果尝试放置带有 hash = A,range = 3 attribute_not_exists(hash), PutItem 将成功,因为 attribute_not_exists(hash)计算为 true 。项 hash = A,range = 3 的键满足 attribute_not_exists(hash)的条件。


  2. 如果尝试放置带有 hash = A,range = 3 attribute_not_exists(范围), PutItem 将成功,因为 attribute_not_exists(range)计算结果为 true 。项 hash = A,range = 3 满足 attribute_not_exists(range)条件的项不存在。


  3. 如果您尝试放置带有 hash = A的项目,范围= 1 attribute_not_exists(哈希) PutItem 将失败,因为 attribute_not_exists(hash)计算为 false 。存在具有键 hash = A,range = 1 的项目,该项目不满足 attribute_not_exists(hash)的条件。


  4. 如果您尝试放置带有 hash = A,range = 1 和<$ c的项目$ c> attribute_not_exists(range), PutItem 将失败,因为 attribute_not_exists(range)计算结果为 false 。存在具有键 hash = A,range = 1 的项目,该项目不满足 attribute_not_exists(range)的条件。


这意味着将发生以下两种情况之一:


  1. 数据库中存在 hash + range 对。


    • attribute_not_exists(hash)必须为 true

    • attribute_not_exists(range)必须为 true


  2. 哈希 + 范围对数据库中不存在。


    • 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 a range key
  • You are making a PutItem request and must provide both the hash and range
  • You are providing a ConditionExpression with attribute_not_exists on either the hash or range 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:

  1. hash=A,range=1
  2. hash=A,range=2

There are four possible cases:

  1. If you try to put an item with hash=A,range=3 and attribute_not_exists(hash), the PutItem will succeed because attribute_not_exists(hash) evaluates to true. No item exists with key hash=A,range=3 that satisfies the condition of attribute_not_exists(hash).

  2. If you try to put an item with hash=A,range=3 and attribute_not_exists(range), the PutItem will succeed because attribute_not_exists(range) evaluates to true. No item exists with key hash=A,range=3 that satisfies the condition of attribute_not_exists(range).

  3. If you try to put an item with hash=A,range=1 and attribute_not_exists(hash), the PutItem will fail because attribute_not_exists(hash) evaluates to false. An item exists with key hash=A,range=1 that does not satisfy the condition of attribute_not_exists(hash).

  4. If you try to put an item with hash=A,range=1 and attribute_not_exists(range), the PutItem will fail because attribute_not_exists(range) evaluates to false. An item exists with key hash=A,range=1 that does not satisfy the condition of attribute_not_exists(range).

This means that one of two things will happen:

  1. The hash+range pair exists in the database.
    • attribute_not_exists(hash) must be true
    • attribute_not_exists(range) must be true
  2. The hash+range pair does not exist in the database.
    • attribute_not_exists(hash) must be false
    • attribute_not_exists(range) must be false

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆