Search in sources :

Example 96 with Item

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;
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) HashMap(java.util.HashMap)

Example 97 with Item

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)));
    }
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue)

Example 98 with Item

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();
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) PutItemOutcome(com.amazonaws.services.dynamodbv2.document.PutItemOutcome) Table(com.amazonaws.services.dynamodbv2.document.Table)

Example 99 with Item

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);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeysAndAttributes(com.amazonaws.services.dynamodbv2.model.KeysAndAttributes) BatchGetItemRequest(com.amazonaws.services.dynamodbv2.model.BatchGetItemRequest) BatchGetItemResult(com.amazonaws.services.dynamodbv2.model.BatchGetItemResult) EasyMock.anyObject(org.easymock.EasyMock.anyObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 100 with Item

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;
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)99 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)55 Table (com.amazonaws.services.dynamodbv2.document.Table)52 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)34 AmazonServiceException (com.amazonaws.AmazonServiceException)30 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)23 ArrayList (java.util.ArrayList)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 List (java.util.List)22 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 IOException (java.io.IOException)19 Date (java.util.Date)18 AmazonClientException (com.amazonaws.AmazonClientException)16 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)15