DynamoDBMapper - 无法创建字符串集

问题描述 投票:0回答:1

我正在使用 DynamoDBMapper 来建模并将我的实体保存到 DynamoDB。我有一个具有一些属性的简单实体,其中一个属性是由

java.util.Set
对象组成的
java.lang.String
。但是,当我请求创建表时,我看到以下警告:

WARN  [com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories] Marshaling a set of non-String objects to a DynamoDB StringSet. You won't be able to read these objects back out of DynamoDB unless you REALLY know what you're doing: it's probably a bug. If you DO know what you're doing feelfree to ignore this warning, but consider using a custom marshaler for this instead.

如果我尝试向此属性添加值然后保存我的实体,我会收到此异常:

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: 
  One or more parameter values were invalid: 
  An string set  may not be empty 
  (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: a6ada7e0-ea45-4826-bff3-78ebbaf6c399)

DynamoDBMapper 文档明确指出支持

Set
String
。我的实体看起来像这样:

@DynamoDBTable(tableName = Constants.TABLE_NAME)
public class ItemRecord implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -554992528076340433L;

    @DynamoDBAttribute
    private String creatorIdentifier = null;

    @DynamoDBTyped(DynamoDBAttributeType.SS)
    private Set<String> itemIdentifiers = new HashSet<>();

    // (More attributes here, including partition and range keys)
    ...
}

我的表创建代码看起来像这样:

ArrayList<AttributeDefinition> attrs = new ArrayList<>();
attrs.add(new AttributeDefinition().withAttributeName("pk").withAttributeType(ScalarAttributeType.S));
attrs.add(new AttributeDefinition().withAttributeName("sk").withAttributeType(ScalarAttributeType.S));

GlobalSecondaryIndex reverseIndex = new GlobalSecondaryIndex().withIndexName(Constants.GSI_REVERSE_INDEX)
        .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L))
        .withKeySchema(new KeySchemaElement().withAttributeName("sk").withKeyType(KeyType.HASH),
                new KeySchemaElement().withAttributeName("pk").withKeyType(KeyType.RANGE))
        .withProjection(new Projection().withProjectionType(ProjectionType.ALL));

CreateTableRequest req = this.mapper.generateCreateTableRequest(ItemRecord.class)
        .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)).withAttributeDefinitions(attrs)
        .withGlobalSecondaryIndexes(reverseIndex);

知道我可能做错了什么吗?我使用的是 AWS Java SDK 版本 1.11.696。

java exception amazon-dynamodb
1个回答
0
投票

DynamoDB does not support empty sets
,但是,集合内允许使用空字符串和二进制值。

这可能会令人困惑,因为一方面不支持空集;另一方面又不支持空集。另一方面,

Lists
Maps
可以存储空内容。

DynamoDb 对待 Set 和 List、Map 的方式不同。

  • Set
    可以由多个标量值表示。集合类型有字符串集合、数字集合和二进制集合。
  • List and Map
    表示文档类型。该类型可以表示具有嵌套属性的复杂结构,例如您在 JSON 文档中找到的内容。

从 Java 的角度来看,集合和列表被视为通用集合类型。然而,在 DynamoDB 的背景下,这些实体承担着针对特定目的量身定制的不同角色。

© www.soinside.com 2019 - 2024. All rights reserved.