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]
}
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);
}
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]
}
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");
}
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);
}
Aggregations