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 USER_STORE_NAME = "users";
26 public static final String RANKING_STORE_NAME = "rankings";
28 public final KafkaStreams streams;
29 public final HostInfo hostInfo;
30 public final StoreQueryParameters<ReadOnlyKeyValueStore<String, UserRanking>> storeParameters;
33 public QueryStreamProcessor(
35 HostInfo applicationServer,
36 String usersInputTopic,
37 String rankingInputTopic,
38 KeyValueBytesStoreSupplier userStoreSupplier,
39 KeyValueBytesStoreSupplier rankingStoreSupplier)
41 Topology topology = buildTopology(
45 rankingStoreSupplier);
46 streams = new KafkaStreams(topology, props);
47 hostInfo = applicationServer;
48 storeParameters = StoreQueryParameters.fromNameAndType(RANKING_STORE_NAME, QueryableStoreTypes.keyValueStore());;
51 static Topology buildTopology(
52 String usersInputTopic,
53 String rankingInputTopic,
54 KeyValueBytesStoreSupplier userStoreSupplier,
55 KeyValueBytesStoreSupplier rankingStoreSupplier)
57 StreamsBuilder builder = new StreamsBuilder();
59 KTable<String, User> users = builder
60 .stream(usersInputTopic)
63 .<String, User>as(userStoreSupplier)
64 .withKeySerde(Serdes.String())
65 .withValueSerde(new JsonSerde().copyWithType(User.class)));
66 KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
69 .join(users, (ranking, user) -> UserRanking.of(
72 ranking.getEntries()))
75 .<String, UserRanking>as(rankingStoreSupplier)
76 .withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
78 Topology topology = builder.build();
79 log.info("\n\n{}", topology.describe());
84 ReadOnlyKeyValueStore<String, UserRanking> getStore()
86 return streams.store(storeParameters);
89 public Optional<URI> getRedirect(String username)
91 KeyQueryMetadata metadata = streams.queryMetadataForKey(RANKING_STORE_NAME, username, Serdes.String().serializer());
92 HostInfo activeHost = metadata.activeHost();
93 log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
95 if (activeHost.equals(this.hostInfo) || activeHost.equals(HostInfo.unavailable()))
97 return Optional.empty();
100 URI location = URI.create("http://" + activeHost.host() + ":" + activeHost.port() + "/" + username);
101 log.debug("Redirecting to {}", location);
102 return Optional.of(location);
105 public Optional<UserRanking> getUserRanking(String username)
107 return Optional.ofNullable(getStore().get(username));
113 log.info("Starting Stream-Processor");
120 log.info("Stopping Stream-Processor");