如何解决AWS DynamoDbException:提供的键元素与架构不匹配
我通过遵循两个字段java sdk v2 guide创建了一个DynamoDB表;
PartitionKey -> Resource (String)
Field1 -> Role (String)
因此没有任何排序键。我有一个非常简单的POJO,看起来像;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
@DynamoDbBean
public class RoleItem {
private String resource;
private String role;
@DynamoDbPartitionKey
public String getResource() {
return this.resource;
};
public void setResource(String resource) {
this.resource = resource;
}
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
}
}
这就是我测试getItem的方式;
public class App {
public static void main(String[] args) {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.create();
DynamoDbTable<RoleItem> userRoles = enhancedClient.table("UserRoles",TableSchema.fromBean(RoleItem.class));
Key key = Key.builder().partitionValue("FINANCE").build();
GetItemEnhancedRequest getItemEnhancedRequest = GetItemEnhancedRequest.builder().key(key).build();
RoleItem item = userRoles.getItem(getItemEnhancedRequest);
}
}
最后我得到了
Exception in thread "main" software.amazon.awssdk.services.dynamodb.model.DynamoDbException: The provided key element does not match the schema (Service: DynamoDb,Status Code: 400,Request ID: 4L424LI9BT2H2S5E9L2H0SJLE3VV4KQNSO5AEMVJF66Q9ASUAAJG,Extended Request ID: null)
我找不到我的示例出了什么问题。感谢您的帮助。谢谢!
注意:以防万一我可以通过使用具有相同架构的DynamoDBMapper但使用aws sdk v1注释来实现相同的操作。所以我的实体类看起来像;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@DynamoDBTable(tableName = "UserRoles")
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RoleItem {
@DynamoDBHashKey(attributeName = "Resource")
private String resource;
@DynamoDBAttribute(attributeName = "Role")
private String role;
}
这是我执行getItem操作的方式;
public class App {
public static void main(String[] args) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.EU_CENTRAL_1).build();
DynamoDBMapper mapper = new DynamoDBMapper(client);
DynamoDBQueryExpression<RoleItem> queryExpression = new DynamoDBQueryExpression<RoleItem>().withHashKeyValues(RoleItem.builder().resource("FINANCE").build());
PaginatedQueryList<RoleItem> roleItems = mapper.query(RoleItem.class,queryExpression);
RoleItem roleItem = roleItems.stream().findFirst().get();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。