This guide shows how to perform real-time data ingestion into ClickHouse using Spring Boot for fast analytics and dashboards.
1. Create a Real-Time Table in ClickHouse
Use
MergeTree for continuous inserts.
CREATE TABLE events (
id UInt64,
type String,
ts DateTime
) ENGINE = MergeTree()
ORDER BY (ts);
2. Configure Spring Boot Datasource
spring.datasource.url=jdbc:clickhouse://localhost:8123/default
spring.datasource.username=default
spring.datasource.driver-class-name=com.clickhouse.jdbc.ClickHouseDriver
3. Define Entity
@Entity
@Table(name = "events")
public class Event {
@Id
private Long id;
private String type;
private LocalDateTime ts;
}
4. Repository
public interface EventRepository extends JpaRepository<Event, Long> {}
5. Real-Time Insert Service
@Service
public class EventService {
@Autowired private EventRepository repo;
public void ingest(Event e) {
repo.save(e);
}
}
6. Streaming Inserts via REST Controller
@RestController
@RequestMapping("/events")
public class EventController {
@Autowired private EventService service;
@PostMapping
public String addEvent(@RequestBody Event e) {
service.ingest(e);
return "OK";
}
}
Send events with curl or any client:
curl -X POST http://localhost:8080/events -H "Content-Type: application/json" -d '{"id":1,"type":"CLICK","ts":"2025-08-25T10:00:00"}'
7. Batch Inserts for High Throughput
Use
JdbcTemplate.batchUpdate for real-time streams with high volume.
jdbcTemplate.batchUpdate(
"INSERT INTO events (id, type, ts) VALUES (?, ?, ?)",
events,
500,
(ps, e) -> {
ps.setLong(1, e.getId());
ps.setString(2, e.getType());
ps.setTimestamp(3, Timestamp.valueOf(e.getTs()));
}
);
image quote pre code