From 609abb983d86281d562767324f14e23560d8044e Mon Sep 17 00:00:00 2001 From: dengziming Date: Sat, 27 Feb 2021 11:02:59 +0800 Subject: [PATCH] MINOR: Word count should account for extra whitespaces between words --- .../streams/examples/wordcount/WordCountProcessorDemo.java | 2 +- .../streams/examples/wordcount/WordCountTransformerDemo.java | 2 +- .../streams/examples/wordcount/WordCountProcessorTest.java | 2 +- .../streams/examples/wordcount/WordCountTransformerTest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorDemo.java b/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorDemo.java index 646a01653224d..8835c109e94f3 100644 --- a/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorDemo.java +++ b/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorDemo.java @@ -85,7 +85,7 @@ public void init(final ProcessorContext context) { @Override public void process(final Record record) { - final String[] words = record.value().toLowerCase(Locale.getDefault()).split(" "); + final String[] words = record.value().toLowerCase(Locale.getDefault()).split("\\W+"); for (final String word : words) { final Integer oldValue = kvStore.get(word); diff --git a/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerDemo.java b/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerDemo.java index bef0a626f98fc..33f8499d91da2 100644 --- a/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerDemo.java +++ b/streams/examples/src/main/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerDemo.java @@ -93,7 +93,7 @@ public void init(final ProcessorContext context) { @Override public KeyValue transform(final String dummy, final String line) { - final String[] words = line.toLowerCase(Locale.getDefault()).split(" "); + final String[] words = line.toLowerCase(Locale.getDefault()).split("\\W+"); for (final String word : words) { final Integer oldValue = this.kvStore.get(word); diff --git a/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorTest.java b/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorTest.java index 0df64b7b88605..eb62630a80329 100644 --- a/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorTest.java +++ b/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountProcessorTest.java @@ -52,7 +52,7 @@ public void test() { processor.init(context); // send a record to the processor - processor.process(new Record<>("key", "alpha beta gamma alpha", 0L)); + processor.process(new Record<>("key", "alpha beta\tgamma\n\talpha", 0L)); // note that the processor does not forward during process() assertTrue(context.forwarded().isEmpty()); diff --git a/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerTest.java b/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerTest.java index 82e916645a25d..662f0a2e494c7 100644 --- a/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerTest.java +++ b/streams/examples/src/test/java/org/apache/kafka/streams/examples/wordcount/WordCountTransformerTest.java @@ -52,7 +52,7 @@ public void test() { transformer.init(context); // send a record to the transformer - transformer.transform("key", "alpha beta gamma alpha"); + transformer.transform("key", "alpha beta\tgamma\n\talpha"); // note that the transformer does not forward during transform() assertTrue(context.forwarded().isEmpty());