好的,所以我看了this post,我看了this other post。这是我的具体问题:
我有一个名为“server”的简单DynamoDB表,其中包含一个分区键“serverName”。这是表格描述:
"table":{
"attributeDefinitions":[{
"attributeName":"serverName","attributeType":"S"
}],
"tableName":"server",
"keySchema":[{
"attributeName":"serverName","keyType":"HASH"
}],
"tableStatus":"ACTIVE",
"creationDateTime":"Jul 29, 2017 12:46:36 PM",
"provisionedThroughput":{
"numberOfDecreasesToday":0,
"readCapacityUnits":5,
"writeCapacityUnits":5
}
}
我有一个Java表定义,如下所示:
@DynamoDBTable(tableName="server")
public static class Server {
private String serverName;
private Room[] rooms;
Server() {}
Server(String serverName) {
this.serverName = serverName;
this.setRooms();
}
@DynamoDBHashKey(attributeName="serverName")
String getServerName() {
return serverName;
}
void setServerName(String serverName) {
this.serverName = serverName;
}
.
.
.
}
你可以在描述中看到我的HASH属性为“serverName”,在我的Java表定义中有一个@DynamoDBHashKey(attributeName="serverName")
。
有人可以帮我弄清楚为什么我仍然得到......
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Server; no mapping for HASH key
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.hashKey(DynamoDBMapperTableModel.java:119) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel$Builder.build(DynamoDBMapperTableModel.java:449) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$StandardTableFactory.getTable(StandardModelFactories.java:107) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.getTableModel(DynamoDBMapper.java:393) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:431) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.load(AbstractDynamoDBMapper.java:80) ~[aws-java-sdk-dynamodb-1.11.105.jar!/:na]
at com.animationlibationstudios.channel.inventory.persist.dynamodb.ServerDynamoRepository.readServer(ServerDynamoRepository.java:110) ~[classes!/:na]
at com.animationlibationstudios.channel.inventory.persist.dynamodb.ServerDynamoRepository.readServer(ServerDynamoRepository.java:26) ~[classes!/:na]
(p.s。我正在使用JDK 8和com.amazonaws:aws-java-sdk-dynamodb:1.11.105)
答案 0 :(得分:2)
<强>更新强>
你的&#34; getServerName&#34;方法不公开,DynamoDBMapper偶然发现它。公开它应该有效:
@DynamoDBHashKey(attributeName="serverName")
public String getServerName() {
return serverName;
}
原始答案:
确保您已创建具有正确分区键定义的表,该定义具有&#34; serverName&#34;作为相同类型的分区键。
以下是如何操作:
DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDB);
CreateTableRequest createTableRequest = dynamoDBMapper.generateCreateTableRequest(itemClass);
// Set your throughput here
createTableRequest.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
dynamoDB.createTable(createTableRequest);