Java DynamoDB——仅在键不存在时插入(无映射器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32705958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:38:35  来源:igfitidea点击:

Java DynamoDB -- Only insert if key not already present (without mapper)

javaamazon-dynamodb

提问by Jeremy

I only want to insert this row if the key is not present. I don't want to override the row if the key already exists.

如果键不存在,我只想插入这一行。如果键已经存在,我不想覆盖该行。

My syntax is this:

我的语法是这样的:

new PutItemRequest().withTableName(myTableName).withItem(myItem).withConditionExpression(?)

As per the AWS docs, I'd use something like attribute ATTRIBUTE_NOT_EXISTS. I could also use ComparisonOperator.NULL, etc. That's as far as I can understand.

根据AWS docs,我会使用类似属性 ATTRIBUTE_NOT_EXISTS 的东西。我也可以使用ComparisonOperator.NULL 等。这是我所能理解的。

Syntax tips? Some explanation of this withConditionExpression mechanism?

语法提示?用ConditionExpression机制对此进行一些解释?

采纳答案by zapl

The doc says ConditionExpressionreplaces the legacy ConditionalOperator(.NULL). You can go both routes but you're supposed to use .withConditionExpression(...)rather then the operand route.

文档说ConditionExpression替换了旧版ConditionalOperator( .NULL)。你可以走两条路线,但你应该使用.withConditionExpression(...)而不是操作数路线。

There is also https://java.awsblog.com/post/TxBG87QOQZRZJF/DynamoDB-XSpec-APIfor more complex expressions.

还有用于更复杂表达式的https://java.awsblog.com/post/TxBG87QOQZRZJF/DynamoDB-XSpec-API

But in your case it should work by writing

但在你的情况下,它应该通过写作来工作

.withConditionExpression("attribute_not_exists(thingId)")

assuming your hash key property is named thingId. That approach is also documented here: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

假设您的哈希键属性名为thingId. 该方法也记录在此处:http: //docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the HASH key for the table. Since every record must contain that attribute, the attribute_not_exists function will only succeed if no matching item exists.

要防止新项目替换现有项目,请使用包含 attribute_not_exists 函数的条件表达式,该函数的名称用作表的 HASH 键。由于每条记录都必须包含该属性,因此 attribute_not_exists 函数仅在不存在匹配项时才会成功。

回答by Sindhu

We can define Dynamo Db primary Key as combination of (partition key and sort key) . based on this assumption ,this code would throw away duplicate records .

我们可以将 Dynamo Db 主键定义为 (partition key and sort key) 的组合。基于这个假设,这段代码会丢弃重复的记录。

PrimaryKey primaryKey = new PrimaryKey();
primaryKey.addComponent("Partition_Key", partitionId);
primaryKey.addComponent("Sort_Key",sortId);

    Item item = new Item().withPrimaryKey(primaryKey).withString("username", userName).withLong("time", time);

    try {

        PutItemSpec putItemSpec = new PutItemSpec().withItem(item).withConditionExpression("attribute_not_exists(Sort_Key)");
        table.putItem(putItemSpec);

    } catch(ConditionalCheckFailedException ex) {
         logger.error("Record already exists in Dynamo DB Table ");

    }