我有一个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)
我检查了双重检查,我的架构是正确的。
答案 0 :(得分:3)
我认为您将分区(哈希)密钥与主键混淆。您的排序(范围)键是主键的一部分。因此,您需要将其作为主键的一部分提供。因此,不需要条件表达式。
主键=分区键+排序键
例如:
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;
}