简体   繁体   English

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

[英]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. 我在本地使用dynamoDB进行开发,但是如果某些属性是List(在字符串示例之前),如果我使用Set而不是List的话,那么我就不能在此DB项目中存储所有东西都正确,但是这破坏了逻辑。 Could you clarify it's my mistake or bug of DynamoDB, example below: 您能否说明这是我的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();
    }

Notes 笔记

  • Java version is: 1.7.0_71 Java版本是: 1.7.0_71
  • Version of AWS sdk: aws-java-sdk-dynamodb : 1.9.24 AWS sdk的版本: aws-java-sdk-dynamodb:1.9.24
  • Local dynamodb: dynamodb_local_2013-12-12 本地dynamodb: dynamodb_local_2013-12-12
  • DynamoDBLocal started by command: java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar DynamoDBLocal由命令启动: 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. 我以相同的依赖项(Java 8除外)运行了您的代码,并向DynamoDBLocal发出请求后看到了相同的异常。 I did change the line from new HashSet<>(Arrays.asList("a")) to Collections.singletonList("a") . 我确实将行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)

I ran the same code against the latest version of DynamoDB local version, which is currently dynamodb_local_2015-01-27 . 我针对最新版本的DynamoDB本地版本(当前为dynamodb_local_2015-01-27)运行了相同的代码。 The command I ran both with was java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -port 8000 我同时运行的命令是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: 1,}} {list = {L:[{S:a,}],},key = {S:1,}}

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

It looks to be a bug with that version of DynamoDB Local. 该版本的DynamoDB Local似乎是一个错误。 You should download the latest version of DynamoDB local here which appears to have fixed it. 您应该在此处下载DynamoDB local的最新版本,该版本似乎已经修复了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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