在DynamoDB中存储非唯一数组

时间:2015-01-25 19:16:30

标签: amazon-dynamodb

我是DynamoDB的新手,并且发现令人惊讶的是“模型驱动”无架构数据库的方式。无论如何,小气的狡猾,我想知道如何在一个项目中声明一个属性,这个属性是一个值数组但是一个可以有重复值的属性。

具体来说,我所存储的是wordcloud的情绪词汇阵列,如果一个特定的词被不止一次使用,那么我想知道。我的简单回答是有一个名为 wordsPositive 的属性/属性,它将被设置为“SS”类型,但它似乎不允许重复的数组元素。

尝试时遇到的错误是:

  

400:输入集合[更好,更好,最好,更重要,更好,更成功,最好,更有创意,更有实力,更有益处]包含重复项。]

在没有重复项的记录中查看AWS Web界面,我可以看到定义的结构可能支持重复值,但鉴于它调用列表“set”我的猜测是这是不允许的:

aws web interface

在DynamoDB中执行此操作的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

您正在寻找"L"数据类型。以下是使用 AWS Java SDK 1.9.3版对DynamoDBLocal运行的完整示例。立即Scan检查表的结果(代码下面的输出)。使用实际的DynamoDB服务时应该没有任何不同:

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.CreateTableResult;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.util.Tables;
import com.google.common.collect.ImmutableList;

import java.util.List;

public class Test28140400 {

    private static final String TABLE_NAME = "test_table";
    private static final String HASH_KEY_ATTRIBUTE_NAME = "hash";
    private static final String RANGE_KEY_ATTRIBUTE_NAME = "range";

    public static void main(String[] args) {
        AWSCredentials awsCredentials = new BasicAWSCredentials("key", "secret");
        AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(awsCredentials);
        dynamoDB.setEndpoint("http://localhost:4000");

        if (Tables.doesTableExist(dynamoDB, TABLE_NAME)) {
            dynamoDB.deleteTable(TABLE_NAME);
        }

        CreateTableRequest createTableRequest = new CreateTableRequest();
        createTableRequest.setTableName(TABLE_NAME);
        createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(50l, 50l));

        List<KeySchemaElement> keySchema = ImmutableList.<KeySchemaElement>builder()
                .add(new KeySchemaElement(HASH_KEY_ATTRIBUTE_NAME, KeyType.HASH))
                .add(new KeySchemaElement(RANGE_KEY_ATTRIBUTE_NAME, KeyType.RANGE))
                .build();
        createTableRequest.setKeySchema(keySchema);

        List<AttributeDefinition> attributeDefinitions = ImmutableList.<AttributeDefinition>builder()
              .add(new AttributeDefinition(HASH_KEY_ATTRIBUTE_NAME, ScalarAttributeType.S))
              .add(new AttributeDefinition(RANGE_KEY_ATTRIBUTE_NAME, ScalarAttributeType.N))
              .build();
        createTableRequest.setAttributeDefinitions(attributeDefinitions);

        CreateTableResult createTableResult = dynamoDB.createTable(createTableRequest);
        Tables.waitForTableToBecomeActive(dynamoDB, TABLE_NAME);

        PutItemRequest putItemRequest = new PutItemRequest();
        putItemRequest.setTableName(TABLE_NAME);
        putItemRequest.addItemEntry(HASH_KEY_ATTRIBUTE_NAME, new AttributeValue().withS("first_hash"));
        putItemRequest.addItemEntry(RANGE_KEY_ATTRIBUTE_NAME, new AttributeValue().withN("123456789"));
        // Using the "L" type
        //better, excellent, best, important, good, success, best, creative, powerful, benefits
        List<AttributeValue> attributes = ImmutableList.<AttributeValue>builder()
                .add(new AttributeValue().withS("better"))
                .add(new AttributeValue().withS("excellent"))
                .add(new AttributeValue().withS("best"))
                .add(new AttributeValue().withS("important"))
                .add(new AttributeValue().withS("good"))
                .add(new AttributeValue().withS("success"))
                .add(new AttributeValue().withS("best"))
                .add(new AttributeValue().withS("creative"))
                .add(new AttributeValue().withS("powerful"))
                .add(new AttributeValue().withS("benefits"))
                .build();
        putItemRequest.addItemEntry("random_attribute", new AttributeValue().withL(attributes));
        dynamoDB.putItem(putItemRequest);

        ScanResult scan = dynamoDB.scan(new ScanRequest(TABLE_NAME));
        scan.getItems().forEach(System.out::println);
    }
}

输出:

  

{random_attribute = {L:[{S:better,},{S:excellent,},{S:best,},{S:   重要的,},{S:好,},{S:成功,},{S:最好,},{S:创意,},   {S:强大,},{S:Benefits,}],},range = {N:123456789,},hash = {S:   first_hash,}}

相关问题