use of org.apache.kafka.common.serialization.Serdes.Double 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;
}
Aggregations