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.KStream;
9 import org.apache.kafka.streams.kstream.KTable;
10 import org.apache.kafka.streams.kstream.Materialized;
11 import org.apache.kafka.streams.state.HostInfo;
12 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
13 import org.apache.kafka.streams.state.QueryableStoreTypes;
14 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
15 import org.springframework.kafka.support.serializer.JsonSerde;
18 import java.util.Optional;
19 import java.util.Properties;
23 public class QueryStreamProcessor
25 public static final String STORE_NAME = "rankings-by-username";
27 public final KafkaStreams streams;
28 public final HostInfo hostInfo;
29 public final StoreQueryParameters<ReadOnlyKeyValueStore<String, UserRanking>> storeParameters;
32 public QueryStreamProcessor(
34 HostInfo applicationServer,
35 String usersInputTopic,
36 String rankingInputTopic,
37 KeyValueBytesStoreSupplier storeSupplier)
39 Topology topology = buildTopology(
43 streams = new KafkaStreams(topology, props);
44 hostInfo = applicationServer;
45 storeParameters = StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore());;
48 static Topology buildTopology(
49 String usersInputTopic,
50 String rankingInputTopic,
51 KeyValueBytesStoreSupplier storeSupplier)
53 StreamsBuilder builder = new StreamsBuilder();
55 KTable<String, User> users = builder
56 .stream(usersInputTopic)
57 .toTable(Materialized.with(null, new JsonSerde().copyWithType(User.class)));
58 KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
61 .join(users, (ranking, user) -> UserRanking.of(
64 ranking.getEntries()))
67 .<String, UserRanking>as(storeSupplier)
68 .withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
70 Topology topology = builder.build();
71 log.info("\n\n{}", topology.describe());
76 ReadOnlyKeyValueStore<String, UserRanking> getStore()
78 return streams.store(storeParameters);
81 public Optional<URI> getRedirect(String username)
83 KeyQueryMetadata metadata = streams.queryMetadataForKey(STORE_NAME, username, Serdes.String().serializer());
84 HostInfo activeHost = metadata.activeHost();
85 log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
87 if (activeHost.equals(this.hostInfo) || activeHost.equals(HostInfo.unavailable()))
89 return Optional.empty();
92 URI location = URI.create("http://" + activeHost.host() + ":" + activeHost.port() + "/" + username);
93 log.debug("Redirecting to {}", location);
94 return Optional.of(location);
97 public Optional<UserRanking> getUserRanking(String username)
99 return Optional.ofNullable(getStore().get(username));
105 log.info("Starting Stream-Processor");
112 log.info("Stopping Stream-Processor");