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()); } } } }
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(); } }