1 package de.juplo.kafka.wordcount.stats;
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.*;
9 import org.apache.kafka.streams.state.HostInfo;
10 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
11 import org.apache.kafka.streams.state.QueryableStoreTypes;
12 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
13 import org.springframework.kafka.support.serializer.JsonSerde;
16 import java.util.Optional;
17 import java.util.Properties;
21 public class StatsStreamProcessor
23 public static final String USER_STORE_NAME = "users";
24 public static final String RANKING_STORE_NAME = "rankings";
26 public final KafkaStreams streams;
27 public final HostInfo hostInfo;
28 public final StoreQueryParameters<ReadOnlyKeyValueStore<String, UserRanking>> storeParameters;
31 public StatsStreamProcessor(
33 HostInfo applicationServer,
34 String usersInputTopic,
35 String rankingInputTopic,
36 KeyValueBytesStoreSupplier userStoreSupplier,
37 KeyValueBytesStoreSupplier rankingStoreSupplier)
39 Topology topology = buildTopology(
43 rankingStoreSupplier);
44 streams = new KafkaStreams(topology, props);
45 hostInfo = applicationServer;
46 storeParameters = StoreQueryParameters.fromNameAndType(RANKING_STORE_NAME, QueryableStoreTypes.keyValueStore());;
49 static Topology buildTopology(
50 String usersInputTopic,
51 String rankingInputTopic,
52 KeyValueBytesStoreSupplier userStoreSupplier,
53 KeyValueBytesStoreSupplier rankingStoreSupplier)
55 StreamsBuilder builder = new StreamsBuilder();
57 KTable<String, User> users = builder
60 Consumed.with(Serdes.String(), new JsonSerde().copyWithType(User.class)))
63 .<String, User>as(userStoreSupplier)
64 .withKeySerde(Serdes.String())
65 .withValueSerde(new JsonSerde().copyWithType(User.class)));
66 KStream<String, Ranking> rankings = builder
67 .<Key, Ranking>stream(rankingInputTopic)
68 .map((key, value) -> new KeyValue<>(key.getChannel(), value));
71 .join(users, (ranking, user) -> UserRanking.of(
74 ranking.getEntries()),
75 Joined.keySerde(Serdes.String()))
78 .<String, UserRanking>as(rankingStoreSupplier)
79 .withKeySerde(Serdes.String())
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(RANKING_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");