clickhouse是什么?

clickhouse集群搭建

刚开始搭建集群的时候,发现有很多相关的博客,但是难过的是都不太完整,跟着博客操作总是达不到效果,最后自己也是根据官网的doc一步步的进行,读文档,读配置文件,如果你跟着博客已经失败了2次,那么我觉得你应该看官网的文档了。clickhouse官方集群部署文档

过程梳理

1

单机部署clickhouse-server

2

修改配置文件,添加metrika.xml文件

3

使用配置文件,启动clickhouse-server,检查

你没看错,就是这么简单。我用的是docker,更简单,搭建三个分片的集群。

1、单机部署clickhouse-server
  • 启动docker
docker run -d -p 8123:8123 -p 9000:9000 \ 
--name some-clickhouse-server \ 
--ulimit nofile=262144:262144 \ 
--volume=$HOME/some_clickhouse_database:/var/lib/clickhouse yandex/clickhouse-server

$HOME是你自己建的一个目录,用来存放clickhouse的数据

  • copy出配置文件
docker cp imageId:/etc/clickhouse-server/ .
2、修改配置

找到config.xml ,查找到remote_servers这个属性,添加incl=“remote_servers” (这个remote_servers是有讲究的,metrika.xml 里面会做配置的)

1、
<remote_servers incl="remote_servers"> 
# 这个说明不使用config中的remote_servers,而使用新增的remote_servers

2、注释配置
<![CDATA[
<test_shard_localhost_1>
...
</test_shard_localhost_1>
]]>

3、添加配置
<include_from>/etc/clickhouse-server/metrika.xml</include_from>
#这个表示使用/etc/clickhouse-server/metrika.xml作为新增的配置 对应的就是incl的内容

4、其他配置
<timezone>Asia/Shanghai</timezone> # 时间配置
<listen_host>0.0.0.0</listen_host> # 访问配置
  • metrika.xml配置(灵活修改你的配置。配置内容也是博大精深,具体解释看官网!)
    看到remote_servers这个了吗?incl配置的是啥?
# 创建在clickhouse-server目录中
touch metrika.xml
# 添加配置
<yandex>
<remote_servers>
    <dixiu_cluster>
        <shard>
          <weight>1</weight>
          <internal_replication>false</internal_replication>
            <replica>
                <host>{ip或者host}</host>
                <port>9000</port>
                <priority>1</priority>
            </replica>
        </shard>
        <shard>
          <weight>1</weight>
          <internal_replication>false</internal_replication>
            <replica>
                <host>{ip或者host}</host>
                <port>9000</port>
                <priority>1</priority>
            </replica>
        </shard>
        <shard>
          <weight>1</weight>
          <internal_replication>false</internal_replication>
            <replica>
                <host>{ip或者host}</host>
                <port>9000</port>
                <priority>1</priority>
            </replica>
        </shard>
    </dixiu_cluster>
</remote_servers>
<!-- ZK  -->
   <zookeeper-servers>
       <node index="1">
           <host>{ip或者host}</host>
           <port>2181</port>
       </node>
       <node index="2">
           <host>{ip或者host}</host>
           <port>2181</port>
       </node>
       <node index="3">
           <host>{ip或者host}</host>
           <port>2181</port>
       </node>
   </zookeeper-servers>

   <networks>
       <ip>::/0</ip>
   </networks>
</yandex>

这样其实配置已经完成了,简单吧!

3、启动集群服务
  • 把配置文件分发到你的多台机器上,scp命令。
  • 启动服务(每一台都启动),灵活配置,记得改
docker run -d \
--name chserver \  # name
--ulimit nofile=262144:262144 \
-p 9000:9000 \
-p 8123:8123 \
-p 9009:9009 \
--volume={本地目录,前面配置的数据文件目录}:/var/lib/clickhouse/  \
--volume={你的配置文件目录}:/etc/clickhouse-server/ \
--add-host {host1}:{ip1} \
--add-host {host2}:{ip2} \
--add-host {host3}:{ip3} \
--hostname {host1} \  # 根据你的机器变化
yandex/clickhouse-server
  • 查询是否连成集群
docker run -it --rm   \ 
--add-host {host1}:{ip1} \
--add-host {host2}:{ip2} \
--add-host {host3}:{ip3} \ 
yandex/clickhouse-client \ 
--host {host} --port 9000
# 执行sql,查询是否有你配置的集群
select * from system.clusters;

这样你的集群基本搭建成功了

数据写入

  • 创建表

首先创建local表,然后创建all表,然后查询all表,详情看集群部署模块

# 创建local
CREATE TABLE tutorial.hits_local (...) ENGINE = MergeTree() ...
# 创建all
CREATE TABLE tutorial.hits_all AS tutorial.hits_local
ENGINE = Distributed(perftest_3shards_1replicas, tutorial, hits_local, rand());

核心是写入local表,在all表中查询,也可以只写all表,由表自己负责分发,但是效率是一样的。
TODO:是否三台机器同时写入local表,效率问题 和 all表的数据问题,待验证,验证过的小伙伴请一定留言我。

  • 代码写入
from clickhouse_driver import Client
bulk_sql = f"insert into {table_name} (*) VALUES {(1,2,3)};"
client = Client(host={})
client_data = client.execute(bulk_sql)
  • 测试
    写入后docker命令打开命令行,查询all表的count即可。