Search in sources :

Example 1 with ListTablesResponse

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

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

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

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

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

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