projects
/
demos
/
kafka
/
wordcount
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
query: 2.0.0 - Defined 2 state-stores (all state in-memory in tests)
[demos/kafka/wordcount]
/
src
/
main
/
java
/
de
/
juplo
/
kafka
/
wordcount
/
query
/
QueryStreamProcessor.java
diff --git
a/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java
b/src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java
index
cc65fce
..
4749264
100644
(file)
--- a/
src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java
+++ b/
src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java
@@
-5,7
+5,6
@@
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.*;
-import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
@@
-23,7
+22,8
@@
import java.util.Properties;
@Slf4j
public class QueryStreamProcessor
{
@Slf4j
public class QueryStreamProcessor
{
- public static final String STORE_NAME = "rankings-by-username";
+ public static final String USER_STORE_NAME = "users";
+ public static final String RANKING_STORE_NAME = "rankings";
public final KafkaStreams streams;
public final HostInfo hostInfo;
public final KafkaStreams streams;
public final HostInfo hostInfo;
@@
-35,25
+35,34
@@
public class QueryStreamProcessor
HostInfo applicationServer,
String usersInputTopic,
String rankingInputTopic,
HostInfo applicationServer,
String usersInputTopic,
String rankingInputTopic,
- KeyValueBytesStoreSupplier storeSupplier)
+ KeyValueBytesStoreSupplier userStoreSupplier,
+ KeyValueBytesStoreSupplier rankingStoreSupplier)
{
Topology topology = buildTopology(
usersInputTopic,
rankingInputTopic,
{
Topology topology = buildTopology(
usersInputTopic,
rankingInputTopic,
- storeSupplier);
+ userStoreSupplier,
+ rankingStoreSupplier);
streams = new KafkaStreams(topology, props);
hostInfo = applicationServer;
streams = new KafkaStreams(topology, props);
hostInfo = applicationServer;
- storeParameters = StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore());;
+ storeParameters = StoreQueryParameters.fromNameAndType(
RANKING_
STORE_NAME, QueryableStoreTypes.keyValueStore());;
}
static Topology buildTopology(
String usersInputTopic,
String rankingInputTopic,
}
static Topology buildTopology(
String usersInputTopic,
String rankingInputTopic,
- KeyValueBytesStoreSupplier storeSupplier)
+ KeyValueBytesStoreSupplier userStoreSupplier,
+ KeyValueBytesStoreSupplier rankingStoreSupplier)
{
StreamsBuilder builder = new StreamsBuilder();
{
StreamsBuilder builder = new StreamsBuilder();
- KTable<String, User> users = builder.table(usersInputTopic);
+ KTable<String, User> users = builder
+ .stream(usersInputTopic)
+ .toTable(
+ Materialized
+ .<String, User>as(userStoreSupplier)
+ .withKeySerde(Serdes.String())
+ .withValueSerde(new JsonSerde().copyWithType(User.class)));
KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
rankings
KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
rankings
@@
-63,7
+72,7
@@
public class QueryStreamProcessor
ranking.getEntries()))
.toTable(
Materialized
ranking.getEntries()))
.toTable(
Materialized
- .<String, UserRanking>as(
s
toreSupplier)
+ .<String, UserRanking>as(
rankingS
toreSupplier)
.withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
Topology topology = builder.build();
.withValueSerde(new JsonSerde().copyWithType(UserRanking.class)));
Topology topology = builder.build();
@@
-79,7
+88,7
@@
public class QueryStreamProcessor
public Optional<URI> getRedirect(String username)
{
public Optional<URI> getRedirect(String username)
{
- KeyQueryMetadata metadata = streams.queryMetadataForKey(STORE_NAME, username, Serdes.String().serializer());
+ KeyQueryMetadata metadata = streams.queryMetadataForKey(
RANKING_
STORE_NAME, username, Serdes.String().serializer());
HostInfo activeHost = metadata.activeHost();
log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());
HostInfo activeHost = metadata.activeHost();
log.debug("Local store for {}: {}, {}:{}", username, metadata.partition(), activeHost.host(), activeHost.port());