Skip to content

Instantly share code, notes, and snippets.

@rendon
Created May 6, 2017 17:21
Show Gist options
  • Save rendon/27b579f588e4a15febd3e8eb70f17e73 to your computer and use it in GitHub Desktop.
Save rendon/27b579f588e4a15febd3e8eb70f17e73 to your computer and use it in GitHub Desktop.
DynamoDB: Truncates table, handy in testing and debugging.
import java.util.Iterator;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.ScanOutcome;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
/**
* Truncates table, handy in testing and debugging.
*
* Credentials are read from ~/.aws/credentials.
* See http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html for more
* details.
*/
public class TableCleaner {
private DynamoDB db;
public TableCleaner(DynamoDB db) {
this.db = db;
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.printf("Usage: java TableCleaner <table:partition_key[:range_key]>");
System.exit(1);
}
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider())
.withRegion(Regions.US_WEST_2)
.build();
DynamoDB db = new DynamoDB(client);
new TableCleaner(db).truncateTable(args[0]);
}
public void truncateTable(String tableSpec) {
try {
String[] tokens = tableSpec.split(":");
if (tokens.length == 2) {
truncateTable(tokens[0], tokens[1]);
} else if (tokens.length == 3) {
truncateTable(tokens[0], tokens[1], tokens[2]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void truncateTable(String tableName, String hashKeyName, String rangeKeyName) throws Exception {
Table table = db.getTable(tableName);
ScanSpec spec = new ScanSpec();
ItemCollection<ScanOutcome> items = table.scan(spec);
Iterator<Item> it = items.iterator();
while (it.hasNext()) {
Item item = it.next();
String hashKey = item.getString(hashKeyName);
String rangeKey = item.getString(rangeKeyName);
PrimaryKey key = new PrimaryKey( hashKeyName, hashKey, rangeKeyName, rangeKey);
table.deleteItem(key);
System.out.printf("Deleted item with key: <%s, %s>\n", hashKey, rangeKey);
}
}
private void truncateTable(String tableName, String hashKeyName) throws Exception {
Table table = db.getTable(tableName);
ScanSpec spec = new ScanSpec();
ItemCollection<ScanOutcome> items = table.scan(spec);
Iterator<Item> it = items.iterator();
while (it.hasNext()) {
Item item = it.next();
String hashKey = item.getString(hashKeyName);
PrimaryKey key = new PrimaryKey(hashKeyName, hashKey);
table.deleteItem(key);
System.out.printf("Deleted item with key: %s\n", hashKey);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment