-
Notifications
You must be signed in to change notification settings - Fork 6
/
TransfersRecordingServiceJoinStreams.java
173 lines (133 loc) · 6.48 KB
/
TransfersRecordingServiceJoinStreams.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
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
package net.devaction.kafka.transfersrecordingservice.joinstreams;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.consumer.ConsumerConfig;
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.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.KafkaStreams.State;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import org.apache.kafka.streams.state.Stores;
import org.apache.kafka.streams.kstream.Consumed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
import net.devaction.kafka.avro.AccountBalance;
import net.devaction.kafka.avro.Transfer;
import net.devaction.kafka.streams.ExceptionHandler;
import net.devaction.kafka.transfersrecordingservice.config.ConfigReader;
import net.devaction.kafka.transfersrecordingservice.config.ConfigValues;
import net.devaction.kafka.transfersrecordingservice.main.TransfersRecordingService;
/**
* @author Víctor Gil
*
* since August 2019
*/
public class TransfersRecordingServiceJoinStreams
implements TransfersRecordingService{
private static final Logger log = LoggerFactory.getLogger(
TransfersRecordingServiceJoinStreams.class);
private KafkaStreams streams;
private static final String ACCOUNT_BALANCES_TOPIC = "account-balances";
private static final String TRANSFERS_TOPIC = "transfers";
@Override
public void start() {
final ConfigValues configValues = readConfig();
final Properties streamsConfigProperties =
createStreamsConfigProperties(configValues.getBootstrapServers());
final Serde<String> stringSerde = Serdes.String();
final StreamsBuilder builder = new StreamsBuilder();
final KStream<String, Transfer> transfersKStream =
createInputKStream(stringSerde, configValues.getSchemaRegistryUrl(),
builder);
final Serde<AccountBalance> accountBalanceSerde =
createAccountBalanceSerde(configValues.getSchemaRegistryUrl());
final KTable<String,AccountBalance> accountBalancesKTable =
createInputKTable(stringSerde, accountBalanceSerde, builder);
final KStream<String, AccountBalance> newABstream = transfersKStream.leftJoin(
accountBalancesKTable, new TransferAndAccountBalanceJoiner());
newABstream.to(ACCOUNT_BALANCES_TOPIC, Produced.with(stringSerde, accountBalanceSerde));
streams = new KafkaStreams(builder.build(), streamsConfigProperties);
streams.setUncaughtExceptionHandler(new ExceptionHandler());
startStreams();
}
@Override
public void stop(){
log.info("Going to close the \"Streams\"");
streams.close();
}
private void startStreams() {
streams.start();
while (streams.state() != State.RUNNING) {
try{
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ex){
log.error("Interrupted while waiting for the \"Streams\" to start.", ex);
Thread.currentThread().interrupt();
}
}
log.info("\"Streams\" started");
}
private ConfigValues readConfig() {
ConfigValues configValues = null;
log.info("Going to read the configuration values");
try{
configValues = new ConfigReader().read();
} catch (Exception ex){
log.error("Unable to read the configuration values, exiting", ex);
System.exit(1);
}
return configValues;
}
private Properties createStreamsConfigProperties(String bootstrapServers) {
final Properties streamsConfigProperties = new Properties();
streamsConfigProperties.put(StreamsConfig.APPLICATION_ID_CONFIG,
"account-balance-streams-retriever");
streamsConfigProperties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,
bootstrapServers);
streamsConfigProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return streamsConfigProperties;
}
private KStream<String, Transfer> createInputKStream(Serde<String> stringSerde,
String schemaRegistryUrl, StreamsBuilder builder) {
final boolean isKeySerde = false;
final Serde<Transfer> transferSerde = new SpecificAvroSerde<>();
transferSerde.configure(
Collections.singletonMap(
AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
schemaRegistryUrl),
isKeySerde);
return builder.stream(TRANSFERS_TOPIC,
Consumed.with(stringSerde, transferSerde));
}
private KTable<String,AccountBalance> createInputKTable(
Serde<String> stringSerde, Serde<AccountBalance> accountBalanceSerde,
StreamsBuilder builder){
final KeyValueBytesStoreSupplier clientsStoreSupplier =
Stores.inMemoryKeyValueStore("account-balance-store");
return builder.table(
ACCOUNT_BALANCES_TOPIC,
Materialized.<String,AccountBalance>as(clientsStoreSupplier)
.withKeySerde(stringSerde)
.withValueSerde(accountBalanceSerde)
.withCachingDisabled());
}
private Serde<AccountBalance> createAccountBalanceSerde(String schemaRegistryUrl) {
final Serde<AccountBalance> accountBalanceSerde = new SpecificAvroSerde<>();
final boolean isKeySerde = false;
accountBalanceSerde.configure(
Collections.singletonMap(
AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
schemaRegistryUrl),
isKeySerde);
return accountBalanceSerde;
}
}