Updated on 2024-04-29 GMT+08:00

Creating a Table

Function Description

Run the on cluster statement to create the ReplicatedMerge and Distributed tables whose names are the same as the values of tableName in clickhouse-example.properties.

Sample Code

private void createTable(String databaseName, String tableName, String clusterName) throws Exception {
	String createSql = "create table " + databaseName + "." + tableName + " on cluster " + clusterName
			+ " (name String, age UInt8, date Date)engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/" + databaseName
			+ "." + tableName + "'," + "'{replica}') partition by toYYYYMM(date) order by age";
	String createDisSql = "create table " + databaseName + "." + tableName + "_all" + " on cluster " + clusterName + " as "
			+ databaseName + "." + tableName + " ENGINE = Distributed(default_cluster," + databaseName + "," + tableName + ", rand());";
	ArrayList<String> sqlList = new ArrayList<String>();
	sqlList.add(createSql);
	sqlList.add(createDisSql);
	util.exeSql(sqlList);
}