-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
180 lines (146 loc) · 6.66 KB
/
Solution.java
File metadata and controls
180 lines (146 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package kafka_streams.windowing.session_windows;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.*;
import org.apache.kafka.streams.kstream.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Objects;
import java.util.Properties;
import java.util.UUID;
public class Solution {
public static final String APPLICATION_NAME = "session_windows";
private static final String APPLICATION_ID = APPLICATION_NAME + "_" + UUID.randomUUID();
private static final String CLIENT_ID = APPLICATION_NAME + "_client";
public static final String BOOTSTRAP_SERVERS = "localhost:9092";
private Path STATE_DIR;
private KafkaStreams streams;
private Topology buildTopology(String inputTopic, String outputTopic) {
StreamsBuilder builder = new StreamsBuilder();
// variable
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
.withZone(ZoneId.systemDefault());
Consumed<String, String> consumed = Consumed.with(Serdes.String(), Serdes.String());
Produced<String, Long> produced = Produced.with(Serdes.String(), Serdes.Long());
SessionWindows sessionWindows = SessionWindows.ofInactivityGapWithNoGrace(Duration.ofSeconds(5));
KeyValueMapper<Windowed<String>, Long, KeyValue<String, Long>> keyValueMapper = (key, value) -> KeyValue.pair(
key.key() + "@"
+ formatter.format(key.window().startTime()) + "-"
+ formatter.format(key.window().endTime()),
value
);
// input
KStream<String, String> inputKStream = builder
.stream(inputTopic, consumed)
.peek((key, value) ->
System.out.println(
"input from topic(" + inputTopic
+ ") -> key='" + (Objects.nonNull(key) ? key : "null")
+ "' value='" + (Objects.nonNull(value) ? value : "null") + "'"
)
);
// transform
KTable<Windowed<String>, Long> userEventKTable = inputKStream
.groupByKey()
.windowedBy(sessionWindows)
.count();
// output
userEventKTable
.toStream()
.map(keyValueMapper)
.peek((key, value) ->
System.out.println(
"output to topic(" + outputTopic
+ ") -> key='" + (Objects.nonNull(key) ? key : "null")
+ "' value='" + (Objects.nonNull(value) ? value : "null") + "'"
)
)
.to(outputTopic, produced);
return builder.build();
}
public void startStream(String inputTopic, String outputTopic) {
Topology topology = buildTopology(inputTopic, outputTopic);
System.out.println("Topology of " + APPLICATION_NAME);
System.out.println(topology.describe());
try {
STATE_DIR = Files.createTempDirectory(APPLICATION_ID).toAbsolutePath();
} catch (IOException ioException) {
}
streams = new KafkaStreams(topology, getStreamsConfiguration());
streams.start();
// Wait briefly to ensure the topology is ready
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void stopStream() {
if (Objects.nonNull(streams)) {
streams.close();
}
if (Objects.nonNull(STATE_DIR)) {
try {
Files.walk(STATE_DIR)
.sorted(Comparator.reverseOrder())
.forEach(file -> {
try {
Files.delete(file);
} catch (IOException ioException) {
}
}
);
} catch (IOException ioException) {
}
}
}
private Properties getStreamsConfiguration() {
Properties props = new Properties();
// Give the Streams application a unique name. The name must be unique in the Kafka cluster
// against which the application is run.
props.put(StreamsConfig.APPLICATION_ID_CONFIG, APPLICATION_ID);
props.put(StreamsConfig.CLIENT_ID_CONFIG, CLIENT_ID);
// Where to find Kafka broker(s).
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
// Specify default (de)serializers for record keys and for record values.
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
// Records should be flushed every 100 ms. This is less than the default
// in order to keep this example interactive.
props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 200); // faster commit for testing
// For illustrative purposes we disable record caches.
//props.put(StreamsConfig.STATESTORE_CACHE_MAX_BYTES_CONFIG, 0);
// Use a temporary directory for storing state, which will be automatically removed after the test.
props.put(StreamsConfig.STATE_DIR_CONFIG, STATE_DIR.toString());
return props;
}
public <T> Serde<T> getSerde(TypeReference<T> typeRef) {
ObjectMapper mapper = new ObjectMapper();
return Serdes.serdeFrom(
(topic, data) -> {
try {
return mapper.writeValueAsString(data).getBytes(StandardCharsets.UTF_8);
} catch (IOException e) {
System.err.println(e.getMessage());
return null;
}
},
(topic, data) -> {
try {
return mapper.readValue(new String(data, StandardCharsets.UTF_8), typeRef);
} catch (IOException e) {
System.err.println(e.getMessage());
return null;
}
}
);
}
}