I am trying to delete multiple records in DynamoDB table using the partition key and sort key. The approach is taking lot of time to delete the records. I am following below approach implemented using java aws sdk library.
public static ItemCollection<QueryOutcome> getKeyDataFromTable(String tableName, HashMap<String, String> keyValue){
QuerySpec spec = null;
//Connect to Database Table
spec = new QuerySpec().withKeyConditionExpression("pKey" =:v_id").withValueMap(new ValueMap().withString(":v_id",keyValue.get("partitionkey")));
ItemCollection<QueryOutcome> items = table.query(spec);
}
public void deleteItems(){
String tableName="tableName";
String partitionKey="partitionKey";
HashMap<String, String> keyValue = new HashMap<>();
keyValue.put("partitionKey",partitionKey);
ItemCollection<QueryOutCome> keyDataFromTable = getKeyDataFromTable(tableName,keyValue);
IteratorSupport<QueryOutcome> iterator = keyDataFromTable.iterator();
Item item=null;
List<String> sortKeyList = new ArrayList<>();
while(iterator.hasNext()){
item=iterator.next();
sortKeyList.add((String) item.get("sortKey"));
}
for(String skey:ortKeyList){
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("partitionKey","partitionKey","sortKey","sortKey");
table.deleteItem(deleteItemSpec);
}
}
Is there any right approach of doing this dynamically with better performance?