Search in sources :

Example 1 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class AsyncPagination method useThirdPartySubscriber.

private static void useThirdPartySubscriber() {
    // snippet-start:[dynamodb.java2.async_pagination.async]
    System.out.println("running AutoPagination - using third party subscriber...\n");
    DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create();
    ListTablesPublisher publisher = asyncClient.listTablesPaginator(ListTablesRequest.builder().build());
    // The Flowable class has many helper methods that work with any reactive streams compatible publisher implementation
    List<String> tables = Flowable.fromPublisher(publisher).flatMapIterable(ListTablesResponse::tableNames).toList().blockingGet();
    System.out.println(tables);
// snippet-end:[dynamodb.java2.async_pagination.async]
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) ListTablesPublisher(software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)

Example 2 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class AsyncPagination method AutoPagination.

private static void AutoPagination() throws InterruptedException, ExecutionException {
    System.out.println("running AutoPagination...\n");
    // snippet-start:[dynamodb.java2.async_pagination.pagesclient]
    // Creates a default client with credentials and regions loaded from the environment
    final DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create();
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    // snippet-end:[dynamodb.java2.async_pagination.pagesclient]
    // snippet-start:[dynamodb.java2.async_pagination.pagesforeach]
    ListTablesPublisher publisher = asyncClient.listTablesPaginator(listTablesRequest);
    // Use a for-loop for simple use cases
    CompletableFuture<Void> future = publisher.subscribe(response -> response.tableNames().forEach(System.out::println));
    // snippet-end:[dynamodb.java2.async_pagination.pagesforeach]
    future.get();
    // snippet-start:[dynamodb.java2.async_pagination.pagessubscribe]
    // Or subscribe method should be called to create a new Subscription.
    // A Subscription represents a one-to-one life-cycle of a Subscriber subscribing to a Publisher.
    publisher.subscribe(new Subscriber<ListTablesResponse>() {

        // Maintain a reference to the subscription object, which is required to request data from the publisher
        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription s) {
            subscription = s;
            // Request method should be called to demand data. Here we request a single page
            subscription.request(1);
        }

        @Override
        public void onNext(ListTablesResponse response) {
            response.tableNames().forEach(System.out::println);
            // Once you process the current page, call the request method to signal that you are ready for next page
            subscription.request(1);
        }

        @Override
        public void onError(Throwable t) {
        // Called when an error has occurred while processing the requests
        }

        @Override
        public void onComplete() {
        // This indicates all the results are delivered and there are no more pages left
        }
    });
    // As the above code is non-blocking, make sure your application doesn't end immediately
    // For this example, I am using Thread.sleep to wait for all pages to get delivered
    Thread.sleep(3_000);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) Subscription(org.reactivestreams.Subscription) ListTablesPublisher(software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)

Example 3 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncGetItem method getItem.

// snippet-start:[dynamoasyc.java2.get_item.main]
public static void getItem(DynamoDbAsyncClient client, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    try {
        // Create a GetItemRequest instance
        GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
        // Invoke the DynamoDbAsyncClient object's getItem
        java.util.Collection<AttributeValue> returnedItem = client.getItem(request).join().item().values();
        // Convert Set to Map
        Map<String, AttributeValue> map = returnedItem.stream().collect(Collectors.toMap(AttributeValue::s, s -> s));
        Set<String> keys = map.keySet();
        for (String sinKey : keys) {
            System.out.format("%s: %s\n", sinKey, map.get(sinKey).toString());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
// snippet-end:[dynamoasyc.java2.get_item.main]
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Map(java.util.Map) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Set(java.util.Set) HashMap(java.util.HashMap) Region(software.amazon.awssdk.regions.Region) Collectors(java.util.stream.Collectors) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Example 4 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncTest method DynamoDBAsyncCreateTable.

@Test
@Order(1)
public void DynamoDBAsyncCreateTable() {
    Region region = Region.US_WEST_2;
    DynamoDbAsyncClient client = DynamoDbAsyncClient.builder().region(region).build();
    DynamoDBAsyncCreateTable.createTable(client, newTableName, newKey);
    System.out.println("Test 2 passed");
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Region(software.amazon.awssdk.regions.Region)

Example 5 with DynamoDbAsyncClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient in project aws-doc-sdk-examples by awsdocs.

the class AsyncPagination method ManualPagination.

private static void ManualPagination() throws InterruptedException {
    System.out.println("running ManualPagination...\n");
    // Creates a default async client with credentials and regions loaded from the environment
    DynamoDbAsyncClient client = DynamoDbAsyncClient.create();
    CompletableFuture<ListTablesResponse> response = client.listTables(ListTablesRequest.builder().build());
    // Map the response to another CompletableFuture containing just the table names
    CompletableFuture<List<String>> tableNames = response.thenApply(ListTablesResponse::tableNames);
    // When future is complete (either successfully or in error) handle the response
    tableNames.whenComplete((tables, err) -> {
        if (tables != null) {
            tables.forEach(System.out::println);
        } else {
            // Handle error
            err.printStackTrace();
        }
    });
    Thread.sleep(3_000);
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) List(java.util.List) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)

Aggregations

DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)12 Region (software.amazon.awssdk.regions.Region)7 ListTablesResponse (software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)4 ListTablesPublisher (software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)4 Subscription (org.reactivestreams.Subscription)2 ListTablesRequest (software.amazon.awssdk.services.dynamodb.model.ListTablesRequest)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)1 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)1