@Slf4j
public class QueryStreamProcessor
{
+ public static final String STATS_TYPE = "COUNTER";
public static final String USER_STORE_NAME = "users";
public static final String RANKING_STORE_NAME = "rankings";
.withValueSerde(new JsonSerde().copyWithType(User.class)));
KStream<String, Ranking> rankings = builder
.<Key, Ranking>stream(rankingInputTopic)
- .map((key, value) -> new KeyValue<>(key.getUser(), value));
+ .filter((key, value) -> STATS_TYPE.equals(key.getType()))
+ .map((key, value) -> new KeyValue<>(key.getChannel(), value));
rankings
.join(users, (ranking, user) -> UserRanking.of(
Map<String, Object> properties = Map.of(
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
- JsonSerializer.TYPE_MAPPINGS, "user:" + TestUser.class.getName() + ",ranking:" + TestRanking.class.getName());
+ JsonSerializer.TYPE_MAPPINGS, "stats:" + TestUser.class.getName() + ",ranking:" + TestRanking.class.getName());
return new KafkaTemplate(producerFactory, properties);
}
import java.util.function.Function;
import java.util.stream.Stream;
+import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.STATS_TYPE;
import static org.assertj.core.api.Assertions.assertThat;
class TestData
{
- static final TestUser PETER = TestUser.of("peter");
- static final TestUser KLAUS = TestUser.of("klaus");
+ static final TestUser PETER = TestUser.of(STATS_TYPE, "peter");
+ static final TestUser KLAUS = TestUser.of(STATS_TYPE, "klaus");
static final Stream<KeyValue<TestUser, TestRanking>> getTop10Messages()
{
static void assertExpectedState(Function<String, UserRanking> function)
{
- assertRankingEqualsRankingFromLastMessage(PETER.getUser(), function.apply(PETER.getUser()));
- assertRankingEqualsRankingFromLastMessage(KLAUS.getUser(), function.apply(KLAUS.getUser()));
+ assertRankingEqualsRankingFromLastMessage(PETER.getChannel(), function.apply(PETER.getChannel()));
+ assertRankingEqualsRankingFromLastMessage(KLAUS.getChannel(), function.apply(KLAUS.getChannel()));
}
private static void assertRankingEqualsRankingFromLastMessage(String user, UserRanking rankingJson)
private static UserRanking getLastMessageFor(String user)
{
return getTop10Messages()
- .filter(kv -> kv.key.getUser().equals(user))
+ .filter(kv -> kv.key.getChannel().equals(user))
.map(kv -> kv.value)
.map(testRanking -> userRankingFor(user, testRanking))
.reduce(null, (left, right) -> right);
private static Entry entryOf(TestEntry testEntry)
{
Entry entry = new Entry();
- entry.setWord(testEntry.getWord());
+ entry.setKey(testEntry.getKey());
entry.setCounter(testEntry.getCounter());
return entry;
}
private static KeyValue<String, TestUserData>[] USERS_MESSAGES = new KeyValue[]
{
KeyValue.pair(
- PETER.getUser(),
- TestUserData.of(PETER.getUser(), "Peter", "Pan", TestUserData.Sex.MALE)),
+ PETER.getChannel(),
+ TestUserData.of(PETER.getChannel(), "Peter", "Pan", TestUserData.Sex.MALE)),
KeyValue.pair(
- KLAUS.getUser(),
- TestUserData.of(KLAUS.getUser(), "Klaus", "Klüse", TestUserData.Sex.OTHER)),
+ KLAUS.getChannel(),
+ TestUserData.of(KLAUS.getChannel(), "Klaus", "Klüse", TestUserData.Sex.OTHER)),
};
}