Q:

Can Apache Kafka be used to aggregate hourly consumption data per customer?

For example electricity consumption per customer. Consumption data not streaming all the time but inserted in batches of last 12 hours per some amount of customers. After insertion we need to aggregate hourly consumption per customer and if previous hour consumption for some customer not exists to find "closest by date" consumption.

A:

Did you check out Kafka Streams (https://kafka.apache.org/documentation/streams/)?
It allows you to read topics as data streams and aggregate on time windows:
StreamsBuilder builder = new StreamsBuilder();
builder.stream("topic-name")
  .groupByKey() // assuming the key is a customer-ID
  .windowedBy(TimeWindows.of(Duration.ofHours(1)))
  .aggregate(...); // insert business logic here

