【问题标题】:Working with DynamoDB's PutItem ConditionExpression in Java (avoid replacement of attributes)在 Java 中使用 DynamoDB 的 PutItem ConditionExpression(避免替换属性)
【发布时间】:2014-11-06 18:01:14
【问题描述】:

我有一个不包含任何唯一键(主键)的数据集。只有当所有行合并时,它才是唯一的。但是,我在 dynamoDB 中创建表的键中给出了第一个属性(类类型),如下所示:

    CreateTableRequest createTableRequest = new CreateTableRequest()
        .withTableName(tableName)
        .withKeySchema(newKeySchemaElement()
        .withAttributeName("Class type")
        .withKeyType(KeyType.HASH))
.withAttributeDefinitions(newAttributeDefinition().withAttributeName("Class type").withAttributeType(ScalarAttributeType.S))
            ...
            ...;

对于这个数据集,PutItem 不起作用,因为它用另一个属性替换了一个属性,因为“类类型”不是唯一的。我希望所有行都上传到 dynamoDB。我的 putItem 如下所示:

Map<String, AttributeValue> item = newItem(col[0], col[1], col[2], col[3], titanic_col[4]);
PutItemRequest putItemRequest = new PutItemRequest(tableName, item).withConditionExpression("Class type = NULL"); 
// No error in the above line. But getting exception.
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

条件表达式行出现异常:

Caught an AmazonServiceException, which means your request made it to AWS, but was rejected with an error response for some reason.
Error Message:    Invalid ConditionExpression: Syntax error; token: "type", near: "Class type =" (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:  J51GM61PM4AE2HPD0DVMQ6TNU7VV4KQNSO5AEMVJF66Q9ASUAAJG)
HTTP Status Code: 400
AWS Error Code:   ValidationException
Error Type:       Client

我想知道语法是否正确。如果不是,请提供正确的语法来解决问题(避免替换属性)。

【问题讨论】:

    标签: java validation amazon-web-services amazon-dynamodb conditional-operator


    【解决方案1】:
    1. 是的,存在语法错误。 “Class type”这两个词之间有一个空格。解析器不知道这一点,除非你告诉它。要让解析知道“类类型”是单个属性,请参阅expression attribute name
    2. 您需要一个唯一的 ID,即使是 uuid 也可以使用

    【讨论】:

    • 是的。我已经更改了我的属性名称(没有空格)。此外,我在我的数据集中创建了一个唯一的键值属性。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多