Skip to content

clickhouse 建表

Posted on:May 15, 2022 at 09:50 AM

背景

背景是目前在使用clickhouse,想自己搭建一个实例,并且建表成功

zookeeper

#### 切换到bin目录
cd apache-zookeeper-3.8.0-bin/bin/
## 启动zk
./zkServer.sh
### 启动zkCli
cd apache-zookeeper-3.8.0-bin/bin/
### 启动节点
./zkCli.sh
### 创建zk节点 需要一级一级建
### 创建节点命令: create path
[zk: localhost:2181(CONNECTED) 11] create /path
Created /path
[zk: localhost:2181(CONNECTED) 12] create /path/to
Created /path/to
[zk: localhost:2181(CONNECTED) 13] create /path/to/zookeeper
Created /path/to/zookeeper
[zk: localhost:2181(CONNECTED) 14] create /path/to/zookeeper/node
Created /path/to/zookeeper/node

建表

建表之前先要把cluseter配置好 我的配置:

<!-- zk 配置 -->
    <zookeeper>
        <node>
            <host>localhost</host>
            <port>2181</port>
        </node>
        <session_timeout_ms>30000</session_timeout_ms>
        <operation_timeout_ms>10000</operation_timeout_ms>
        <!-- Optional. Chroot suffix. Should exist. -->
        <root>/path/to/zookeeper/node</root>

    </zookeeper>
    <!-- 宏变量, 建表的时候指定的path中的变量从宏里面读取 -->
    <macros>
        <cluster>testcluster</cluster>
        <shard>01</shard>
        <replica>example01-01-1</replica>
    </macros>
   <remote_servers>
      <!-- cluster 名称叫做 testcluster , 名字随便取的-->
    <testcluster>
        <shard>
            <replica>
                <host>localhost</host>
                <port>9000</port>
            </replica>
        </shard>
    </testcluster>
  </remote_servers>
<distributed_ddl>

建表语句:

### 这里的{cluster} 就是上面配置的testcluster
CREATE TABLE test ON CLUSTER `{cluster}`
(
    `timestamp` DateTime,
    `contractid` UInt32,
    `userid` UInt32
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{cluster}/{shard}/default/test', '{replica}')
PARTITION BY toYYYYMM(timestamp)
ORDER BY (contractid, toDate(timestamp), userid)
SAMPLE BY userid

Query id: 56c07fac-9a0b-4b0b-bf8f-fb808ce452e6

查询zk配置

SELECT  path  FROM system.zookeeper

遇到错误

遇到错误:There is no DistributedDDL configuration in server config

原因是: clickhosue的配置没有配对,需要参考上面给的链接添加配置

<distributed_ddl>
  <!-- Path in ZooKeeper to queue with DDL queries -->
  <path>/clickhouse/task_queue/ddl</path>
  <cleanup_delay_period>60</cleanup_delay_period>
  <task_max_lifetime>86400</task_max_lifetime>
  <max_tasks_in_queue>1000</max_tasks_in_queue>
</distributed_ddl>