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.JsonSerde;
19 import java.util.Optional;
20 import java.util.Properties;
24 public class QueryStreamProcessor
26 public static final String USER_STORE_NAME = "users";
27 public static final String RANKING_STORE_NAME = "rankings";
29 public final KafkaStreams streams;
30 public final HostInfo hostInfo;
31 public final StoreQueryParameters<ReadOnlyKeyValueStore<String, UserRanking>> storeParameters;
34 public QueryStreamProcessor(
36 HostInfo applicationServer,
37 String usersInputTopic,
38 String rankingInputTopic,
39 KeyValueBytesStoreSupplier userStoreSupplier,
40 KeyValueBytesStoreSupplier rankingStoreSupplier)
42 Topology topology = buildTopology(
46 rankingStoreSupplier);
47 streams = new KafkaStreams(topology, props);
48 hostInfo = applicationServer;
49 storeParameters = StoreQueryParameters.fromNameAndType(RANKING_STORE_NAME, QueryableStoreTypes.keyValueStore());;
52 static Topology buildTopology(
53 String usersInputTopic,
54 String rankingInputTopic,
55 KeyValueBytesStoreSupplier userStoreSupplier,
56 KeyValueBytesStoreSupplier rankingStoreSupplier)
58 StreamsBuilder builder = new StreamsBuilder();
60 KTable<String, User> users = builder
63 Consumed.with(null, new JsonSerde().copyWithType(User.class)))
66 .<String, User>as(userStoreSupplier)
67 .withKeySerde(Serdes.String())
68 .withValueSerde(new JsonSerde().copyWithType(User.class)));
69 KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
72 .join(users, (ranking, user) -> UserRanking.of(
75 ranking.getEntries()))
78 .<String, UserRanking>as(rankingStoreSupplier)
79 .withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
81 Topology topology = builder.build();
82 log.info("\n\n{}", topology.describe());
87 ReadOnlyKeyValueStore<String, UserRanking> getStore()
89 return streams.store(storeParameters);
92 public Optional<URI> getRedirect(String username)
94 KeyQueryMetadata metadata = streams.queryMetadataForKey(RANKING_STORE_NAME, username, Serdes.String().serializer());
95 HostInfo activeHost = metadata.activeHost();
96 log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
98 if (activeHost.equals(this.hostInfo) || activeHost.equals(HostInfo.unavailable()))
100 return Optional.empty();
103 URI location = URI.create("http://" + activeHost.host() + ":" + activeHost.port() + "/" + username);
104 log.debug("Redirecting to {}", location);
105 return Optional.of(location);
108 public Optional<UserRanking> getUserRanking(String username)
110 return Optional.ofNullable(getStore().get(username));
116 log.info("Starting Stream-Processor");
123 log.info("Stopping Stream-Processor");