use of io.confluent.demo.Rating in project kafka-tutorials by confluentinc.
the class RunningAverage method getRatingAverageTable.
protected static KTable<Long, Double> getRatingAverageTable(KStream<Long, Rating> ratings, String avgRatingsTopicName, SpecificAvroSerde<CountAndSum> countAndSumSerde) {
// Grouping Ratings
KGroupedStream<Long, Double> ratingsById = ratings.map((key, rating) -> new KeyValue<>(rating.getMovieId(), rating.getRating())).groupByKey(with(Long(), Double()));
final KTable<Long, CountAndSum> ratingCountAndSum = ratingsById.aggregate(() -> new CountAndSum(0L, 0.0), (key, value, aggregate) -> {
aggregate.setCount(aggregate.getCount() + 1);
aggregate.setSum(aggregate.getSum() + value);
return aggregate;
}, Materialized.with(Long(), countAndSumSerde));
final KTable<Long, Double> ratingAverage = ratingCountAndSum.mapValues(value -> value.getSum() / value.getCount(), Materialized.as("average-ratings"));
// persist the result in topic
ratingAverage.toStream().to(avgRatingsTopicName);
return ratingAverage;
}
use of io.confluent.demo.Rating in project kafka-tutorials by confluentinc.
the class RunningAverageTest method validateAverageRating.
@Test
public void validateAverageRating() {
TestInputTopic<Long, Rating> inputTopic = testDriver.createInputTopic(RATINGS_TOPIC_NAME, new LongSerializer(), ratingSpecificAvroSerde.serializer());
inputTopic.pipeKeyValueList(asList(new KeyValue<>(LETHAL_WEAPON_RATING_8.getMovieId(), LETHAL_WEAPON_RATING_8), new KeyValue<>(LETHAL_WEAPON_RATING_10.getMovieId(), LETHAL_WEAPON_RATING_10)));
final TestOutputTopic<Long, Double> outputTopic = testDriver.createOutputTopic(AVERAGE_RATINGS_TOPIC_NAME, new LongDeserializer(), new DoubleDeserializer());
final List<KeyValue<Long, Double>> keyValues = outputTopic.readKeyValuesToList();
// I sent two records to input topic
// I expect second record in topic will contain correct result
final KeyValue<Long, Double> longDoubleKeyValue = keyValues.get(1);
System.out.println("longDoubleKeyValue = " + longDoubleKeyValue);
MatcherAssert.assertThat(longDoubleKeyValue, equalTo(new KeyValue<>(362L, 9.0)));
final KeyValueStore<Long, Double> keyValueStore = testDriver.getKeyValueStore("average-ratings");
final Double expected = keyValueStore.get(362L);
Assert.assertEquals("Message", expected, 9.0, 0.0);
}
use of io.confluent.demo.Rating in project kafka-tutorials by confluentinc.
the class RunningAverageTest method setUp.
@Before
public void setUp() {
final Properties mockProps = new Properties();
mockProps.put("application.id", "kafka-movies-test");
mockProps.put("bootstrap.servers", "DUMMY_KAFKA_CONFLUENT_CLOUD_9092");
mockProps.put("schema.registry.url", "mock://DUMMY_SR_CONFLUENT_CLOUD_8080");
mockProps.put("default.topic.replication.factor", "1");
mockProps.put("offset.reset.policy", "latest");
mockProps.put("specific.avro.reader", true);
final RunningAverage streamsApp = new RunningAverage();
final Properties streamsConfig = streamsApp.buildStreamsProperties(mockProps);
StreamsBuilder builder = new StreamsBuilder();
SpecificAvroSerde<CountAndSum> countAndSumSerde = RunningAverage.getCountAndSumSerde(mockProps);
ratingSpecificAvroSerde = RunningAverage.getRatingSerde(mockProps);
KStream<Long, Rating> ratingStream = builder.stream(RATINGS_TOPIC_NAME, Consumed.with(Serdes.Long(), ratingSpecificAvroSerde));
RunningAverage.getRatingAverageTable(ratingStream, AVERAGE_RATINGS_TOPIC_NAME, countAndSumSerde);
final Topology topology = builder.build();
testDriver = new TopologyTestDriver(topology, streamsConfig);
}
Aggregations