Search in sources :

Example 6 with WriteRequest

use of com.amazonaws.services.dynamodbv2.model.WriteRequest in project nifi by apache.

the class PutDynamoDBTest method setUp.

@Before
public void setUp() {
    outcome = new BatchWriteItemOutcome(result);
    result.setUnprocessedItems(new HashMap<String, List<WriteRequest>>());
    final DynamoDB mockDynamoDB = new DynamoDB(Regions.AP_NORTHEAST_1) {

        @Override
        public BatchWriteItemOutcome batchWriteItem(TableWriteItems... tableWriteItems) {
            return outcome;
        }
    };
    putDynamoDB = new PutDynamoDB() {

        @Override
        protected DynamoDB getDynamoDB() {
            return mockDynamoDB;
        }
    };
}
Also used : TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) BatchWriteItemOutcome(com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.junit.Before)

Example 7 with WriteRequest

use of com.amazonaws.services.dynamodbv2.model.WriteRequest in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIBatchWrite method writeMultipleItemsBatchWrite.

private static void writeMultipleItemsBatchWrite() {
    try {
        // Add a new item to Forum
        TableWriteItems forumTableWriteItems = // Forum
        new TableWriteItems(forumTableName).withItemsToPut(new Item().withPrimaryKey("Name", "Amazon RDS").withNumber("Threads", 0));
        // Add a new item, and delete an existing item, from Thread
        // This table has a partition key and range key, so need to specify
        // both of them
        TableWriteItems threadTableWriteItems = new TableWriteItems(threadTableName).withItemsToPut(new Item().withPrimaryKey("ForumName", "Amazon RDS", "Subject", "Amazon RDS Thread 1").withString("Message", "ElastiCache Thread 1 message").withStringSet("Tags", new HashSet<String>(Arrays.asList("cache", "in-memory")))).withHashAndRangeKeysToDelete("ForumName", "Subject", "Amazon S3", "S3 Thread 100");
        System.out.println("Making the request.");
        BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(forumTableWriteItems, threadTableWriteItems);
        do {
            // Check for unprocessed keys which could happen if you exceed
            // provisioned throughput
            Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems();
            if (outcome.getUnprocessedItems().size() == 0) {
                System.out.println("No unprocessed items found");
            } else {
                System.out.println("Retrieving the unprocessed items");
                outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems);
            }
        } while (outcome.getUnprocessedItems().size() > 0);
    } catch (Exception e) {
        System.err.println("Failed to retrieve items: ");
        e.printStackTrace(System.err);
    }
}
Also used : TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) Item(com.amazonaws.services.dynamodbv2.document.Item) BatchWriteItemOutcome(com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome) List(java.util.List) IOException(java.io.IOException)

Example 8 with WriteRequest

use of com.amazonaws.services.dynamodbv2.model.WriteRequest in project aws-doc-sdk-examples by awsdocs.

the class LowLevelBatchWrite method writeMultipleItemsBatchWrite.

private static void writeMultipleItemsBatchWrite() {
    try {
        // Create a map for the requests in the batch
        Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
        // Create a PutRequest for a new Forum item
        Map<String, AttributeValue> forumItem = new HashMap<String, AttributeValue>();
        forumItem.put("Name", new AttributeValue().withS("Amazon RDS"));
        forumItem.put("Threads", new AttributeValue().withN("0"));
        List<WriteRequest> forumList = new ArrayList<WriteRequest>();
        forumList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(forumItem)));
        requestItems.put(table1Name, forumList);
        // Create a PutRequest for a new Thread item
        Map<String, AttributeValue> threadItem = new HashMap<String, AttributeValue>();
        threadItem.put("ForumName", new AttributeValue().withS("Amazon RDS"));
        threadItem.put("Subject", new AttributeValue().withS("Amazon RDS Thread 1"));
        threadItem.put("Message", new AttributeValue().withS("ElastiCache Thread 1 message"));
        threadItem.put("KeywordTags", new AttributeValue().withSS(Arrays.asList("cache", "in-memory")));
        List<WriteRequest> threadList = new ArrayList<WriteRequest>();
        threadList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(threadItem)));
        // Create a DeleteRequest for a Thread item
        Map<String, AttributeValue> threadDeleteKey = new HashMap<String, AttributeValue>();
        threadDeleteKey.put("ForumName", new AttributeValue().withS("Amazon S3"));
        threadDeleteKey.put("Subject", new AttributeValue().withS("S3 Thread 100"));
        threadList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(threadDeleteKey)));
        requestItems.put(table2Name, threadList);
        BatchWriteItemResult result;
        BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest().withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
        do {
            System.out.println("Making the request.");
            batchWriteItemRequest.withRequestItems(requestItems);
            result = client.batchWriteItem(batchWriteItemRequest);
            // Print consumed capacity units
            for (ConsumedCapacity consumedCapacity : result.getConsumedCapacity()) {
                String tableName = consumedCapacity.getTableName();
                Double consumedCapacityUnits = consumedCapacity.getCapacityUnits();
                System.out.println("Consumed capacity units for table " + tableName + ": " + consumedCapacityUnits);
            }
            // Check for unprocessed keys which could happen if you exceed
            // provisioned throughput
            System.out.println("Unprocessed Put and Delete requests: \n" + result.getUnprocessedItems());
            requestItems = result.getUnprocessedItems();
        } while (result.getUnprocessedItems().size() > 0);
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to retrieve items: ");
        ase.printStackTrace(System.err);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) WriteRequest(com.amazonaws.services.dynamodbv2.model.WriteRequest) ArrayList(java.util.ArrayList) PutRequest(com.amazonaws.services.dynamodbv2.model.PutRequest) BatchWriteItemRequest(com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) ReturnConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity) BatchWriteItemResult(com.amazonaws.services.dynamodbv2.model.BatchWriteItemResult) AmazonServiceException(com.amazonaws.AmazonServiceException) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(com.amazonaws.services.dynamodbv2.model.DeleteRequest)

