提问者:小点点

DynamoDB-DynamoDb原子计数器返回始终为空


我遵循这个https://aws.amazon.com/blogs/developer/using-atomic-counters-in-the-enhanced-dynamodb-aws-sdk-for-java-2-x-client/的目标是使用原子计数器生成唯一的ID,但我总是得到null这两个例子:

我的代码如下所示:

@DynamoDbBean
public class CustomerDocument {

    private String root;
    private Long updateCounter;
    private Long customCounter;

    @DynamoDbPartitionKey
    public String getRoot() {
        return this.root;
    }

    public void setRoot(String id) {
        this.root = id;
    }

    @DynamoDbAtomicCounter
    public Long getUpdateCounter() {
        return this.updateCounter;
    }

    public void setUpdateCounter(Long counter) {
        this.updateCounter = counter;
    }

    @DynamoDbAtomicCounter(delta = 5, startValue = 10)
    public Long getCustomCounter() {
        return this.customCounter;
    }

    public void setCustomCounter(Long counter) {
        this.customCounter = counter;
    }
}

存储库

CustomerDocument document = new CustomerDocument();
document.setRoot(root);

customerTable.updateItem(document);
CustomerDocument retrievedCustomer = customerTable.getItem(document);

retrievedCustomer.getUpdateCounter(); // null
retrievedCustomer.getCustomCounter(); // null

customerTable.updateItem(document);

retrievedCustomer = orderCounterTable.getItem(document);
retrievedCustomer.getUpdateCounter(); // null
retrievedCustomer.getCustomCounter(); // null

对这个问题有什么想法吗?


共1个答案

匿名用户

只要您正确设置代码,此功能就可以正常工作。我们有一个使用DynamoDb原子计数器的无服务器照片资产管理应用程序。

在此示例中,前端是一个React应用程序。后端使用AWSSDKJavaV2。每次应用程序使用Amazon Rekognition找到新标签时,计数列都会使用DynamoDbAnalyicCounter递增。这是ReactUI。您可以看到每个标签的标签计数。

这是使其工作的适用代码。

这是使用必要注释的DynamoDB类——例如@DynamoDbAnalyicCounter。

    package com.example.photo;
    
    import software.amazon.awssdk.enhanced.dynamodb.extensions.annotations.DynamoDbAtomicCounter;
    import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
    import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
    import java.util.List;
    
    @DynamoDbBean
    public class Label {
        private String id;
        private Integer count;
        private List<String> images;
    
        @DynamoDbPartitionKey
        public String getId() {
            return this.id;
        };
        public void setId(String id) {
            this.id = id;
        }
    
        @DynamoDbAtomicCounter (startValue = 1)
        public Integer getCount() {
            return this.count;
        }
        public void setCount(Integer count) {
            this.count = count;
        }
    
        public List<String> getImages() {
            return this.images;
        }
        public void setImages(List<String> images) {
            this.images = images;
        }
    }

以下是与增强型客户端一起使用的方法:

private void addSingleRecord(DynamoDbTable<Label> table, String tag, String key) {
        // Check to see if the label exists in the Amazon DynamoDB table.
        // The count item uses an @DynamoDbAtomicCounter which means it is
        // updated automatically. No need to manually set this value when the record is
        // created or updated.
        if (!checkTagExists(table, tag)) {
            Label photoRec = new Label();
            photoRec.setId(tag);
            List<String> keyList = new ArrayList<>();
            keyList.add(key);
            photoRec.setImages(keyList);
            table.putItem(photoRec);
        } else {
            // The tag exists in the table.
            Key myKey = Key.builder()
                .partitionValue(tag)
                .build();

            // Add the file name to the list.
            Label myPhoto = table.getItem(myKey);
            Label updatedPhoto = new Label();
            List<String> imageList = myPhoto.getImages();
            imageList.add(key);
            updatedPhoto.setId(tag);
            updatedPhoto.setImages(imageList);
            table.updateItem(updatedPhoto);
        }
    }

请注意,这里有一个技巧——来自SDK团队:

“我知道发生了什么。这是因为我们正在重用我们从db中读取的带有‘getItem’的相同对象。如果您创建一个新对象并设置数据,它就可以工作。”

这可能是您问题的原因。创建一个新对象,增量就会发生。

您可以在AWS代码库中找到这个完整的示例。您可以编写后端代码并使用AWSCDK来独立存储资源并运行此应用程序。按照文档中的说明并按照CDK说明进行操作。按照所有操作后,您将看到应用程序在运行中以及正在运行的DynamoDb原子计数器。

见:

创建一个照片资产管理应用程序,让用户使用标签管理照片