1 package de.juplo.kafka.wordcount.query;
3 import jakarta.annotation.PostConstruct;
4 import jakarta.annotation.PreDestroy;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.common.serialization.Serdes;
7 import org.apache.kafka.streams.*;
8 import org.apache.kafka.streams.kstream.Consumed;
9 import org.apache.kafka.streams.kstream.KStream;
10 import org.apache.kafka.streams.kstream.KTable;
11 import org.apache.kafka.streams.kstream.Materialized;
12 import org.apache.kafka.streams.state.HostInfo;
13 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
14 import org.apache.kafka.streams.state.QueryableStoreTypes;
15 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
16 import org.springframework.kafka.support.serializer.JsonDeserializer;
17 import org.springframework.kafka.support.serializer.JsonSerde;
21 import java.util.Optional;
22 import java.util.Properties;
26 public class QueryStreamProcessor
28 public static final String STORE_NAME = "rankings-by-username";
30 public final KafkaStreams streams;
31 public final HostInfo hostInfo;
32 public final StoreQueryParameters<ReadOnlyKeyValueStore<String, UserRanking>> storeParameters;
35 public QueryStreamProcessor(
37 HostInfo applicationServer,
38 String usersInputTopic,
39 String rankingInputTopic,
40 KeyValueBytesStoreSupplier storeSupplier)
42 Topology topology = buildTopology(
46 streams = new KafkaStreams(topology, props);
47 hostInfo = applicationServer;
48 storeParameters = StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore());;
51 static Topology buildTopology(
52 String usersInputTopic,
53 String rankingInputTopic,
54 KeyValueBytesStoreSupplier storeSupplier)
56 StreamsBuilder builder = new StreamsBuilder();
58 JsonSerde valueSerde = new JsonSerde();
59 valueSerde.configure(Map.of(
60 JsonDeserializer.TYPE_MAPPINGS,
61 "user:" + Key.class.getName() + "," +
62 "ranking:" + Ranking.class.getName() + "," +
63 "userdata:" + User.class.getName() + "," +
64 "userranking:" + UserRanking.class.getName()
66 KTable<String, User> users = builder.table(
68 Consumed.with(null, valueSerde.copyWithType(User.class))
70 KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
73 .join(users, (ranking, user) -> UserRanking.of(
76 ranking.getEntries()))
79 .<String, UserRanking>as(storeSupplier)
80 .withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
82 Topology topology = builder.build();
83 log.info("\n\n{}", topology.describe());
88 ReadOnlyKeyValueStore<String, UserRanking> getStore()
90 return streams.store(storeParameters);
93 public Optional<URI> getRedirect(String username)
95 KeyQueryMetadata metadata = streams.queryMetadataForKey(STORE_NAME, username, Serdes.String().serializer());
96 HostInfo activeHost = metadata.activeHost();
97 log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
99 if (activeHost.equals(this.hostInfo) || activeHost.equals(HostInfo.unavailable()))
101 return Optional.empty();
104 URI location = URI.create("http://" + activeHost.host() + ":" + activeHost.port() + "/" + username);
105 log.debug("Redirecting to {}", location);
106 return Optional.of(location);
109 public Optional<UserRanking> getUserRanking(String username)
111 return Optional.ofNullable(getStore().get(username));
117 log.info("Starting Stream-Processor");
124 log.info("Stopping Stream-Processor");