use of com.amazonaws.services.dynamodbv2.document.Item in project archaius by Netflix.
the class DynamoDbDeploymentContextTableCache method loadPropertiesFromTable.
/**
* Scan the table in dynamo and create a map with the results. In this case the map has a complex type as the value,
* so that Deployment Context is taken into account.
*
* @param table
* @return
*/
@Override
protected Map<String, PropertyWithDeploymentContext> loadPropertiesFromTable(String table) {
Map<String, PropertyWithDeploymentContext> propertyMap = new HashMap<String, PropertyWithDeploymentContext>();
Map<String, AttributeValue> lastKeysEvaluated = null;
do {
ScanRequest scanRequest = new ScanRequest().withTableName(table).withExclusiveStartKey(lastKeysEvaluated);
ScanResult result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
String keyVal = item.get(keyAttributeName.get()).getS();
// Need to deal with the fact that these attributes might not exist
DeploymentContext.ContextKey contextKey = item.containsKey(contextKeyAttributeName.get()) ? DeploymentContext.ContextKey.valueOf(item.get(contextKeyAttributeName.get()).getS()) : null;
String contextVal = item.containsKey(contextValueAttributeName.get()) ? item.get(contextValueAttributeName.get()).getS() : null;
String key = keyVal + ";" + contextKey + ";" + contextVal;
propertyMap.put(key, new PropertyWithDeploymentContext(contextKey, contextVal, keyVal, item.get(valueAttributeName.get()).getS()));
}
lastKeysEvaluated = result.getLastEvaluatedKey();
} while (lastKeysEvaluated != null);
return propertyMap;
}
use of com.amazonaws.services.dynamodbv2.document.Item in project di-authentication-api by alphagov.
the class DynamoExtension method clearDynamoTable.
protected void clearDynamoTable(AmazonDynamoDB dynamoDB, String tableName, String key) {
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
ScanResult result = dynamoDB.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
dynamoDB.deleteItem(tableName, Map.of(key, item.get(key)));
}
}
use of com.amazonaws.services.dynamodbv2.document.Item in project newrelic-java-agent by newrelic.
the class DynamoApiTest method putItem.
private void putItem() {
Table table = getOrCreateTable(TABLE_NAME);
Item item = DynamoUtil.createDefaultItem();
PutItemOutcome outcome = table.putItem(item);
outcome.getPutItemResult();
}
use of com.amazonaws.services.dynamodbv2.document.Item in project aws-sdk-android by aws-amplify.
the class DynamoDBMapperTest method testBatchLoadRetiresForUnprocessedItems.
@Test
public void testBatchLoadRetiresForUnprocessedItems() {
List<Object> itemsToGet = new ArrayList<Object>();
itemsToGet.add(new MockTwoValuePlusVersionClass("PrimaryKey", "Value1", null));
itemsToGet.add(new MockDifferentTableName("OtherPrimaryKey", "OtherValue1"));
EasyMock.reset(mockClient);
// First result will show that the first item was processed
// successfully, and the second item needs to be retried
BatchGetItemResult firstResult = new BatchGetItemResult();
Map<String, List<Map<String, AttributeValue>>> responses = new HashMap<String, List<Map<String, AttributeValue>>>();
List<Map<String, AttributeValue>> items = new ArrayList<Map<String, AttributeValue>>();
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("id", new AttributeValue().withS("idValue"));
item.put("firstValue", new AttributeValue().withS("firstValueValue"));
item.put("secondValue", new AttributeValue().withS("secondValueValue"));
item.put("version", new AttributeValue().withN("1"));
items.add(item);
responses.put(mapper.getTableName(MockTwoValuePlusVersionClass.class, config), items);
firstResult.withResponses(responses);
// Do Not process second table on first go around
Map<String, KeysAndAttributes> unprocessedObjects = new HashMap<String, KeysAndAttributes>();
KeysAndAttributes unprocessedObject = new KeysAndAttributes();
Map<String, AttributeValue> unprocessedKey = new HashMap<String, AttributeValue>();
unprocessedKey.put("id", new AttributeValue().withS("OtherPrimaryKey"));
unprocessedObject.withKeys(unprocessedKey);
unprocessedObjects.put("MockDifferentTableName", unprocessedObject);
firstResult.withUnprocessedKeys(unprocessedObjects);
// EasyMock is broken and will change all captured values to the last
// capture, even if the capture
// objects are different and set to CaptureType.ALL this is used so that
// we can verify arguments
FixedCapture<BatchGetItemRequest> capture = new FixedCapture<BatchGetItemRequest>(CaptureType.ALL, new FixedCapture.CapCallback<BatchGetItemRequest>() {
@Override
public void valueSet(BatchGetItemRequest value) {
assertEquals(value.getRequestItems().size(), 2);
}
});
EasyMock.expect(mockClient.batchGetItem(EasyMock.capture(capture))).andReturn(firstResult);
BatchGetItemResult secondResult = new BatchGetItemResult();
Map<String, List<Map<String, AttributeValue>>> secondResponses = new HashMap<String, List<Map<String, AttributeValue>>>();
List<Map<String, AttributeValue>> secondItems = new ArrayList<Map<String, AttributeValue>>();
Map<String, AttributeValue> secondItem = new HashMap<String, AttributeValue>();
secondItem.put("id", new AttributeValue().withS("idValue2"));
secondItem.put("firstValue", new AttributeValue().withS("firstValueValue2"));
secondItems.add(secondItem);
responses.put(mapper.getTableName(MockDifferentTableName.class, config), secondItems);
secondResult.withResponses(secondResponses);
FixedCapture<BatchGetItemRequest> capture2 = new FixedCapture<BatchGetItemRequest>(CaptureType.ALL, new FixedCapture.CapCallback<BatchGetItemRequest>() {
@Override
public void valueSet(BatchGetItemRequest value) {
assertEquals(value.getRequestItems().size(), 1);
}
});
EasyMock.expect(mockClient.batchGetItem(EasyMock.capture(capture2))).andReturn(secondResult);
EasyMock.replay(mockClient);
Map<String, List<Object>> loadResults = mapper.batchLoad(itemsToGet);
EasyMock.verify(mockClient);
assertEquals(loadResults.keySet().size(), 2);
assertEquals(loadResults.get("aws-android-sdk-dynamodbmapper-test").size(), 1);
assertEquals(loadResults.get("aws-android-sdk-dynamodbmapper-test-different-table").size(), 1);
assertEquals(loadResults.get("aws-android-sdk-dynamodbmapper-test").get(0).getClass(), MockTwoValuePlusVersionClass.class);
assertEquals(loadResults.get("aws-android-sdk-dynamodbmapper-test-different-table").get(0).getClass(), MockDifferentTableName.class);
}
use of com.amazonaws.services.dynamodbv2.document.Item in project aws-sdk-android by aws-amplify.
the class DynamoDBMapper method count.
/**
* Evaluates the specified scan expression and returns the count of matching
* items, without returning any of the actual item data.
* <p>
* This operation will scan your entire table, and can therefore be very
* expensive. Use with caution.
*
* @param clazz The class mapped to a DynamoDB table.
* @param scanExpression The parameters for running the scan.
* @param config The configuration to use for this scan, which overrides the
* default provided at object construction.
* @return The count of matching items, without returning any of the actual
* item data.
*/
public int count(Class<?> clazz, DynamoDBScanExpression scanExpression, DynamoDBMapperConfig config) {
config = mergeConfig(config);
final ScanRequest scanRequest = createScanRequestFromExpression(clazz, scanExpression, config);
scanRequest.setSelect(Select.COUNT);
// Count scans can also be truncated for large datasets
int count = 0;
ScanResult scanResult = null;
do {
scanResult = db.scan(applyUserAgent(scanRequest));
count += scanResult.getCount();
scanRequest.setExclusiveStartKey(scanResult.getLastEvaluatedKey());
} while (scanResult.getLastEvaluatedKey() != null);
return count;
}
Aggregations