简体   繁体   English

如何在AWS DynamoDB中处理空的java字符串集

[英]How do I handle an empty java set of strings in AWS DynamoDB

I'm trying to store an array of strings within an AWS DynamoDB table. 我正在尝试在AWS DynamoDB表中存储一个字符串数组。 For the most part this array will be populated with at least one string. 在大多数情况下,此数组将填充至少一个字符串。 However there is the case where the array could be empty. 但是,存在阵列可能为空的情况。

I've created a DynamoDB model in a Java Lambda function that has a set of strings as one of it's properties. 我在Java Lambda函数中创建了一个DynamoDB模型,该函数有一组字符串作为其中一个属性。 If I try to save a DynamoDB model when the set of strings is empty it gives me an error saying I can't store an empty set in DynamoDB. 如果我在字符串集为空时尝试保存DynamoDB模型,则会给出一个错误,指出我无法在DynamoDB中存储空集。

So my question is, how would handle removing that set property when it's empty from my model before I save / update it in the DynamoDB? 所以我的问题是,在我在DynamoDB中保存/更新它之前,如何从我的模型中删除该set属性?

Here's an example of the model. 这是模型的一个例子。

@DynamoDBTable(tableName = "group")
public class Group {
    private String _id;
    private Set<String> users;

    @Null
    @DynamoDBHashKey
    @DynamoDBAutoGeneratedKey
    public String getId() {
        return _id;
    }

    public void setId(final String id) {
        _id = id;
    }

    @DynamoDBAttribute
    public Set<String> getUsers(){
        return users;
    }

    public void setUsers(final Set<String> users) {
        this.users = users;
    }

    public void addUser(String userId) {
        if(this.users == null){
            this.setUsers(new HashSet<String>(Arrays.asList(userId)));
        }else{
            this.getUsers().add(userId);
        }
   }
}

First time when I will create a group. 我第一次创建一个小组。 It could have no user, or it could have one or more user. 它可能没有用户,也可能有一个或多个用户。

This is somewhat of an old question but the way I would solve this problem is with a custom DynamoDBMarshaller . 这有点老问题,但我解决这个问题的方法是使用自定义DynamoDBMarshaller

Making use of the @DynamoDBMarshalling annotation, you can decorate the POJO accessor methods in order to dictate to the DynamoDB mapper which marshaller class to use to serialize and deserialize the set of strings. 利用@DynamoDBMarshalling注释,您可以修饰POJO访问器方法,以指示DynamoDB映射器使用哪个编组器类来序列化和反序列化字符串集。 This way you get control over the special use cases. 这样您就可以控制特殊用例。

Here is also a link to an AWS blog post with an example 这里还有一个带有示例的AWS博客文章链接

The one caveat with the approach above is the customer marshaller solution serializes and deserializes to/from string so the representation in the database wouldn't be a set per se. 使用上述方法的一个警告是客户编组解决方案对字符串进行序列化和反序列化,因此数据库中的表示本身不是集合​​。 However, I wouldn't consider that to be too bad. 但是,我不认为这太糟糕了。

Another approach might be to use the Document API , instead of the object mappers, which gives you full control over the items. 另一种方法可能是使用Document API而不是对象映射器,它可以让您完全控制项目。 Though I would still go for custom mapper with string backing. 虽然我仍然会选择带有字符串支持的自定义映射器。

When it came down to the actual problem it was because the code I inherited had the SaveBehavior on the DynamoDBMapperConfig set to UPDATE_SKIP_NULL_ATTRIBUTES which skips null values and therefore will never remove them from DynamoDB. 当它归结为实际问题时,因为我继承的代码将DynamoDBMapperConfig上的SaveBehavior设置为UPDATE_SKIP_NULL_ATTRIBUTES,它跳过空值,因此永远不会从DynamoDB中删除它们。 See this post of explanations of all the different save behaviors. 请参阅此帖对所有不同保存行为的解释。 https://java.awsblog.com/post/Tx1MH3BFPW8FX6W/Using-the-SaveBehavior-Configuration-for-the-DynamoDBMapper https://java.awsblog.com/post/Tx1MH3BFPW8FX6W/Using-the-SaveBehavior-Configuration-for-the-DynamoDBMapper

To elaborate on this a little bit further. 详细说明这一点。

I'm setting my DynamoDBMapperConfig.SaveBehavior as such within my JAVA project. 我在我的JAVA项目中设置了DynamoDBMapperConfig.SaveBehavior。

_dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder() .withSaveBehavior(SaveBehavior.UPDATE) .withTableNameResolver(TABLE_NAME_RESOLVER) .withConversionSchema(ConversionSchemas.V2) .build();

Then whenever you update a model that has a mapped @DynamoDBAttribute you also need to set the particular value to (null). 然后,每当更新具有映射的@DynamoDBAttribute的模型时,您还需要将特定值设置为(null)。

user.setFirstName(null)

This was the easiest way I found to be able to remove attributes from a DynamoDB Entry. 这是我发现能够从DynamoDB条目中删除属性的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM