private static void AutoPagination() { System.out.println("running AutoPagination...\n"); final DynamoDbClient client = DynamoDbClient.create(); 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); }
private static void AutoPaginationWithResume() { System.out.println("running AutoPagination with resume in case of errors...\n"); final DynamoDbClient client = DynamoDbClient.create(); 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 (Exception exception) { if (lastSuccessfulPage != null) { System.out.println(exception.getMessage()); } } } }
List<String> table_names = response.tableNames();
private static void ManualPagination() { System.out.println("running ManualPagination...\n"); final DynamoDbClient client = DynamoDbClient.create(); 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(); } }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("ListTablesResponse").add("TableNames", tableNames()) .add("LastEvaluatedTableName", lastEvaluatedTableName()).build(); }
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "TableNames": return Optional.ofNullable(clazz.cast(tableNames())); case "LastEvaluatedTableName": return Optional.ofNullable(clazz.cast(lastEvaluatedTableName())); default: return Optional.empty(); } }
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(tableNames()); hashCode = 31 * hashCode + Objects.hashCode(lastEvaluatedTableName()); return hashCode; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ListTablesResponse)) { return false; } ListTablesResponse other = (ListTablesResponse) obj; return Objects.equals(tableNames(), other.tableNames()) && Objects.equals(lastEvaluatedTableName(), other.lastEvaluatedTableName()); }
/** * Returns an iterable to iterate through the paginated {@link ListTablesResponse#tableNames()} member. The returned * iterable is used to iterate through the results across all response pages and not a single page. * * This method is useful if you are interested in iterating over the paginated member in the response pages instead * of the top level pages. Similar to iteration over pages, this method internally makes service calls to get the * next list of results until the iteration stops or there are no more results. */ public final SdkIterable<String> tableNames() { Function<ListTablesResponse, Iterator<String>> getIterator = response -> { if (response != null && response.tableNames() != null) { return response.tableNames().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsIterable.builder().pagesIterable(this).itemIteratorFunction(getIterator).build(); }
/** * Returns a publisher that can be used to get a stream of data. You need to subscribe to the publisher to request * the stream of data. The publisher has a helper forEach method that takes in a {@link java.util.function.Consumer} * and then applies that consumer to each response returned by the service. */ public final SdkPublisher<String> tableNames() { Function<ListTablesResponse, Iterator<String>> getIterator = response -> { if (response != null && response.tableNames() != null) { return response.tableNames().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsPublisher.builder().nextPageFetcher(new ListTablesResponseFetcher()).iteratorFunction(getIterator) .isLastPage(isLastPage).build(); }