我有一个dynamoDb表,只有hashKey。我试图通过使用dynamoDbMapper插入记录。保存方法。虽然没有键不匹配,但我收到以下错误:
提供的键元素不匹配模式(Service: AmazonDynamoDBv2;状态码:400;错误码:ValidationException
表只有hashKey (id),没有排序键。还有全局二级索引
我的dynamoDb数据类是:
public class DynamoDbData {
@DynamoDBHashKey(attributeName = "id")
private String id;
@DynamoDBIndexRangeKey(globalSecondaryIndexName = "my-index", attributeName = "myfield")
private String myField;
@DynamoDBAttribute(attributeName = "title")
@DynamoDBIndexHashKey(globalSecondaryIndexName = "my-index", attributeName = "title")
private String title;
}
和我试图保存对象使用dynamoDbMapper。
dynamoDBMapper.save(dynamoDbData);
通常表示键不匹配。但这里我只有hashKey在dynamoDb和对象,我试图保存也只有hasKey具有相同的属性名称。这里出了什么问题?为什么我得到这个错误?
我需要使用单独的对象只有hashKey或只有索引吗?
注意:我可以通过使用DynamoDBLocal成功保存测试
description -table输出:
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "myfield",
"AttributeType": "S"
},
{
"AttributeName": "title",
"AttributeType": "S"
}
],
"TableName": "myTable",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2022-11-15T14:44:24.068000+01:00",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
},
"TableSizeBytes": 8413,
"GlobalSecondaryIndexes": [
{
"IndexName": "my-index",
"KeySchema": [
{
"AttributeName": "title",
"KeyType": "HASH"
},
{
"AttributeName": "myfield",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "KEYS_ONLY"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
}
}
]
}
}
映射器的Bean定义:
@Bean
DynamoDBMapper dynamoDbMapper(AmazonDynamoDB amazonDynamoDB) {
DynamoDBMapperConfig config = DynamoDBMapperConfig.builder()
.withPaginationLoadingStrategy(DynamoDBMapperConfig.PaginationLoadingStrategy.EAGER_LOADING)
.withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
.withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix(properties.getTablePrefix()))
.build();
return new DynamoDBMapper(amazonDynamoDB, config);
}
这取决于Java和DynamoDB如何推断它们的变量/属性名称…我在为Getter/Setter
使用Lombok注释时注意到了这一点虽然你已经设置了你的哈希键注释为@DynamoDBHashKey(attributeName = "id")
,当使用Mapper时,它会看到id
为Id
,这意味着它不匹配你的键元素。
如果您正在使用Lombok,那么请尝试使用小写的id
:
@Data
@DynamoDBTable(tableName = "Stack01")
public static class DynamoDbData {
@DynamoDBHashKey(attributeName = "id")
private String id;
@DynamoDBAttribute(attributeName = "title")
@DynamoDBIndexHashKey(globalSecondaryIndexName = "my-index", attributeName = "title")
private String title;
@DynamoDBIndexRangeKey(globalSecondaryIndexName = "my-index", attributeName = "age")
private String age;
}