Skip to content

Instantly share code, notes, and snippets.

@picadoh
Last active January 2, 2021 00:45
Show Gist options
  • Save picadoh/b6c0ba6fe2d368a8f413e3fa9ac9a172 to your computer and use it in GitHub Desktop.
Save picadoh/b6c0ba6fe2d368a8f413e3fa9ac9a172 to your computer and use it in GitHub Desktop.
Kafka Streams Example
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper:3.4.6
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:0.10.0.1
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 0.0.0.0
KAFKA_CREATE_TOPICS: "words-topic:1:1,counts-topic:1:1"
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kstreaming</groupId>
<artifactId>kstreaming</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>0.10.0.1</version>
</dependency>
</dependencies>
</project>
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStreamBuilder;
import org.apache.kafka.streams.processor.WallclockTimestampExtractor;
import java.util.Properties;
import static java.util.Arrays.asList;
public class WordCounter {
public static void main(String[] args) {
StreamsConfig streamsConfig = new StreamsConfig(getProperties());
Serde<String> stringSerde = Serdes.String();
KStreamBuilder kStreamBuilder = new KStreamBuilder();
kStreamBuilder.stream(stringSerde, stringSerde, "words-topic")
.flatMapValues(text -> asList(text.split(" ")))
.map((key, word) -> new KeyValue<>(word, word))
.countByKey(stringSerde, "Counts")
.toStream()
.map((word, count) -> new KeyValue<>(word, word + ":" + count))
.to(stringSerde, stringSerde, "counts-topic");
KafkaStreams kafkaStreams = new KafkaStreams(kStreamBuilder, streamsConfig);
kafkaStreams.start();
Runtime.getRuntime().addShutdownHook(new Thread(kafkaStreams::close));
}
private static Properties getProperties() {
Properties props = new Properties();
props.put(StreamsConfig.CLIENT_ID_CONFIG, "Example-Counter");
props.put("group.id", "test-group");
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-counter");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.TIMESTAMP_EXTRACTOR_CLASS_CONFIG, WallclockTimestampExtractor.class);
return props;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment