简体   繁体   中英

Can't store list of attributes to local dynamoDB via Java API

I used dynamoDB locally for development, but I can't store in this DB items if some on of attributes is List (fore example of Strings), if I use a Set instead of List all works correct but it's broken the logic. Could you clarify it's my mistake or bug of DynamoDB, example below:

    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();
    }

Notes

  • Java version is: 1.7.0_71
  • Version of AWS sdk: aws-java-sdk-dynamodb : 1.9.24
  • Local dynamodb: dynamodb_local_2013-12-12
  • DynamoDBLocal started by command: java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar

I ran your code with the same dependencies (except Java 8) and saw the same exception after making the request to DynamoDBLocal. I did change the line from new HashSet<>(Arrays.asList("a")) to 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)

I ran the same code against the latest version of DynamoDB local version, which is currently dynamodb_local_2015-01-27 . The command I ran both with was java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -port 8000

This was the output:

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

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

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

It looks to be a bug with that version of DynamoDB Local. You should download the latest version of DynamoDB local here which appears to have fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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