gpt4 book ai didi

java - AWS DynamoDb 使用主键和排序键删除项目

转载 作者:行者123 更新时间:2023-12-01 17:52:07 24 4
gpt4 key购买 nike

我有一个 DynamoDb 表,我想使用主键和排序键从表中删除项目,

以下是我的代码-

public Boolean deleteItem(String primaryKey, String sortKey) {
DynamoDBUtil dynamoDBUtil = new DynamoDBUtil();
AmazonDynamoDB dynamoDBClient = dynamoDBUtil.getDynamoDBClient();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

Table table = dynamoDB.getTable("demo_table");
DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
.withPrimaryKey(new PrimaryKey("primaryKey", primaryKey))
.withConditionExpression("primaryKey = :p_key and sortKey = :s_key")
.withValueMap(new ValueMap()
.withString(":p_key", primaryKey)
.withString(":s_key", sortKey));

table.deleteItem(deleteItemSpec);
return true;
}

我收到以下错误响应 -

Received error response: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FHR7236SPHAJEANHBDKTO52Q2FVV4KQNSO5AEMVJF66Q9ASUAAJG)
Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FHR7236SPHAJEANHBDKTO52Q2FVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2089)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2065)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeDeleteItem(AmazonDynamoDBClient.java:744)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.deleteItem(AmazonDynamoDBClient.java:720)
at com.amazonaws.services.dynamodbv2.document.internal.DeleteItemImpl.doDeleteItem(DeleteItemImpl.java:96)
at com.amazonaws.services.dynamodbv2.document.internal.DeleteItemImpl.deleteItem(DeleteItemImpl.java:75)
at com.amazonaws.services.dynamodbv2.document.Table.deleteItem(Table.java:339)

我双重三重检查,我的架构是正确的。

最佳答案

我认为您将分区(哈希)键与主键混淆了。您的排序(范围)键是主键的一部分。因此,您需要将其作为主键的一部分提供。因此,不需要条件表达式。

主键 = 分区键 + 排序键

例如:

public Boolean deleteItem(String partitionKey, String sortKey) {
DynamoDBUtil dynamoDBUtil = new DynamoDBUtil();
AmazonDynamoDB dynamoDBClient = dynamoDBUtil.getDynamoDBClient();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

Table table = dynamoDB.getTable("demo_table");
DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
.withPrimaryKey("partitionKey", partitionKey, "sortKey", sortKey);

table.deleteItem(deleteItemSpec);
return true;
}

关于java - AWS DynamoDb 使用主键和排序键删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925284/

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