更新时间:2024-11-28 GMT+08:00

创建表

功能介绍

如下示例中通过on cluster语句在集群的所有Server节点创建分布式表和本地表。

createSql为本地表,createDisSql为基于本地表的分布式表。

样例代码

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);
}