Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Created November 19, 2015 08:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuxingloh/4105feda2e7b3aca5a44 to your computer and use it in GitHub Desktop.
Save fuxingloh/4105feda2e7b3aca5a44 to your computer and use it in GitHub Desktop.
DynamoDB count total rows that match key and filter expression also fixes the reserved word issues
Table table = dynamoDB.getTable(UserActivity.Table.TableName);
Map<String, Object> valueMap = new HashMap<>();
valueMap.put(":user_id", getUserId());
valueMap.put(":state", UserActivity.STATE_ACTIVE);
valueMap.put(":type", type);
QuerySpec spec = new QuerySpec()
.withNameMap(new NameMap()
.with("#s", "State").with("#t", "Type"))
.withKeyConditionExpression("UserId = :user_id")
.withFilterExpression("#s = :state and #t = :type")
.withValueMap(valueMap);
spec.withSelect(Select.COUNT);
ItemCollection<QueryOutcome> items = table.query(spec);
for (Item item : items) {
Logger.of(getClass()).info(item.toJSONPretty());
}
return items.getTotalCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment