Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要Java DynamoDB验证异常";“未给出密钥”;当存在于JSON中时_Java_Json_Amazon Web Services_Amazon Dynamodb - Fatal编程技术网

需要Java DynamoDB验证异常";“未给出密钥”;当存在于JSON中时

需要Java DynamoDB验证异常";“未给出密钥”;当存在于JSON中时,java,json,amazon-web-services,amazon-dynamodb,Java,Json,Amazon Web Services,Amazon Dynamodb,运行dynamoDB.putItem()时遇到异常 表定义: { "AttributeDefinitions": [ { "AttributeName": "storage_CACHE_KEY", "AttributeType": "S" } ], "TableName": "my-table", "KeySchema": [ { "AttributeName": "storage_CACHE_KEY", "

运行dynamoDB
.putItem()
时遇到异常

表定义:

{
  "AttributeDefinitions": [
    {
      "AttributeName": "storage_CACHE_KEY",
      "AttributeType": "S"
    }
  ],
  "TableName": "my-table",
  "KeySchema": [
    {
      "AttributeName": "storage_CACHE_KEY",
      "KeyType": "HASH"
    }
  ],
  "TableStatus": "ACTIVE",
  "CreationDateTime": "2017-11-01T05:01:18.883Z",
  "ProvisionedThroughput": {
    "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
    "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
    "NumberOfDecreasesToday": 0,
    "ReadCapacityUnits": 3,
    "WriteCapacityUnits": 3
  },
  "TableSizeBytes": 199,
  "ItemCount": 1,
  "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/my-table"
} 
另外,当序列化为JSON时,我尝试输入的值如下所示:

{
  "storage_CACHE_KEY" : "storage_cache_key_value",
  "some_other_fields" : [""]
}
我仍然不明白为什么会出现以下错误:

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: One of the required keys was not given a value (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:
更新:如何使用.putItem

public <T> PutItemResult put(String key, T value, Optional<Long> expires) throws JsonProcessingException {
    PutItemRequest req = (new PutItemRequest()).withTableName(this.tableName).withItem(this.keyValuePair(key, value, expires));
    return this.db.putItem(req);
}
private Map<String, AttributeValue> buildKey(String value) {
    Map<String, AttributeValue> key = new HashMap();
    key.put("name", new AttributeValue(value));
    return key;
}

private <T> Map<String, AttributeValue> keyValuePair(String key, T value, Optional<Long> expires) throws JsonProcessingException {
    Map<String, AttributeValue> item = this.buildKey(key);
    item.put("json", new AttributeValue(this.mapper.writeValueAsString(value)));
    if (expires.isPresent()) {
        item.put(this.ttlKey, (new AttributeValue()).withN(((Long)expires.get()).toString()));
    }

    return item;
}
(0个已知属性:)
无法识别的字段“present”(class java.util.Optional),未标记为可忽略的

我刚刚花了一整天的时间解决了此错误的一个实例。我没有研究你的问题,看看它是否相关,但提供它的价值。我的原因是以一种方式创建表,然后以另一种方式访问它。具体来说,我是在
com.amazonaws.services.dynamodbv2.document
(返回一个表)中使用
createTable
创建表的,而我本应该在
com.amazonaws.services.dynamodbv2
(返回一个CreateTableResult)中使用
createTable
。如果使用前者创建,然后尝试使用
mapper.load()
从Java映射器访问表,则会出现错误


如果您将表描述为
TableDescription
,然后使用
.toString()
将其转换为字符串,则无论您以何种方式创建表,都会得到完全相同的描述(除去创建日期等)。但是对于Java mapper来说,它们并不相同。

我只是花了一整天的时间来解决这个错误的一个实例。我没有研究你的问题,看看它是否相关,但提供它的价值。我的原因是以一种方式创建表,然后以另一种方式访问它。具体来说,我是在
com.amazonaws.services.dynamodbv2.document
(返回一个表)中使用
createTable
创建表的,而我本应该在
com.amazonaws.services.dynamodbv2
(返回一个CreateTableResult)中使用
createTable
。如果使用前者创建,然后尝试使用
mapper.load()
从Java映射器访问表,则会出现错误


如果您将表描述为
TableDescription
,然后使用
.toString()
将其转换为字符串,则无论您以何种方式创建表,都会得到完全相同的描述(除去创建日期等)。但是,对于Java映射器来说,它们并不相同。

请发布您如何调用putItem方法的代码。putItem方法参数似乎有问题。下面的代码对我来说很好:
amazonDynamoDB.putItem(“我的表”,新的HashMap(){{put(“storage_CACHE_KEY”,new AttributeValue().withS(“storage_CACHE_KEY_value”);put(“some_other_字段”,new AttributeValue()。withSS(Arrays.asList(“hello”,“bye”);})显示项目代码。您说它在“JSON”中,但如果您使用Java,那么在使用DynamoDB时不会直接创建JSON。为什么将密钥设置为“JSON”?它应该是表中定义的“存储\缓存\密钥”(即散列密钥属性名)。@conceptQuest json是原始数据。当我将上面的“name”项更改为“storage\u CACHE\u KEY”时,put起作用,但现在读取无法封送回我的类类型:无法识别的字段“present”(class java.util.Optional),未标记为ignorableI我更新了上面的问题,并提供了详细信息请发布您的代码如何调用putItem方法。putItem方法参数似乎有问题。下面的代码对我来说很好:
amazonDynamoDB.putItem(“我的表”,新的HashMap(){{put(“storage_CACHE_KEY”,new AttributeValue().withS(“storage_CACHE_KEY_value”);put(“some_other_字段”,new AttributeValue()。withSS(Arrays.asList(“hello”,“bye”);})显示项目代码。您说它在“JSON”中,但如果您使用Java,那么在使用DynamoDB时不会直接创建JSON。为什么将密钥设置为“JSON”?它应该是表中定义的“存储\缓存\密钥”(即散列密钥属性名)。@conceptQuest json是原始数据。当我将上面的“name”项更改为“storage\u CACHE\u KEY”时,put起作用,但现在读取无法封送回我的类类型:无法识别的字段“present”(class java.util.Optional),未标记为ignorableI我更新了上面的问题,并提供了详细信息
{
  "storage_CACHE_KEY": "4728264794434232301",
  "json": "{\"storage_CACHE_KEY\":\"23232432472826479401\", \"otherFields\": \"example\"}"
}