Search in sources :

Example 1 with ListTablesRequest

use of software.amazon.awssdk.services.dynamodb.model.ListTablesRequest 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 2 with ListTablesRequest

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

the class SyncPagination method autoPaginationWithResume.

public static void autoPaginationWithResume(DynamoDbClient client) {
    System.out.println("running AutoPagination with resume in case of errors...\n");
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    ListTablesIterable responses = client.listTablesPaginator(listTablesRequest);
    ListTablesResponse lastSuccessfulPage = null;
    try {
        for (ListTablesResponse response : responses) {
            response.tableNames().forEach(System.out::println);
            lastSuccessfulPage = response;
        }
    } catch (DynamoDbException exception) {
        if (lastSuccessfulPage != null) {
            System.out.println(exception.getMessage());
        }
    }
}
Also used : ListTablesIterable(software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)

Example 3 with ListTablesRequest

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

the class SyncPagination method manualPagination.

// snippet-start:[dynamodb.java2.sync_pagination.main]
public static void manualPagination(DynamoDbClient client) {
    System.out.println("running ManualPagination...\n");
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    boolean done = false;
    while (!done) {
        ListTablesResponse listTablesResponse = client.listTables(listTablesRequest);
        System.out.println(listTablesResponse.tableNames());
        if (listTablesResponse.lastEvaluatedTableName() == null) {
            done = true;
        }
        listTablesRequest = listTablesRequest.toBuilder().exclusiveStartTableName(listTablesResponse.lastEvaluatedTableName()).build();
    }
}
Also used : ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)

Example 4 with ListTablesRequest

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

the class SyncPagination method autoPagination.

public static void autoPagination(DynamoDbClient client) {
    System.out.println("running AutoPagination...\n");
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    ListTablesIterable responses = client.listTablesPaginator(listTablesRequest);
    System.out.println("AutoPagination: using for loop");
    for (final ListTablesResponse response : responses) {
        System.out.println(response.tableNames());
    }
    // Print the table names using the responses stream
    System.out.println("AutoPagination: using stream");
    responses.stream().forEach(response -> System.out.println(response.tableNames()));
    // Convert the stream of responses to stream of table names, then print the table names
    System.out.println("AutoPagination: using flatmap to get stream of table names");
    responses.stream().flatMap(response -> response.tableNames().stream()).forEach(System.out::println);
    System.out.println("AutoPagination: iterating directly on the table names");
    Iterable<String> tableNames = responses.tableNames();
    tableNames.forEach(System.out::println);
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Region(software.amazon.awssdk.regions.Region) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) ListTablesIterable(software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable) ListTablesIterable(software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)

Example 5 with ListTablesRequest

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

the class AsyncPagination method AutoPaginationOnCollection.

private static void AutoPaginationOnCollection() throws InterruptedException, ExecutionException {
    // snippet-start:[dynamodb.java2.async_pagination.asyncclient]
    System.out.println("running AutoPagination - iterating on item collection...\n");
    // 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.asyncclient]
    ListTablesPublisher listTablesPublisher = asyncClient.listTablesPaginator(listTablesRequest);
    SdkPublisher<String> publisher = listTablesPublisher.tableNames();
    // snippet-start:[dynamodb.java2.async_pagination.foreach]
    // Use forEach
    CompletableFuture<Void> future = publisher.subscribe(System.out::println);
    future.get();
    // snippet-end:[dynamodb.java2.async_pagination.foreach]
    // snippet-start:[dynamodb.java2.async_pagination.subscriber]
    // Use subscriber
    publisher.subscribe(new Subscriber<String>() {

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription s) {
            subscription = s;
            subscription.request(1);
        }

        @Override
        public void onNext(String tableName) {
            System.out.println(tableName);
            subscription.request(1);
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    // 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) Subscription(org.reactivestreams.Subscription) ListTablesPublisher(software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)

Aggregations

ListTablesRequest (software.amazon.awssdk.services.dynamodb.model.ListTablesRequest)6 ListTablesResponse (software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)5 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)3 Subscription (org.reactivestreams.Subscription)2 DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)2 ListTablesIterable (software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable)2 ListTablesPublisher (software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)2 Region (software.amazon.awssdk.regions.Region)1 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)1