use of io.confluent.demo.CountAndSum 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.CountAndSum 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