#1
This guide shows how to connect Kafka to ClickHouse with Spring Boot for streaming and real-time analytics.

1. Create ClickHouse Table with Kafka Engine

ClickHouse can consume directly from Kafka topics.
CREATE TABLE kafka_events (
  id UInt64,
  type String,
  ts DateTime
) ENGINE = Kafka
SETTINGS kafka_broker_list = 'localhost:9092',
         kafka_topic_list = 'events',
         kafka_group_name = 'event_consumer',
         kafka_format = 'JSONEachRow';

2. Materialized View for Storage

Move Kafka data into a MergeTree table.
CREATE TABLE events (
  id UInt64,
  type String,
  ts DateTime
) ENGINE = MergeTree()
ORDER BY ts;

CREATE MATERIALIZED VIEW kafka_to_events
TO events
AS SELECT * FROM kafka_events;

3. Spring Boot Kafka Producer

Add dependency:
<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>
Configure in application.properties:
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

4. Kafka Producer Service

@Service
public class EventProducer {
    @Autowired private KafkaTemplate<String, String> kafkaTemplate;

    public void send(String topic, String message) {
        kafkaTemplate.send(topic, message);
    }
}

5. REST Endpoint to Publish Events

@RestController
@RequestMapping("/events")
public class EventController {
    @Autowired private EventProducer producer;

    @PostMapping
    public String publish(@RequestBody Event e) {
        producer.send("events",
            "{\"id\":"+e.getId()+",\"type\":\""+e.getType()+"\",\"ts\":\""+e.getTs()+"\"}");
        return "Sent";
    }
}

6. Query Data in ClickHouse

Once events flow from Kafka into ClickHouse via materialized view, you can run analytics queries directly:
SELECT type, count(*) FROM events GROUP BY type;

image quote pre code