标签: aws-sdk-java

如何在 DynamoDB 事务中使用 UpdateItemRequest?

我有一个 Java Spring 应用程序,其中使用 DynamoDB,到目前为止还没有事务。然而,我最近遇到了一个新的用例,它需要将相关对象持久化在一起。我是这个主题的新手,因此阅读过有关 DynamoDB 事务的内容,但找不到以正确的面向对象方式使用 API 的方法。我缺少什么?

现在,当我需要更新对象时,我按如下方式进行操作,构建一个UpdateItemRequest

Map<String, AttributeValueUpdate> updates = new HashMap<>();
// fill updates

Map<String, ExpectedAttributeValue> expected = new HashMap<>();
// fill expectations

UpdateItemRequest request = new UpdateItemRequest()
            .withTableName(TABLE_NAME)
            .withKey(key)
            .withAttributeUpdates(updates)
            .withExpected(expected);
dynamoDBClient.updateItem(request);
Run Code Online (Sandbox Code Playgroud)

但是,构建交易的文档建议使用以下语法:

Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":new_status", new AttributeValue("SOLD"));
expressionAttributeValues.put(":expected_status", new AttributeValue("IN_STOCK"));

Update markItemSold = new Update()
    .withTableName(PRODUCT_TABLE_NAME)
    .withKey(productItemKey)
    .withUpdateExpression("SET ProductStatus = :new_status")
    .withExpressionAttributeValues(expressionAttributeValues)
    .withConditionExpression("ProductStatus = :expected_status")
    .withReturnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure.ALL_OLD);
Run Code Online (Sandbox Code Playgroud)

如果我要遵循文档,那么我需要构建一个update expression- 但我坚持的内容可能非常复杂,这将需要一些繁重的字符串操作,这并不令人满意(要制作的解析器会很慢并且容易出错)。

update expression有没有办法从 a获取这个 …

amazon-dynamodb aws-sdk-java

5
推荐指数
0
解决办法
721
查看次数

使用 DynamoDB 二级索引查询 AWS SDK 2 Java 异常创建 DynamoDbIndex 对象

我在对二级索引运行查询时遇到问题,出现异常:

Ex 获取 dynamodb 扫描:java.lang.IllegalArgumentException:尝试执行需要二级索引的操作,而不定义表元数据中的索引属性。索引名称:category-timestamp-index

有人可以指导我如何做错吗?

我的表是 idIT_RSS_Sources 并且我创建了一个索引类别时间戳索引。

附上索引截图

我的代码是:

DynamoDbEnhancedClient enhancedClient = getEnhancedDBClient(region);

 // Create a DynamoDbTable object

logger.debug("getting RSS Source category-timestamp-index");

//this throws the exception         
DynamoDbIndex<RSS_Source> catIndex = 
        enhancedClient.table("idIT_RSS_Sources", 
        TableSchema.fromBean(RSS_Source.class))
         .index("category-timestamp-index");


                logger.debug("building query attributes");

                AttributeValue att = AttributeValue.builder()
                        .s(theCategory)
                        .build();

                Map<String, AttributeValue> expressionValues = new HashMap<>();
                expressionValues.put(":value", att);

                Expression expression = Expression.builder()
                        .expression("category = :value")
                        .expressionValues(expressionValues)
                        .build();


                // Create a QueryConditional object that's used in the query operation
                QueryConditional queryConditional = QueryConditional
                        .keyEqualTo(Key.builder().partitionValue(theCategory)
                        .build());

                logger.debug("calling catIndex.query in …
Run Code Online (Sandbox Code Playgroud)

amazon-dynamodb aws-sdk-java

2
推荐指数
3
解决办法
2843
查看次数

标签 统计

amazon-dynamodb ×2

aws-sdk-java ×2