DomGarguilo commented on code in PR #334: URL: https://github.com/apache/accumulo-website/pull/334#discussion_r960013687
########## tour/batch-scanner-code.md: ########## @@ -4,44 +4,52 @@ title: Batch Scanner Code Below is a solution to the exercise. -```java - static void exercise(AccumuloClient client) throws Exception { - // Create a table called "GothamPD". - client.tableOperations().create("GothamPD"); - - // Generate 10,000 rows of henchman data - try (BatchWriter writer = client.createBatchWriter("GothamPD")) { - for (int i = 0; i < 10_000; i++) { - Mutation m = new Mutation(String.format("id%04d", i)); - m.put("villain", "alias", "henchman" + i); - m.put("villain", "yearsOfService", "" + (new Random().nextInt(50))); - m.put("villain", "wearsCape?", "false"); - writer.addMutation(m); - } - } - // 1. Create a BatchScanner with 5 query threads - try (BatchScanner batchScanner = client.createBatchScanner("GothamPD", Authorizations.EMPTY, 5)) { - - // 2. Create a collection of 2 sample ranges and set it to the batchScanner - List<Range>ranges = new ArrayList<Range>(); - ranges.add(new Range("id1000", "id1999")); - ranges.add(new Range("id9000", "id9999")); - batchScanner.setRanges(ranges); - - // 3. Fetch just the columns we want - batchScanner.fetchColumn(new Text("villain"), new Text("yearsOfService")); - - // 4. Calculate average years of service - Long totalYears = 0L; - Long entriesRead = 0L; - for (Map.Entry<Key, Value> entry : batchScanner) { - totalYears += Long.valueOf(entry.getValue().toString()); - entriesRead++; - } - System.out.println("The average years of service of " + entriesRead + " villains is " + totalYears / entriesRead); - } - } +Create a table called "GothamBatch". + +```commandline +jshell> client.tableOperations().create("GothamBatch"); +``` + +Generate 10,000 rows of villain data + +```commandline +jshell> try (BatchWriter writer = client.createBatchWriter("GothamBatch")) { + ...> for (int i = 0; i < 10_000; i++) { + ...> Mutation m = new Mutation(String.format("id%04d", i)); + ...> m.put("villain", "alias", "henchman" + i); + ...> m.put("villain", "yearsOfService", "" + (new Random().nextInt(50))); + ...> m.put("villain", "wearsCape?", "false"); + ...> writer.addMutation(m); + ...> } + ...> } ``` + +Create a BatchScanner with 5 query threads +```commandline +jshell> try (BatchScanner batchScanner = client.createBatchScanner("GothamBatch", Authorizations.EMPTY, 5)) { + ...> + ...> // Create a collection of 2 sample ranges and set it to the batchScanner + ...> List<Range> ranges = new ArrayList<Range>(); + ...> + ...> // Create a collection of 2 sample ranges and set it to the batchScanner + ...> ranges.add(new Range("id1000", "id1999")); + ...> ranges.add(new Range("id9000", "id9999")); + ...> batchScanner.setRanges(ranges); + ...> + ...> // Fetch just the columns we want + ...> batchScanner.fetchColumn(new Text("villain"), new Text("yearsOfService")); + ...> + ...> // Calculate average years of service + ...> Long totalYears = 0L; + ...> Long entriesRead = 0L; + ...> for (Map.Entry<Key, Value> entry : batchScanner) { + ...> totalYears += Long.valueOf(entry.getValue().toString()); + ...> entriesRead++; + ...> } + ...> System.out.println("The average years of service of " + entriesRead + " villains is " + totalYears / entriesRead); Review Comment: It could be cool to show off how BatchScanner.stream could be used here or somewhere else. For example, could be used to find the average like this: ``` Double average = scanner.stream().map(Map.Entry::getValue).map(Value::toString).mapToLong(Long::valueOf).average().getAsDouble(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@accumulo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org