Skip to content

Commit 0a76081

Browse files
tomekl007pivovarit
authored andcommitted
Bael 1421 (eugenp#3312)
* BAEL-1421 live test of kafka streams * BAEL-1421 Removed not-needed dependency * BAEL-1421 rearannge * BAEL-1421 rearannge * fix pom * move to libraries-data * move to libraries-data
1 parent e87114b commit 0a76081

2 files changed

Lines changed: 112 additions & 19 deletions

File tree

‎libraries-data/pom.xml‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>com.baeldung</groupId>
@@ -15,18 +16,18 @@
1516
<artifactId>kryo</artifactId>
1617
<version>${kryo.version}</version>
1718
</dependency>
18-
<dependency>
19+
<dependency>
1920
<groupId>com.h2database</groupId>
2021
<artifactId>h2</artifactId>
2122
<version>${h2.version}</version>
2223
</dependency>
23-
<dependency>
24+
<dependency>
2425
<groupId>junit</groupId>
2526
<artifactId>junit</artifactId>
2627
<version>${junit.version}</version>
2728
<scope>test</scope>
2829
</dependency>
29-
<dependency>
30+
<dependency>
3031
<groupId>com.goldmansachs.reladomo</groupId>
3132
<artifactId>reladomo</artifactId>
3233
<version>${reladomo.version}</version>
@@ -41,8 +42,31 @@
4142
<artifactId>ormlite-jdbc</artifactId>
4243
<version>${ormlite.version}</version>
4344
</dependency>
45+
<dependency>
46+
<groupId>org.apache.kafka</groupId>
47+
<artifactId>kafka-streams</artifactId>
48+
<version>${kafka.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.apache.kafka</groupId>
52+
<artifactId>kafka-clients</artifactId>
53+
<version>${kafka.version}</version>
54+
<exclusions>
55+
<exclusion>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-log4j12</artifactId>
58+
</exclusion>
59+
</exclusions>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.apache.kafka</groupId>
63+
<artifactId>kafka-clients</artifactId>
64+
<version>${kafka.version}</version>
65+
<classifier>test</classifier>
66+
<scope>test</scope>
67+
</dependency>
4468
</dependencies>
45-
69+
4670
<build>
4771
<plugins>
4872
<plugin>
@@ -68,24 +92,24 @@
6892
</goals>
6993
<configuration>
7094
<tasks>
71-
<property name="plugin_classpath" refid="maven.plugin.classpath" />
95+
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
7296
<taskdef name="gen-reladomo" classpath="plugin_classpath"
73-
classname="com.gs.fw.common.mithra.generator.MithraGenerator" />
97+
classname="com.gs.fw.common.mithra.generator.MithraGenerator"/>
7498
<gen-reladomo
75-
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
76-
generateGscListMethod="true"
77-
generatedDir="${project.build.directory}/generated-sources/reladomo"
78-
nonGeneratedDir="${project.basedir}/src/main/java" />
99+
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
100+
generateGscListMethod="true"
101+
generatedDir="${project.build.directory}/generated-sources/reladomo"
102+
nonGeneratedDir="${project.basedir}/src/main/java"/>
79103

80104
<taskdef name="gen-ddl"
81-
classname="com.gs.fw.common.mithra.generator.dbgenerator.MithraDbDefinitionGenerator"
82-
loaderRef="reladomoGenerator">
83-
<classpath refid="maven.plugin.classpath" />
105+
classname="com.gs.fw.common.mithra.generator.dbgenerator.MithraDbDefinitionGenerator"
106+
loaderRef="reladomoGenerator">
107+
<classpath refid="maven.plugin.classpath"/>
84108
</taskdef>
85109
<gen-ddl
86-
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
87-
generatedDir="${project.build.directory}/generated-db/sql"
88-
databaseType="postgres" />
110+
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
111+
generatedDir="${project.build.directory}/generated-db/sql"
112+
databaseType="postgres"/>
89113
</tasks>
90114
</configuration>
91115
</execution>
@@ -139,16 +163,23 @@
139163
</executions>
140164
</plugin>
141165
<!-- /Reladomo-->
142-
166+
143167
</plugins>
144168
</build>
145-
169+
170+
<repositories>
171+
<repository>
172+
<id>Apache Staging</id>
173+
<url>https://repository.apache.org/content/groups/staging</url>
174+
</repository>
175+
</repositories>
146176
<properties>
147177
<kryo.version>4.0.1</kryo.version>
148178
<h2.version>1.4.196</h2.version>
149179
<reladomo.version>16.5.1</reladomo.version>
150180
<junit.version>4.12</junit.version>
151181
<maven-compiler-plugin.version>3.6.2</maven-compiler-plugin.version>
152182
<ormlite.version>5.0</ormlite.version>
183+
<kafka.version>1.0.0</kafka.version>
153184
</properties>
154185
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.kafkastreams;
2+
3+
import org.apache.kafka.clients.consumer.ConsumerConfig;
4+
import org.apache.kafka.common.serialization.Serde;
5+
import org.apache.kafka.common.serialization.Serdes;
6+
import org.apache.kafka.streams.KafkaStreams;
7+
import org.apache.kafka.streams.StreamsConfig;
8+
import org.apache.kafka.streams.kstream.KStream;
9+
import org.apache.kafka.streams.kstream.KStreamBuilder;
10+
import org.apache.kafka.streams.kstream.KTable;
11+
import org.apache.kafka.test.TestUtils;
12+
import org.junit.Ignore;
13+
import org.junit.Test;
14+
15+
import java.util.Arrays;
16+
import java.util.Properties;
17+
import java.util.regex.Pattern;
18+
19+
public class KafkaStreamsLiveTest {
20+
private String bootstrapServers = "localhost:9092";
21+
22+
@Test
23+
@Ignore("it needs to have kafka broker running on local")
24+
public void shouldTestKafkaStreams() throws InterruptedException {
25+
//given
26+
String inputTopic = "inputTopic";
27+
28+
Properties streamsConfiguration = new Properties();
29+
streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-live-test");
30+
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
31+
streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
32+
streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
33+
streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 1000);
34+
streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
35+
// Use a temporary directory for storing state, which will be automatically removed after the test.
36+
streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
37+
38+
//when
39+
KStreamBuilder builder = new KStreamBuilder();
40+
KStream<String, String> textLines = builder.stream(inputTopic);
41+
Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS);
42+
43+
KTable<String, Long> wordCounts = textLines
44+
.flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase())))
45+
.groupBy((key, word) -> word)
46+
.count();
47+
48+
wordCounts.foreach((word, count) -> System.out.println("word: " + word + " -> " + count));
49+
50+
String outputTopic = "outputTopic";
51+
final Serde<String> stringSerde = Serdes.String();
52+
final Serde<Long> longSerde = Serdes.Long();
53+
wordCounts.to(stringSerde, longSerde, outputTopic);
54+
55+
KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);
56+
streams.start();
57+
58+
//then
59+
Thread.sleep(30000);
60+
streams.close();
61+
}
62+
}

0 commit comments

Comments
 (0)