DynamoDB 不支持 PutItem 请求中的条件表达式

发布于 2025-01-11 10:15:20 字数 1761 浏览 0 评论 0原文

我正在尝试有条件地将项目写入 DynamoDB 表。

我正在使用 适用于 Java 2.x 的 AWS 开发工具包。下面是我的代码:

PutItemEnhancedRequest<AssessmentBean> putItemRequest =
    PutItemEnhancedRequest.builder(AssessmentBean.class)
        .conditionExpression(sqsMsgIdNoMatch)
        .item(assessment)
        .build();
WriteBatch batch =
    WriteBatch.builder(AssessmentBean.class)
        .addPutItem(putItemRequest)
        .mappedTableResource(table)
        .build(); // This line throws the IllegalArgumentException
BatchWriteItemEnhancedRequest batchWriteRequest =
    BatchWriteItemEnhancedRequest.builder().addWriteBatch(batch).build();

eDDBClient
    .batchWriteItem(batchWriteRequest)
    .whenCompleteAsync(/* not important to this question */);

如上面代码中的注释所示,我的代码抛出一个 IllegalArgumentException

java.lang.IllegalArgumentException:映射器扩展将条件表达式插入 PutItem 请求中,作为 BatchWriteItemRequest 的一部分。 DynamoDb 不支持此功能。已知执行此操作的扩展是 VersionedRecordExtension,除非被覆盖,否则默认情况下会加载该扩展。要解决此问题,请使用其中没有版本化属性的表架构,或者不加载有问题的扩展。

以下是一些类型信息:

  • putItemRequest 是一个 software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest 对象
  • batch 是一个 software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch 对象
  • batchWriteRequest 是一个software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest 对象
  • eDDBClientsoftware.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient 对象

如何有条件地将项目正确写入 DynamoDB?

I am trying to conditionally write an item to a DynamoDB table.

I am using AWS SDK for Java 2.x. Below is my code:

PutItemEnhancedRequest<AssessmentBean> putItemRequest =
    PutItemEnhancedRequest.builder(AssessmentBean.class)
        .conditionExpression(sqsMsgIdNoMatch)
        .item(assessment)
        .build();
WriteBatch batch =
    WriteBatch.builder(AssessmentBean.class)
        .addPutItem(putItemRequest)
        .mappedTableResource(table)
        .build(); // This line throws the IllegalArgumentException
BatchWriteItemEnhancedRequest batchWriteRequest =
    BatchWriteItemEnhancedRequest.builder().addWriteBatch(batch).build();

eDDBClient
    .batchWriteItem(batchWriteRequest)
    .whenCompleteAsync(/* not important to this question */);

As indicated by the comment in the code above, my code throws an IllegalArgumentException:

java.lang.IllegalArgumentException: A mapper extension inserted a conditionExpression in a PutItem request as part of a BatchWriteItemRequest. This is not supported by DynamoDb. An extension known to do this is the VersionedRecordExtension which is loaded by default unless overridden. To fix this use a table schema that does not have a versioned attribute in it or do not load the offending extension.

Below is some type information:

  • putItemRequest is a software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest object
  • batch is a software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch object
  • batchWriteRequest is a software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest object
  • eDDBClient is a software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient object

How do correctly conditionally write an item to DynamoDB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文