更新时间:2024-10-18 GMT+08:00
分享

创建ClickHouse表

本章节介绍创建ClickHouse表样例代码。

以下代码片段在com.huawei.clickhouse.examples包的“Demo”类的createTable方法中。

通过on cluster语句在集群中创建表1中tableName参数值为表名的ReplicatedMerge表和Distributed表。

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

相关文档