简体   繁体   English

通过Java SDK删除AWS DynamoDB

[英]AWS DynamoDB Deleting via Java SDK

When trying to do a delete via AWS Java SDK I get the error 尝试通过AWS Java SDK进行删除时出现错误

The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 52N303HS3D535K28KSN3R3803VVV4KQNSO5AEMVJF66Q9ASUAAJG) 提供的键元素与架构不匹配(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求ID:52N303HS3D535K28KSN3R3803VVV4KQNSO5AEMVJF66Q9ASUAAJG)

I have a delete item spec defined that looks like this 我定义了一个删除项目规范,看起来像这样

DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey("pk", messageId)
            .withConditionExpression("#ip > :val")
            .withNameMap(new NameMap()
                    .with("#ip", "timestamp"))
            .withValueMap(new ValueMap()
                    .withNumber(":val", 0))
            .withReturnValues(ReturnValue.NONE);

And my table is created like this 我的桌子是这样创建的

List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("pk")
            .withAttributeType(ScalarAttributeType.S));
    attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("timestamp")
            .withAttributeType(ScalarAttributeType.N));

    List<KeySchemaElement> keySchema = new ArrayList<>();
    keySchema.add(new KeySchemaElement()
            .withAttributeName("pk")
            .withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement()
            .withAttributeName("timestamp")
            .withKeyType(KeyType.RANGE));

I'm wondering if the sort key for timestamp is causing this issue. 我想知道时间戳的排序键是否导致此问题。 Do I need to specify the timestamp other than > 0? 我是否需要指定> 0以外的时间戳?

The issue is that you must specify both the hash and range key when you delete an object. 问题是删除对象时必须同时指定哈希键和范围键。 Your hash key is "pk" and your range key is "timestamp" but you are only passing in the hash key into the withPrimaryKey method. 您的哈希键为“ pk”,范围键为“ timestamp”,但您仅将哈希键传递给withPrimaryKey方法。

It looks like you are trying to delete multiple items at a time. 您似乎一次要删除多个项目。 This is not possible with DynamoDB. 对于DynamoDB,这是不可能的。 You will first need to do a query on the key and you can apply your condition expression to that to only retrieve the keys of the items you want to delete. 您首先需要对键进行查询,然后可以将条件表达式应用于该键,以仅检索要删除的项的键。 However, you will then need to call the delete API individually for each record or use the batch API to delete records in batches while still specifying the hash and range key for each individual item. 但是,您将需要为每个记录单独调用delete API或使用批处理API批量删除记录,同时仍为每个单独的项目指定哈希和范围键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM