Created
May 4, 2020 03:19
-
-
Save RICH0423/e54cccc93113b3d2e0be2f78764a41bb to your computer and use it in GitHub Desktop.
implement a simple WordCount program using the high-level Kafka Streams DSL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rich.stream.example; | |
import org.apache.kafka.common.serialization.Serdes; | |
import org.apache.kafka.common.utils.Bytes; | |
import org.apache.kafka.streams.KafkaStreams; | |
import org.apache.kafka.streams.StreamsBuilder; | |
import org.apache.kafka.streams.StreamsConfig; | |
import org.apache.kafka.streams.Topology; | |
import org.apache.kafka.streams.kstream.KeyValueMapper; | |
import org.apache.kafka.streams.kstream.Materialized; | |
import org.apache.kafka.streams.kstream.Produced; | |
import org.apache.kafka.streams.kstream.ValueMapper; | |
import org.apache.kafka.streams.state.KeyValueStore; | |
import java.util.Arrays; | |
import java.util.Locale; | |
import java.util.Properties; | |
import java.util.concurrent.CountDownLatch; | |
/** | |
* In this example, we implement a simple WordCount program using the high-level Streams DSL | |
* that reads from a source topic "streams-plaintext-input", where the values of messages represent lines of text, | |
* split each text line into words and then compute the word occurence histogram, write the continuous updated histogram | |
* into a topic "streams-wordcount-output" where each record is an updated count of a single word. | |
*/ | |
public class WordCount { | |
public static void main(String[] args) throws Exception { | |
Properties props = new Properties(); | |
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-wordcount"); | |
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); | |
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass()); | |
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); | |
final StreamsBuilder builder = new StreamsBuilder(); | |
builder.<String, String>stream("streams-plaintext-input") | |
// Split each text line, by whitespace, into words. | |
.flatMapValues(value -> Arrays.asList(value.toLowerCase(Locale.getDefault()).split("\\W+"))) | |
// Group the text words as message keys | |
.groupBy((key, value) -> value) | |
// specifies that the running count should be stored in a state store named counts-store | |
.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("counts-store")) | |
.toStream() | |
// Store the running counts as a changelog stream to the output topic. | |
.to("streams-wordcount-output", Produced.with(Serdes.String(), Serdes.Long())); | |
final Topology topology = builder.build(); | |
final KafkaStreams streams = new KafkaStreams(topology, props); | |
final CountDownLatch latch = new CountDownLatch(1); | |
// attach shutdown handler to catch control-c | |
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") { | |
@Override | |
public void run() { | |
streams.close(); | |
latch.countDown(); | |
} | |
}); | |
try { | |
streams.start(); | |
latch.await(); | |
} catch (Throwable e) { | |
System.exit(1); | |
} | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment