-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodeconAverageStream.java
136 lines (109 loc) · 4.84 KB
/
CodeconAverageStream.java
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
package codecon;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.KGroupedStream;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import codecon.avro.CountAndSum;
import codecon.avro.Rating;
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
import org.apache.kafka.streams.errors.LogAndContinueExceptionHandler;
import static io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG;
import static java.lang.Integer.parseInt;
import static java.lang.Short.parseShort;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toMap;
import static org.apache.kafka.common.serialization.Serdes.Double;
import static org.apache.kafka.common.serialization.Serdes.Long;
import static org.apache.kafka.streams.StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG;
import static org.apache.kafka.streams.StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG;
import static org.apache.kafka.streams.kstream.Grouped.with;
public class CodeconAverageStream {
private final Properties props;
private final String inputTopic;
private final Integer inputTopicPartitions;
private final Short inputTopicReplicationFactor;
private final String outputTopic;
private final Integer outputTopicPartitions;
private final Short outputTopicReplicationFactor;
public CodeconAverageStream(Properties props) {
this.props = props;
this.inputTopic = props.getProperty("input.topic.name");
this.inputTopicPartitions = Integer.parseInt(props.getProperty("input.topic.partitions"));
this.inputTopicReplicationFactor = Short.parseShort(props.getProperty("input.topic.replication.factor"));
this.outputTopic = props.getProperty("output.topic.name");
this.outputTopicPartitions = Integer.parseInt(props.getProperty("output.topic.partitions"));
this.outputTopicReplicationFactor = Short.parseShort(props.getProperty("output.topic.replication.factor"));
}
public void createTopics() {
AdminClient client = AdminClient.create(props);
client.createTopics(List.of(
new NewTopic(inputTopic, inputTopicPartitions, inputTopicReplicationFactor),
new NewTopic(outputTopic, outputTopicPartitions, outputTopicReplicationFactor)
));
client.close();
}
public static SpecificAvroSerde<CountAndSum> buildCountAndSumAvroSerde(Properties props) {
Map<String, String> config = Map.of(
SCHEMA_REGISTRY_URL_CONFIG, props.getProperty("schema.registry.url")
);
SpecificAvroSerde<CountAndSum> avro = new SpecificAvroSerde<>();
avro.configure(config, false);
return avro;
}
public static SpecificAvroSerde<Rating> buildRatingAvroSerde(Properties props) {
Map<String, String> config = Map.of(
SCHEMA_REGISTRY_URL_CONFIG, props.getProperty("schema.registry.url")
);
SpecificAvroSerde<Rating> avro = new SpecificAvroSerde<>();
avro.configure(config, false);
return avro;
}
public Topology buildTopology() {
StreamsBuilder builder = new StreamsBuilder();
// TODO Implementar
return builder.build();
}
private void run() {
createTopics();
KafkaStreams streams = new KafkaStreams(buildTopology(), props);
CountDownLatch latch = new CountDownLatch(1);
// Attach shutdown handler to catch Control-C.
Runtime.getRuntime().addShutdownHook(new Thread("shutdown-hook") {
@Override
public void run() {
streams.close();
latch.countDown();
}
});
try {
streams.cleanUp();
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
}
public static void main(String[] args) throws Exception {
if (args.length < 1) {
throw new IllegalArgumentException("This program takes the path to an environment configuration file as argument.");
}
Properties props = Helpers.loadProperties(args[0]);
props.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, Long().getClass());
props.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, Double().getClass());
props.put("default.deserialization.exception.handler", LogAndContinueExceptionHandler.class);
new CodeconAverageStream(props).run();
}
}