use of com.amazonaws.services.dynamodbv2.document.QueryOutcome 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.QueryOutcome 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.QueryOutcome 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<>();
}
use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.
the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByIp.
@Test
public void testGetWorkloadRecordsByIp() {
long currTime = System.currentTimeMillis();
Mockito.doReturn("1234").when(item).getString("instanceId");
Mockito.doReturn("openstack").when(item).getString("provider");
Mockito.doReturn("athenz.api").when(item).getString("service");
Mockito.doReturn("test-host").when(item).getString("hostname");
Mockito.doReturn(currTime).when(item).get("creationTime");
Mockito.doReturn(currTime).when(item).get("updateTime");
Mockito.doReturn(currTime).when(item).get("certExpiryTime");
Mockito.doReturn(currTime).when(item).getLong("creationTime");
Mockito.doReturn(currTime).when(item).getLong("updateTime");
Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
when(itemCollection.iterator()).thenReturn(iteratorSupport);
when(iteratorSupport.hasNext()).thenReturn(true, false);
when(iteratorSupport.next()).thenReturn(item);
Mockito.doReturn(itemCollection).when(ipIndex).query(Mockito.any(QuerySpec.class));
DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
dbConn.setOperationTimeout(10);
List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByIp("10.0.0.1");
Assert.assertNotNull(wlRecordList);
Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
Assert.assertEquals(wlRecordList.get(0).getService(), "athenz.api");
Assert.assertEquals(wlRecordList.get(0).getHostname(), "NA");
Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(0));
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.QueryOutcome in project athenz by yahoo.
the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByService.
@Test
public void testGetWorkloadRecordsByService() {
long currTime = System.currentTimeMillis();
Mockito.doReturn("1234").when(item).getString("instanceId");
Mockito.doReturn("openstack").when(item).getString("provider");
Mockito.doReturn("10.10.10.11").when(item).getString("ip");
Mockito.doReturn("test-host").when(item).getString("hostname");
Mockito.doReturn(true).when(item).hasAttribute("hostname");
Mockito.doReturn(currTime).when(item).get("creationTime");
Mockito.doReturn(currTime).when(item).get("updateTime");
Mockito.doReturn(currTime).when(item).get("certExpiryTime");
Mockito.doReturn(currTime).when(item).getLong("creationTime");
Mockito.doReturn(currTime).when(item).getLong("updateTime");
Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
Mockito.doReturn(true).when(item).hasAttribute("certExpiryTime");
ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
when(itemCollection.iterator()).thenReturn(iteratorSupport);
when(iteratorSupport.hasNext()).thenReturn(true, false);
when(iteratorSupport.next()).thenReturn(item);
Mockito.doReturn(itemCollection).when(serviceIndex).query(Mockito.any(QuerySpec.class));
DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
dbConn.setOperationTimeout(10);
List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByService("athenz", "api");
Assert.assertNotNull(wlRecordList);
Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
Assert.assertEquals(wlRecordList.get(0).getIp(), "10.10.10.11");
Assert.assertEquals(wlRecordList.get(0).getHostname(), "test-host");
Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(currTime));
dbConn.close();
}
Aggregations