ClickHouse: Java (clickhouse-jdbc)
Plain JDBC over the ClickHouse HTTP interface: create the table, batch insert, aggregate.
The only change
Section titled “The only change”# CLICKHOUSE_JDBC_URL jdbc:clickhouse:https://clickhouse.internal:8443/default jdbc:clickhouse:https://warehouse.internal:8443/default# CLICKHOUSE_USER / CLICKHOUSE_PASSWORD ClickHouse user MachWarehouse usermvn -q compile exec:javaLibrary: com.clickhouse:clickhouse-jdbc 0.11.0 (all classifier), Java 25 LTS. The same URL works in DBeaver, DataGrip, Tableau (JDBC) and any JDBC tool.
Source
Section titled “Source”The program as it is kept and tested in the Mach Database Warehouse repository; highlighted lines read the connection setting.
package example;
import java.nio.file.Files;import java.nio.file.Path;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.List;import java.util.Properties;
/** Loads the demo events with clickhouse-jdbc and prints revenue per country. Plain JDBC code. */public final class Main { private Main() {}
public static void main(String[] args) throws Exception { String url = System.getenv().getOrDefault("CLICKHOUSE_JDBC_URL", "jdbc:clickhouse:https://localhost:8443/default"); Properties props = new Properties(); props.setProperty("user", System.getenv().getOrDefault("CLICKHOUSE_USER", "default")); props.setProperty("password", System.getenv("CLICKHOUSE_PASSWORD")); Path dataset = Path.of("../../dataset");
try (Connection conn = DriverManager.getConnection(url, props)) { try (Statement st = conn.createStatement()) { st.execute(Files.readString(dataset.resolve("clickhouse.sql"))); } List<String> lines = Files.readAllLines(dataset.resolve("events.csv")); try (PreparedStatement ps = conn.prepareStatement("INSERT INTO events VALUES (?, ?, ?, ?, ?, ?)")) { for (String line : lines.subList(1, lines.size())) { String[] f = line.split(","); ps.setDate(1, java.sql.Date.valueOf(f[0])); ps.setTimestamp(2, java.sql.Timestamp.valueOf(f[1])); ps.setLong(3, Long.parseLong(f[2])); ps.setString(4, f[3]); ps.setString(5, f[4]); ps.setBigDecimal(6, new java.math.BigDecimal(f[5])); ps.addBatch(); } ps.executeBatch(); } try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery( "SELECT country, count() AS events, sum(revenue) AS revenue FROM events GROUP BY country ORDER BY revenue DESC")) { while (rs.next()) { System.out.printf("%s\t%d\t%s%n", rs.getString(1), rs.getLong(2), rs.getBigDecimal(3)); } } } }}<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>example.machwarehouse</groupId> <artifactId>clickhouse-jdbc-example</artifactId> <version>1.0.0</version> <properties> <maven.compiler.release>25</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <exec.mainClass>example.Main</exec.mainClass> </properties> <dependencies> <dependency> <groupId>com.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.11.0</version> <classifier>all</classifier> </dependency> </dependencies></project>