无法通过Java API将属性列表存储到本地dynamoDB

时间:2015-04-06 13:40:32

标签: java amazon-web-services amazon-dynamodb nosql

我在本地使用dynamoDB进行开发,但是如果某些属性是List(字符串的前例),我就不能存储在这个DB项目中,如果我使用Set而不是List,则所有工作都正确但是#&# 39; s打破了逻辑。你能否澄清一下我的错误或DynamoDB的错误,例如:

    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    client.setEndpoint("http://0.0.0.0:8000");
    DynamoDB dynamoDB = new DynamoDB(client);
    try {
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
        attributeDefinitions.add(new AttributeDefinition()
                .withAttributeName("key")
                .withAttributeType("S"));

        ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
        keySchema.add(new KeySchemaElement()
                .withAttributeName("key")
                .withKeyType(KeyType.HASH));

        CreateTableRequest createTableRequest = new CreateTableRequest()
                .withKeySchema(keySchema)
                .withAttributeDefinitions(attributeDefinitions)
                .withProvisionedThroughput(new ProvisionedThroughput()
                        .withReadCapacityUnits(1000L)
                        .withWriteCapacityUnits(100L)); //I know it's now reason for local db
        Table table = dynamoDB.createTable(createTableRequest.withTableName("test-list"));
        table.waitForActiveOrDelete();

        Item correctItem = new Item().withPrimaryKey("key", "1").with("list", new HashSet<>(Arrays.asList("a")));
        table.putItem(correctItem);

        ScanResult scanResult = client.scan(new ScanRequest().withTableName(table.getTableName()).withScanFilter(Collections.EMPTY_MAP));

        for (Map<String, AttributeValue> stringAttributeValueMap : scanResult.getItems()) {
            System.out.println(stringAttributeValueMap);
        }


        Item wrongItem = new Item().withPrimaryKey("key", "2").with("list", Arrays.asList("a"));
        table.putItem(wrongItem);

        scanResult = client.scan(new ScanRequest().withTableName(table.getTableName()).withScanFilter(Collections.EMPTY_MAP));

        for (Map<String, AttributeValue> stringAttributeValueMap : scanResult.getItems()) {
            System.out.println(stringAttributeValueMap);
        }
    } finally {
        dynamoDB.getTable("test-list").delete();
    }

备注

  • Java版本为: 1.7.0_71
  • AWS sdk的版本: aws-java-sdk-dynamodb:1.9.24
  • 本地dynamodb: dynamodb_local_2013-12-12
  • DynamoDBLocal由命令启动:java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar

1 个答案:

答案 0 :(得分:0)

我使用相同的依赖项(Java 8除外)运行代码,并在向DynamoDBLocal发出请求后看到了相同的异常。我确实将行从new HashSet<>(Arrays.asList("a"))更改为Collections.singletonList("a")

Exception in thread "main" com.amazonaws.AmazonServiceException: The request processing has failed because of an unknown error, exception or failure. (Service: AmazonDynamoDBv2; Status Code: 500; Error Code: InternalFailure; Request ID: ddce50a5-4e56-4769-9d9e-9e23a3b2dc92)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1078)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:726)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:461)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:296)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3139)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1214)
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:87)
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:41)
    at com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:138)

我针对最新版本的DynamoDB本地版本运行了相同的代码,目前版本为 dynamodb_local_2015-01-27 。我同时运行的命令是java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -port 8000

这是输出:

  

{list = {L:[{S:a,}],},key = {S:1,}}

     

{list = {L:[{S:a,}],},key = {S:1,}}

     

{list = {L:[{S:a,}],},key = {S:2,}}

该版本的DynamoDB Local似乎是一个错误。您应该下载最新版本的DynamoDB本地here,它似乎已修复它。