use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.
the class MoviesQuery method main.
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Movies");
HashMap<String, String> nameMap = new HashMap<String, String>();
nameMap.put("#yr", "year");
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":yyyy", 1985);
QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#yr = :yyyy").withNameMap(nameMap).withValueMap(valueMap);
ItemCollection<QueryOutcome> items = null;
Iterator<Item> iterator = null;
Item item = null;
try {
System.out.println("Movies from 1985");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("year") + ": " + item.getString("title"));
}
} catch (Exception e) {
System.err.println("Unable to query movies from 1985");
System.err.println(e.getMessage());
}
valueMap.put(":yyyy", 1992);
valueMap.put(":letter1", "A");
valueMap.put(":letter2", "L");
querySpec.withProjectionExpression("#yr, title, info.genres, info.actors[0]").withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2").withNameMap(nameMap).withValueMap(valueMap);
try {
System.out.println("Movies from 1992 - titles A-L, with genres and lead actor");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("year") + ": " + item.getString("title") + " " + item.getMap("info"));
}
} catch (Exception e) {
System.err.println("Unable to query movies from 1992:");
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.
the class TryDaxTests method queryTest.
void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) {
long startTime, endTime;
System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2);
Table table = client.getTable(tableName);
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":pkval", pk);
valueMap.put(":skval1", sk1);
valueMap.put(":skval2", sk2);
QuerySpec spec = new QuerySpec().withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2").withValueMap(valueMap);
for (int i = 0; i < iterations; i++) {
startTime = System.nanoTime();
ItemCollection<QueryOutcome> items = table.query(spec);
try {
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
iter.next();
}
} catch (Exception e) {
System.err.println("Unable to query table:");
e.printStackTrace();
}
endTime = System.nanoTime();
printTime(startTime, endTime, iterations);
}
}
use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.
the class TryDaxTests method scanTest.
void scanTest(String tableName, DynamoDB client, int iterations) {
long startTime, endTime;
System.out.println("Scan test - all items in the table");
Table table = client.getTable(tableName);
for (int i = 0; i < iterations; i++) {
startTime = System.nanoTime();
ItemCollection<ScanOutcome> items = table.scan();
try {
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
iter.next();
}
} catch (Exception e) {
System.err.println("Unable to scan table:");
e.printStackTrace();
}
endTime = System.nanoTime();
printTime(startTime, endTime, iterations);
}
}
use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnection method mostUpdatedHostRecord.
private boolean mostUpdatedHostRecord(Item recordToCheck) {
try {
// Query all records with the same hostName / provider / service as recordToCheck
QuerySpec spec = new QuerySpec().withKeyConditionExpression("hostName = :v_host_name").withFilterExpression("attribute_exists(provider) AND provider = :v_provider AND attribute_exists(service) AND service = :v_service").withValueMap(new ValueMap().withString(":v_host_name", recordToCheck.getString(KEY_HOSTNAME)).withString(":v_provider", recordToCheck.getString(KEY_PROVIDER)).withString(":v_service", recordToCheck.getString(KEY_SERVICE)));
ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> hostNameIndex.query(spec));
List<Item> allRecordsWithHost = new ArrayList<>();
for (Item item : outcome) {
allRecordsWithHost.add(item);
}
// Verify recordToCheck is the most updated record with this hostName
return dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(recordToCheck, allRecordsWithHost, KEY_CURRENT_TIME, KEY_PRIMARY);
} catch (Exception ex) {
LOGGER.error("DynamoDB mostUpdatedHostRecord failed for item: {}, error: {}", recordToCheck.toString(), ex.getMessage());
return false;
}
}
use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnection method getUnrefreshedCertRecordsByDate.
private List<Item> getUnrefreshedCertRecordsByDate(String provider, long yesterday, String unrefreshedCertDate) {
try {
QuerySpec spec = new QuerySpec().withKeyConditionExpression("currentDate = :v_current_date").withFilterExpression("provider = :v_provider AND attribute_exists(hostName) AND (attribute_not_exists(lastNotifiedTime) OR lastNotifiedTime < :v_last_notified)").withValueMap(new ValueMap().withString(":v_current_date", unrefreshedCertDate).withNumber(":v_last_notified", yesterday).withString(":v_provider", provider));
ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> currentTimeIndex.query(spec));
List<Item> items = new ArrayList<>();
for (Item item : outcome) {
items.add(item);
}
return items;
} catch (Exception ex) {
LOGGER.error("DynamoDB getUnrefreshedCertRecordsByDate failed for provider: {}, date: {} error: {}", provider, unrefreshedCertDate, ex.getMessage());
}
return new ArrayList<>();
}
Aggregations