0

I have one partition key value and a list of corresponding sort key values for a dynamoDB table, and I'm trying to use batchWriteItem in java to delete them. But it is not working

TableWriteItems deleteItems = new TableWriteItems("tableName");
keyList = new ArrayList();
for (String sortKey: sortKeys) {
   keyList.add(partitionKey)
   keyList.add(sortKey)
}
deleteItems.withHashAndRangeKeysToDelete(partKeyField,sortKeyField,keyList)

Source for doing it this way. But I get the following error:

number of hash and range key values must be the same

I also tried to add the withHashAndRangeKeysToDelete iteratively to the TableWriteItems object directly, but that didn't work either. So how do I perform this batch delete operation?

Edit: Based on @luk2302 suggestion, I used this solution to pass the list of hash key and sort keys collected above as a varargs parameter. I am getting the following error:

java.lang.UnsupportedOperationException: value type: class [Ljava.lang.String;

My code for reference:

deleteItems.withHashAndRangeKeysToDelete(partKeyField,sortKeyField, hashRangeList.toArray(new String[0]))

Edit2: I mistakenly added an additional param in my code which caused the error. Issue is resolved now so closing the question

baggy696
  • 37
  • 7
  • Where is `keyList` defined, are there already objects in it? The internal code only checks `alternatingHashAndRangeKeyValues.length % 2 != 0` and your code should pass that since you always add two entries. – luk2302 May 16 '22 at 12:51
  • @luk2302 KeyList is just an empty arrayList that I create prior to the loop. Will edit to add the same. And I checked keyList while debugging and it seems alright – baggy696 May 16 '22 at 12:56
  • Actually: the method has a varargs parameter, you need to pass in an array of keys, not a list of keys, the list of keys is considered just one `Object` and you therefore receive that error. – luk2302 May 16 '22 at 12:58

0 Answers0