gpt4 book ai didi

java - 通过 dynamoDb 表的 Id(hashKey) 检索数据时出错

转载 作者:行者123 更新时间:2023-12-02 08:49:07 25 4
gpt4 key购买 nike

我正在尝试根据 Id(HashKey) 从 DynamoDB 表中获取数据。

Association.java

@DynamoDBTable(tableName = "Association")
public class Association {

private String id;
private String name;
private String adminName;
private String email;
private String url;
private String contactNumber;
private String password;

public Association() { }

public Association(String name, String adminName, String email, String url,
String contactNumber, String password) {
this.name = name;
this.adminName = adminName;
this.email = email;
this.url = url;
this.contactNumber = contactNumber;
this.password = password;
}

public Association(String id, String name, String adminName, String email, String url,
String contactNumber, String password) {
this.id = id;
this.name = name;
this.adminName = adminName;
this.email = email;
this.url = url;
this.contactNumber = contactNumber;
this.password = password;
}

@DynamoDBHashKey(attributeName = "Id")
@DynamoDBAutoGeneratedKey
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@DynamoDBAttribute(attributeName="Name")
public String getName() { return name; }

public void setName(String name) { this.name = name; }

@DynamoDBAttribute(attributeName="AdminName")
public String getAdminName() { return adminName; }

public void setAdminName(String adminName) { this.adminName = adminName; }

@DynamoDBAttribute(attributeName="Email")
public String getEmail() { return email; }

public void setEmail(String email) { this.email = email; }

@DynamoDBAttribute(attributeName="Url")
public String getUrl() { return url; }

public void setUrl(String url) { this.url = url; }

@DynamoDBAttribute(attributeName="ContactNumber")
public String getContactNumber() { return contactNumber; }

public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; }

@DynamoDBAttribute(attributeName="Password")
public String getPassword() { return password; }

public void setPassword(String password) { this.password = password; }

}

AssociationRepository.java:-

private AmazonDynamoDBClient getDynamoDBClient(){
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(REGION));
client.setEndpoint(EndPoint);
return client;
}

public Association fetchById(String id) {
DynamoDBConfig dynamoDBConfig = new DynamoDBConfig();
DynamoDBMapper mapper = new DynamoDBMapper(getDBClient());
Association association = new Association();
association.setId(id);
Association scanResult = null;
try {
scanResult = mapper.load(association);
}
catch (Exception exception){
throw exception;
}
return scanResult;
}

调用 fetchById("123")。其中“123”是表的现有 ID 它向我抛出以下错误:-

{
"errorMessage": "The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: IT6U9BJ0RAJUDPMLGGR67C542VVV4KQNSO5AEMVJF66Q9ASUAAJG)",
"errorType": "com.amazonaws.AmazonServiceException",
"stackTrace": [
"com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1305)",
"com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:852)",

DYNAMODB 表详细信息:-

enter image description here

enter image description here

我已在 AWS 文档中搜索了此问题,但找不到任何解决此问题的解决方案。你们中有人遇到过此问题吗?

最佳答案

如果您意识到,您已将 Name 定义为分区键。但是在代码中,您尝试通过属性 Id 加载项目。

这是不可能的,因为 DynamoDB 只允许您通过主键加载项目,在您的情况下,主键只是分区键(属性 Name)。

因此,您必须修改表以将 Id 设置为分区键,或者创建辅助全局索引来完成此操作: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

关于java - 通过 dynamoDb 表的 Id(hashKey) 检索数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com