Example 9 with WriteRequest

use of com.amazonaws.services.dynamodbv2.model.WriteRequest in project aws-doc-sdk-examples by awsdocs.

the class LowLevelBatchWriteSyntax method writeMultipleItemsBatchWrite.

private static void writeMultipleItemsBatchWrite() {
    try {
        // Begin syntax extract
        // Create a map for the requests in the batch
        Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
        // Create a PutRequest for a new Forum item
        Map<String, AttributeValue> forumItem = new HashMap<String, AttributeValue>();
        forumItem.put("Name", new AttributeValue().withS("Amazon RDS"));
        forumItem.put("Threads", new AttributeValue().withN("0"));
        List<WriteRequest> forumList = new ArrayList<WriteRequest>();
        forumList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(forumItem)));
        requestItems.put("Forum", forumList);
        // Create a PutRequest for a new Thread item
        Map<String, AttributeValue> threadItem = new HashMap<String, AttributeValue>();
        threadItem.put("ForumName", new AttributeValue().withS("Amazon RDS"));
        threadItem.put("Subject", new AttributeValue().withS("Amazon RDS Thread 1"));
        List<WriteRequest> threadList = new ArrayList<WriteRequest>();
        threadList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(threadItem)));
        // Create a DeleteRequest for a Thread item
        Map<String, AttributeValue> threadDeleteKey = new HashMap<String, AttributeValue>();
        threadDeleteKey.put("ForumName", new AttributeValue().withS("Some partition key value"));
        threadDeleteKey.put("Subject", new AttributeValue().withS("Some sort key value"));
        threadList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(threadDeleteKey)));
        requestItems.put("Thread", threadList);
        BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest();
        System.out.println("Making the request.");
        batchWriteItemRequest.withRequestItems(requestItems);
        client.batchWriteItem(batchWriteItemRequest);
    // End syntax extract
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to retrieve items: ");
        ase.printStackTrace(System.err);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) WriteRequest(com.amazonaws.services.dynamodbv2.model.WriteRequest) ArrayList(java.util.ArrayList) PutRequest(com.amazonaws.services.dynamodbv2.model.PutRequest) BatchWriteItemRequest(com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(com.amazonaws.services.dynamodbv2.model.DeleteRequest)

Example 10 with WriteRequest

use of com.amazonaws.services.dynamodbv2.model.WriteRequest in project beam by apache.

the class DynamoDBIOWriteTest method testWritePutItemsWithPartialSuccess.

@Test
public void testWritePutItemsWithPartialSuccess() {
    List<WriteRequest> writes = putRequests(Item.range(0, 10));
    when(client.batchWriteItem(any(BatchWriteItemRequest.class))).thenReturn(partialWriteSuccess(writes.subList(4, 10))).thenReturn(partialWriteSuccess(writes.subList(8, 10))).thenReturn(new BatchWriteItemResult().withUnprocessedItems(ImmutableMap.of()));
    pipeline.apply(// number if items to produce
    Create.of(10)).apply(// 10 items in one bundle
    ParDo.of(new GenerateItems())).apply("write", DynamoDBIO.<Item>write().withWriteRequestMapperFn(putRequestMapper).withAwsClientsProvider(StaticAwsClientsProvider.of(client)).withRetryConfiguration(try4Times));
    PipelineResult result = pipeline.run();
    result.waitUntilFinish();
    verify(client, times(3)).batchWriteItem(any(BatchWriteItemRequest.class));
    InOrder ordered = inOrder(client);
    ordered.verify(client).batchWriteItem(argThat(matchWritesUnordered(writes)));
    ordered.verify(client).batchWriteItem(argThat(matchWritesUnordered(writes.subList(4, 10))));
    ordered.verify(client).batchWriteItem(argThat(matchWritesUnordered(writes.subList(8, 10))));
}
Also used : BatchWriteItemResult(com.amazonaws.services.dynamodbv2.model.BatchWriteItemResult) InOrder(org.mockito.InOrder) WriteRequest(com.amazonaws.services.dynamodbv2.model.WriteRequest) PipelineResult(org.apache.beam.sdk.PipelineResult) BatchWriteItemRequest(com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest) Test(org.junit.Test)

Aggregations

WriteRequest (com.amazonaws.services.dynamodbv2.model.WriteRequest)7 List (java.util.List)7 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)6 ArrayList (java.util.ArrayList)6 PutRequest (com.amazonaws.services.dynamodbv2.model.PutRequest)4 HashMap (java.util.HashMap)4 BatchWriteItemOutcome (com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome)3 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)3 BatchWriteItemRequest (com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest)3 BatchWriteItemResult (com.amazonaws.services.dynamodbv2.model.BatchWriteItemResult)3 DeleteRequest (com.amazonaws.services.dynamodbv2.model.DeleteRequest)3 Test (org.junit.Test)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 TestRunner (org.apache.nifi.util.TestRunner)2 Before (org.junit.Before)2 Item (com.amazonaws.services.dynamodbv2.document.Item)1 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)1 ReturnConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity)1 IOException (java.io.IOException)1