Search in sources :

Example 1 with ListTablesPublisher

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

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

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

the class AsyncPagination method useThirdPartySubscriber_Reactor.

private static void useThirdPartySubscriber_Reactor() {
    System.out.println("running AutoPagination - using third party subscriber...\n");
    DynamoDbAsyncClient asyncClient = DynamoDbAsyncClient.create();
    ListTablesPublisher publisher = asyncClient.listTablesPaginator(ListTablesRequest.builder().build());
    // The Flux class has many helper methods that work with any reactive streams compatible publisher implementation
    List<String> tables = Flux.from(publisher).flatMapIterable(ListTablesResponse::tableNames).collectList().block();
    System.out.println(tables);
}
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 4 with ListTablesPublisher

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

DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)4 ListTablesPublisher (software.amazon.awssdk.services.dynamodb.paginators.ListTablesPublisher)4 ListTablesResponse (software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)3 Subscription (org.reactivestreams.Subscription)2 ListTablesRequest (software.amazon.awssdk.services.dynamodb.model.ListTablesRequest)2