Java DynamoDB -- 仅在键不存在时插入(没有映射器)
Java DynamoDB -- Only insert if key not already present (without mapper)
如果键不存在,我只想插入这一行。如果键已经存在,我不想覆盖该行。
我的语法是这样的:
new PutItemRequest().withTableName(myTableName).withItem(myItem).withConditionExpression(?)
根据 the AWS docs,我会使用属性 ATTRIBUTE_NOT_EXISTS 之类的东西。我也可以用ComparisonOperator.NULL,等等。这是我能理解的。
语法提示?对此 withConditionExpression 机制的一些解释?
文档说 ConditionExpression
替换了遗留的 ConditionalOperator
(.NULL
)。你可以走两条路线,但你应该使用 .withConditionExpression(...)
而不是操作数路线。
还有 https://java.awsblog.com/post/TxBG87QOQZRZJF/DynamoDB-XSpec-API 用于更复杂的表达式。
但在你的情况下,它应该通过写
来工作
.withConditionExpression("attribute_not_exists(thingId)")
假设您的哈希键 属性 被命名为 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.
我们可以将 Dynamo Db 主键定义为(分区键和排序键)的组合。基于这个假设,这段代码会丢弃重复的记录。
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 ");
}
如果键不存在,我只想插入这一行。如果键已经存在,我不想覆盖该行。
我的语法是这样的:
new PutItemRequest().withTableName(myTableName).withItem(myItem).withConditionExpression(?)
根据 the AWS docs,我会使用属性 ATTRIBUTE_NOT_EXISTS 之类的东西。我也可以用ComparisonOperator.NULL,等等。这是我能理解的。
语法提示?对此 withConditionExpression 机制的一些解释?
文档说 ConditionExpression
替换了遗留的 ConditionalOperator
(.NULL
)。你可以走两条路线,但你应该使用 .withConditionExpression(...)
而不是操作数路线。
还有 https://java.awsblog.com/post/TxBG87QOQZRZJF/DynamoDB-XSpec-API 用于更复杂的表达式。
但在你的情况下,它应该通过写
来工作.withConditionExpression("attribute_not_exists(thingId)")
假设您的哈希键 属性 被命名为 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.
我们可以将 Dynamo Db 主键定义为(分区键和排序键)的组合。基于这个假设,这段代码会丢弃重复的记录。
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 ");
}