You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by yh...@apache.org on 2025/06/24 12:09:37 UTC

(beam) branch master updated: Use splitAsStream for wordcount (#35358)

This is an automated email from the ASF dual-hosted git repository.

yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 70a066bfc12 Use splitAsStream for wordcount (#35358)
70a066bfc12 is described below

commit 70a066bfc1228249e369993e3d606353953a531b
Author: Yi Hu <ya...@google.com>
AuthorDate: Tue Jun 24 08:09:31 2025 -0400

    Use splitAsStream for wordcount (#35358)
    
    * Use splitAsStream for wordcount
    
    * add back comments
    
    * Remove unrelated change
---
 .../main/java/org/apache/beam/examples/WordCount.java | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/examples/java/src/main/java/org/apache/beam/examples/WordCount.java b/examples/java/src/main/java/org/apache/beam/examples/WordCount.java
index 3d8e1221836..4eb7eeadfa1 100644
--- a/examples/java/src/main/java/org/apache/beam/examples/WordCount.java
+++ b/examples/java/src/main/java/org/apache/beam/examples/WordCount.java
@@ -32,6 +32,8 @@ package org.apache.beam.examples;
 //     - count
 //     - strings
 
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
 import org.apache.beam.examples.common.ExampleUtils;
 import org.apache.beam.sdk.Pipeline;
 import org.apache.beam.sdk.io.TextIO;
@@ -108,6 +110,7 @@ public class WordCount {
     private final Counter emptyLines = Metrics.counter(ExtractWordsFn.class, "emptyLines");
     private final Distribution lineLenDist =
         Metrics.distribution(ExtractWordsFn.class, "lineLenDistro");
+    private final Pattern splitPattern = Pattern.compile(ExampleUtils.TOKENIZER_PATTERN);
 
     @ProcessElement
     public void processElement(@Element String element, OutputReceiver<String> receiver) {
@@ -117,14 +120,14 @@ public class WordCount {
       }
 
       // Split the line into words.
-      String[] words = element.split(ExampleUtils.TOKENIZER_PATTERN, -1);
-
-      // Output each word encountered into the output PCollection.
-      for (String word : words) {
-        if (!word.isEmpty()) {
-          receiver.output(word);
-        }
-      }
+      Stream<String> stream = splitPattern.splitAsStream(element);
+      stream.forEach(
+          word -> {
+            if (!word.isEmpty()) {
+              // Output each word encountered into the output PCollection.
+              receiver.output(word);
+            }
+          });
     }
   }
   // [END extract_words_fn]