【问题标题】:how to form composite unique key in dynamo db如何在 dynamo db 中形成复合唯一键
【发布时间】:2016-02-11 02:33:18
【问题描述】:

我在 dynamo DB 中有一个表,其中包含字段 A、B、C、D 和 E。 主键是A(分区键)和B(排序键)。

我想将另一个唯一约束 C 和 D 一起作为复合唯一键。在mysql中我会做这样的事情

ALTER TABLE YourTable
 add CONSTRAINT YourTable_unique UNIQUE (C, D);

我想在 dynamo DB 中做类似的事情,这样当我使用已经匹配的复合唯一键(C 和 D)创建新条目时,它不允许我创建该条目。

【问题讨论】:

  • 我怀疑 DynamoDB 中是否提供此功能,但您可以在应用程序级别执行此操作

标签: amazon-dynamodb


【解决方案1】:

来自documentation

To write an item only if it doesn't already exist,
use PutItem with a conditional expression that uses the 
attribute_not_exists function and the name of the table's 
partition key

你不能在其他键上添加约束然后分区键(你不能在全局辅助键上添加约束)

【讨论】:

  • 我采取了一种类似的方法,不确定它是否是最好的方法。我在保存之前进行了扫描。如果扫描结果为空,我保存记录,否则我跳过它。
  • 很好,我也用这种方法
【解决方案2】:

@Eyal Ch 是对的。我在应用程序级别处理它,在保存之前进行扫描:

DynamoDBScanExpression expr = new DynamoDBScanExpression();
expr.addFilterCondition("C",new Condition()
                        .withComparisonOperator(ComparisonOperator.EQ)
                        .withAttributeValueList(new AttributeValue().withS("value of C")));
expr.addFilterCondition("D",new Condition()
                        .withComparisonOperator(ComparisonOperator.EQ)
                        .withAttributeValueList(new AttributeValue().withS("value of D")));
 List<TableClass> responseList = mapper.scan(TableClass.class, expr);
 if (responseList.size() == 0){
 ........//save the record here
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2018-07-29
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2022-01-13
    相关资源
    最近更新 更多