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

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

我在本地使用 dynamoDB 进行开发,但如果某些属性是列表(例如字符串的示例),我无法在该数据库中存储项目,如果我使用集合而不是列表,则一切正常但它已损坏逻辑。你能澄清这是我的错误还是 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 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 local here,它似乎已经修复了它。