大数据系列 | 详解基于Zookeeper或ClickHouse Keeper的ClickHouse集群部署- ...

打印 上一主题 下一主题

主题 1616|帖子 1616|积分 4848

1. ClickHouse与MySQL的区别

   ● 数据存储方式:
   MySQL是关系型数据库管理体系(RDBMS),它使用表格来组织数据,并支持SQL查询语言。
   ClickHouse是列式数据库管理体系(Columnar DBMS),它以列为单元存储数据,这使得它在处理大量数据时非常高效。
   ● 用途:
   MySQL通常用于事务处理(OLTP)工作负载,比如Web应用步伐的后端数据库,支持高并发的读写操作。
   ClickHouse则更适合于分析型(OLAP)工作负载,用于大规模数据分析和陈诉,以及实时数据分析。
   ● 性能:
   ClickHouse在处理大量数据时通常比MySQL更快,尤其是在数据分析和聚合查询方面。
   MySQL在处理事务处理方面可能更快,因为它是为支持事务处理而计划的。
   ● 数据复制和高可用性:
   MySQL有成熟的复制和高可用性方案,如MySQL Replication和Galera Cluster,可以确保数据的可靠性和可用性。
   ClickHouse也有一些复制和高可用性的办理方案,但相对来说还不如MySQL那么成熟。
   事务的概念:
   事务(Transaction)是指数据库管理体系执行的一组操作,被视为一个逻辑工作单元,要么全部执行成功,要么全部不执行。事务具有以下四个特性,通常称为 ACID 特性:
   原子性(Atomicity):事务是一个原子操作,要么全部执行成功,要么全部不执行。如果事务中的任何一部分操作失败,整个事务都会被回滚(Rollback),即撤销之前的操作,保持数据库状态一致性。
   一致性(Consistency):事务执行的结果必须使数据库从一个一致性状态转变到另一个一致性状态。换句话说,事务执行后,数据库必须保持满足预界说的约束和完备性规则的状态。
   隔离性(Isolation):并发执行的事务之间应该相互隔离,即一个事务的执行不应该受到其他事务的影响。事务应该像是在独立的执行环境中运行一样,尽管它们可能同时运行。
   持久性(Durability):一旦事务提交(Commit),其结果应该永久保存在数据库中,纵然体系崩溃或重启,也应该可以或许恢复到提交后的状态。
   接下来通俗的案例来解释MySQL中的事务:
   假设你在网上有一个小型的书店,你的网站上允许人们购买书籍。现在你需要确保每一次购买都是可靠的,制止出现错误的情况。现在,让我们来看看一次范例的购买流程,以及如何使用MySQL的事务来保证它的可靠性:
   开始事务:当有人点击购买按钮时,你需要开始一个事务。这就好像给购买过程打了个标记,告诉体系:“我现在要进行一次购买操作了,请留意!”
   扣除库存:在购买过程中,你需要查抄库存是否充足,并且将库存数量相应地减少。这一步是在事务中完成的,如果库存不敷或者出现其他错误,你可以回滚事务,即取消这次购买操作。
   生成订单:如果库存充足,你就可以为购买生成一个订单,记载购买者的信息、购买的书籍以及付款信息等。这个过程也在事务中进行,确保订单的生成是原子性的操作,要么全部成功,要么全部失败。
   扣款:最后,你需要从购买者的银行账户中扣款,完成付出过程。同样地,在事务中进行,以保证付出的精确性和可靠性。
   提交事务:如果上述所有步骤都顺遂完成,你可以提交事务,告诉体系:“购买操作已经完成,可以更新数据库并关照用户了!”
   完成购买:一旦事务提交成功,购买操作就完成了。你可以更新库存数量,发送确认邮件给购买者,以及做其他须要的处理。
   通过MySQL的事务的原子性,购买过程中的每个步骤要么全部成功执行,要么全部失败回滚。纵然在处理过程中出现了错误,也可以保证数据库的一致性(库存一致性、订单一致性、付出一致性)和完备性。
2. 在群集的所有机器上安装ClickHouse服务端

   官网部署文档:https://clickhouse.com/docs/zh/getting-started/tutorial
   RPM软件包下载:https://packages.clickhouse.com/rpm/stable/
  1. hostnamectl set-hostname ck1
  2. hostnamectl set-hostname ck2
  3. hostnamectl set-hostname ck3
  4. hostnamectl set-hostname ck4
复制代码
  1. cat /etc/hosts
  2. 127.0.0.1 localhost
  3. 127.0.0.1 vm10-1-248-33.cloud.local
  4. 10.1.248.33  ck1
  5. 10.1.248.247 ck2
  6. 10.1.248.68  ck3
  7. 10.1.248.16  ck4
复制代码
  在线安装或者离线安装任选其中一种方式,每台节点都需要安装
     各个节点之间要做无密钥登录
2.1. 在线安装clickhouse

   推荐使用CentOS、RedHat和所有其他基于rpm的Linux发行版的官方预编译rpm包。
   首先,您需要添加官方存储库:
  1. yum install -y yum-utils
  2. yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
  3. yum install -y clickhouse-server clickhouse-client
复制代码
  1. rpm -qa | grep clickhouse-server
  2. clickhouse-server-24.6.2.17-1.x86_64
  3. rpm -qa | grep clickhouse-client
  4. clickhouse-client-24.6.2.17-1.x86_64
复制代码
  1. /etc/init.d/clickhouse-server start
  2. chown -R clickhouse: '/var/run/clickhouse-server/'
  3. Will run sudo --preserve-env -u 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon
  4. Waiting for server to start
  5. Waiting for server to start
  6. Server started
复制代码
   设置登录密码:
  1. 方式一(随机):
  2. PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
  3. xS3UbICi
  4. 019a756a1aaf0d0df25f3507978bbb0c375ed97cfa6282832cbc95143e7b7e4e
  5. 方式二(指定):
  6. echo -n 123456 | openssl dgst -sha256
  7. <password_sha256_hex>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</password_sha256_hex>
复制代码
   修改users.xml使密码收效:
  1. vim /etc/clickhouse-server/users.xml
  2. <!-- <password></password> -->   // 注销此行
  3. <password_sha256_hex>019a756a1aaf0d0df25f3507978bbb0c375ed97cfa6282832cbc95143e7b7e4e</password_sha256_hex>
复制代码
  1. systemctl restart clickhouse-server
  2. systemctl enable clickhouse-server
复制代码
  1. clickhouse-client                            // 如果没有密码直接使用此命令执行
  2. clickhouse-client --password        // 如果有密码需要指定--password
  3. ClickHouse client version 24.6.2.17 (official build).
  4. Password for user (default):    // 输入密码
  5. Connecting to localhost:9000 as user default.
  6. Connected to ClickHouse server version 24.6.2.
  7. Warnings:
  8. * Linux transparent hugepages are set to "always". Check /sys/kernel/mm/transparent_hugepage/enabled
  9. * Maximum number of threads is lower than 30000. There could be problems with handling a lot of simultaneous queries.
  10. clickhouse1 :) quit
  11. Bye.
复制代码
2.2. 离线安装clickhouse

  1. export LATEST_VERSION=24.6.2.17
  2. curl -O https://packages.clickhouse.com/rpm/stable/clickhouse-client-$LATEST_VERSION.x86_64.rpm
  3. curl -O https://packages.clickhouse.com/rpm/stable/clickhouse-common-static-$LATEST_VERSION.x86_64.rpm
  4. curl -O https://packages.clickhouse.com/rpm/stable/clickhouse-common-static-dbg-$LATEST_VERSION.x86_64.rpm
  5. curl -O https://packages.clickhouse.com/rpm/stable/clickhouse-server-$LATEST_VERSION.x86_64.rpm
复制代码
   clickhouse-common-static :ClickHouse编译的二进制文件。
   clickhouse-server :创建clickhouse-server软连接,并安装默认设置服务
   clickhouse-client :创建clickhouse-client客户端工具软连接,并安装客户端设置文件。
   clickhouse-common-static-dbg :带有调试信息的ClickHouse二进制文件
  1. rpm -ivh clickhouse-common-static-24.6.2.17.x86_64.rpm
  2. rpm -ivh clickhouse-common-static-dbg-24.6.2.17.x86_64.rpm
  3. rpm -ivh clickhouse-client-24.6.2.17.x86_64.rpm
  4. rpm -ivh clickhouse-server-24.6.2.17.x86_64.rpm
  5. ClickHouse binary is already located at /usr/bin/clickhouse
  6. Symlink /usr/bin/clickhouse-server already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  7. Creating symlink /usr/bin/clickhouse-server to /usr/bin/clickhouse.
  8. Symlink /usr/bin/clickhouse-client already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  9. Creating symlink /usr/bin/clickhouse-client to /usr/bin/clickhouse.
  10. Symlink /usr/bin/clickhouse-local already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  11. Creating symlink /usr/bin/clickhouse-local to /usr/bin/clickhouse.
  12. Symlink /usr/bin/clickhouse-benchmark already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  13. Creating symlink /usr/bin/clickhouse-benchmark to /usr/bin/clickhouse.
  14. Symlink /usr/bin/clickhouse-obfuscator already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  15. Creating symlink /usr/bin/clickhouse-obfuscator to /usr/bin/clickhouse.
  16. Creating symlink /usr/bin/clickhouse-git-import to /usr/bin/clickhouse.
  17. Symlink /usr/bin/clickhouse-compressor already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  18. Creating symlink /usr/bin/clickhouse-compressor to /usr/bin/clickhouse.
  19. Symlink /usr/bin/clickhouse-format already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  20. Creating symlink /usr/bin/clickhouse-format to /usr/bin/clickhouse.
  21. Symlink /usr/bin/clickhouse-extract-from-config already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  22. Creating symlink /usr/bin/clickhouse-extract-from-config to /usr/bin/clickhouse.
  23. Symlink /usr/bin/clickhouse-keeper already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  24. Creating symlink /usr/bin/clickhouse-keeper to /usr/bin/clickhouse.
  25. Symlink /usr/bin/clickhouse-keeper-converter already exists but it points to /clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
  26. Creating symlink /usr/bin/clickhouse-keeper-converter to /usr/bin/clickhouse.
  27. Creating symlink /usr/bin/clickhouse-disks to /usr/bin/clickhouse.
  28. Symlink /usr/bin/ch already exists. Will keep it.
  29. Symlink /usr/bin/chl already exists. Will keep it.
  30. Symlink /usr/bin/chc already exists. Will keep it.
  31. Creating clickhouse group if it does not exist.
  32. groupadd -r clickhouse
  33. Creating clickhouse user if it does not exist.
  34. useradd -r --shell /bin/false --home-dir /nonexistent -g clickhouse clickhouse
  35. Will set ulimits for clickhouse user in /etc/security/limits.d/clickhouse.conf.
  36. Creating config directory /etc/clickhouse-server/config.d that is used for tweaks of main server configuration.
  37. Creating config directory /etc/clickhouse-server/users.d that is used for tweaks of users configuration.
  38. Config file /etc/clickhouse-server/config.xml already exists, will keep it and extract path info from it.
  39. /etc/clickhouse-server/config.xml has /var/lib/clickhouse/ as data path.
  40. /etc/clickhouse-server/config.xml has /var/log/clickhouse-server/ as log path.
  41. Users config file /etc/clickhouse-server/users.xml already exists, will keep it and extract users info from it.
  42. Creating log directory /var/log/clickhouse-server/.
  43. Creating data directory /var/lib/clickhouse/.
  44. Creating pid directory /var/run/clickhouse-server.
  45. chown -R clickhouse:clickhouse '/var/log/clickhouse-server/'
  46. chown -R clickhouse:clickhouse '/var/run/clickhouse-server'
  47. chown  clickhouse:clickhouse '/var/lib/clickhouse/'
  48. Enter password for the default user:     // 输入密码(xS3UbICi)
  49. Password for the default user is saved in file /etc/clickhouse-server/users.d/default-password.xml.
  50. Setting capabilities for clickhouse binary. This is optional.
  51. Cannot set 'net_admin' or 'ipc_lock' or 'sys_nice' or 'net_bind_service' capability for clickhouse binary. This is optional. Taskstats accounting will be disabled. To enable taskstats accounting you may add the required capability later manually.
  52. chown -R clickhouse:clickhouse '/etc/clickhouse-server'
  53. ClickHouse has been successfully installed.
  54. Start clickhouse-server with:
  55. sudo clickhouse start
  56. Start clickhouse-client with:
  57. clickhouse-client --password
  58. Created symlink from /etc/systemd/system/multi-user.target.wants/clickhouse-server.service to /usr/lib/systemd/system/clickhouse-server.service.
复制代码
   检察保存的密码(使用sha256编码格式存储的):
  1. cat /etc/clickhouse-server/users.d/default-password.xml
  2. <clickhouse>
  3.     <users>
  4.         <default>
  5.             <password remove='1' />
  6.             <password_sha256_hex>019a756a1aaf0d0df25f3507978bbb0c375ed97cfa6282832cbc95143e7b7e4e</password_sha256_hex>
  7.         </default>
  8.     </users>
  9. </clickhouse>
复制代码
   启动clickhouse-server:
  1. systemctl start clickhouse-server
  2. [root@clickhouse2 ~]# ps -ef | grep clickhouse
  3. clickho+ 24511     1  0 13:50 ?        00:00:00 clickhouse-watchdog        --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid
  4. clickho+ 24512 24511 27 13:50 ?        00:00:01 /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid
复制代码
  1. clickhouse-client --password
  2. ClickHouse client version 24.6.2.17 (official build).
  3. Password for user (default):
  4. Connecting to localhost:9000 as user default.
  5. Connected to ClickHouse server version 24.6.2.
  6. Warnings:
  7. * Linux transparent hugepages are set to "always". Check /sys/kernel/mm/transparent_hugepage/enabled
  8. * Maximum number of threads is lower than 30000. There could be problems with handling a lot of simultaneous queries.
  9. clickhouse2 :)
复制代码
3. ClickHouse Keeper/Zookeeper集群安装

   为什么使用zookeeper?不用也可以,但建议还是使用。ClickHouse提供了非常高级的基于zookeeper的表复制方式,同时也提供了基于Cluster的复制方式。
   clickhouse keeper可以和clickhouse集群使用类似的主机,也可以单独运行在其他主机上
   clickhouse keeper包含在clickhouse软件包中,以是如果clickhouse keeper单独出来部署一个集群,也是需要安装clickhouse-server软件,然后按照如下设置config.xml文件内容,再启动clickhouse-server服务
  1. <listen_host>0.0.0.0</listen_host>
复制代码
  1. <keeper_server>
  2.     <tcp_port>2181</tcp_port>
  3.     <server_id>1</server_id>     // 每台主机的id不一样
  4.     <raft_configuration>
  5.         <server>
  6.             <id>1</id>
  7.             <hostname>keeper1</hostname>
  8.             <port>9444</port>
  9.         </server>
  10.         <server>
  11.             <id>2</id>
  12.             <hostname>keeper2</hostname>
  13.             <port>9444</port>
  14.         </server>
  15.         <server>
  16.             <id>3</id>
  17.             <hostname>keeper3</hostname>
  18.             <port>9444</port>
  19.         </server>
  20.     </raft_configuration>
  21. </keeper_server>
复制代码
4. 在设置文件中设置集群设置

   ClickHouse的设置主要为config.xml、user.xml
   修改config.xml文件:
  1. cp /etc/clickhouse-server/config.xml /etc/clickhouse-server/config.xml.bak
  2. vim /etc/clickhouse-server/config.xml
复制代码
   设置listen_host字段:
  1. <listen_host>0.0.0.0</listen_host>
复制代码
   设置remote_server字段:
   找到设置文件中的remote_server标签,发现它里面有许多的内容,我们没有都用到,它只是给我一个例子,把里面的内容都删除,粘贴上我们自己想要的:
   设置2个分片,每个分片包含2个副本:
  1.     <remote_servers>
  2.         <cluster1>                        // 集群名称
  3.             <shard>
  4.                 <replica>
  5.                     <host>ck1</host>
  6.                     <port>9000</port>
  7.                     <user>default</user>
  8.                     <password>123.com</password>
  9.                 </replica>
  10.                 <replica>
  11.                     <host>ck3</host>
  12.                     <port>9000</port>
  13.                     <user>default</user>
  14.                     <password>123.com</password>
  15.                 </replica>
  16.                 <internal_replication>true</internal_replication>   // 将内部复制设置为true
  17.             </shard>
  18.             <shard>
  19.                 <replica>
  20.                     <host>ck2</host>
  21.                     <port>9000</port>
  22.                     <user>default</user>
  23.                     <password>123.com</password>
  24.                 </replica>
  25.                 <replica>
  26.                     <host>ck4</host>
  27.                     <port>9000</port>
  28.                     <user>default</user>
  29.                     <password>123.com</password>
  30.                 </replica>
  31.                 <internal_replication>true</internal_replication>
  32.             </shard>
  33.         </cluster1>
  34.     </remote_servers>
复制代码
   四台设置完成后查抄ClickHouse是否在集群体系表中显示新的Cluster集群设置:
  1. ck1 :) SELECT cluster FROM system.clusters;
  2. SELECT cluster
  3. FROM system.clusters
  4. Query id: 0f8461fd-c06c-4a39-8f13-9dee83ada84a
  5. Password for user (default):
  6. Connecting to localhost:9000 as user default.
  7. Connected to ClickHouse server version 24.6.2.
  8.    ┌─cluster──┐
  9. 1. │ cluster1 │
  10. 2. │ cluster1 │
  11. 3. │ cluster1 │
  12. 4. │ cluster1 │
  13.    └──────────┘
  14. 4 rows in set. Elapsed: 0.002 sec.
复制代码
   设置macros字段: 根据每台机器的分片副本设置:
   CK1节点设置如下:
  1. <macros>
  2.     <shard>1</shard>
  3.     <replica>1</replica>
  4. </macros>
复制代码
   修改CK3节点设置如下:
  1. <macros>
  2.     <shard>1</shard>
  3.     <replica>2</replica>
  4. </macros>
复制代码
   修改CK2节点设置如下:
  1. <macros>
  2.     <shard>2</shard>
  3.     <replica>1</replica>
  4. </macros>
复制代码
   修改CK4节点设置如下:
  1. <macros>
  2.     <shard>2</shard>
  3.     <replica>2</replica>
  4. </macros>
复制代码
   设置文件是热更新的,以是修改设置后无需要重启服务
  1. ck1 :) SELECT cluster,shard_num,replica_num,host_name,port FROM system.clusters WHERE cluster = 'cluster1' ORDER BY shard_num ASC,replica_num ASC;
  2. SELECT
  3.     cluster,
  4.     shard_num,
  5.     replica_num,
  6.     host_name,
  7.     port
  8. FROM system.clusters
  9. WHERE cluster = 'cluster1'
  10. ORDER BY
  11.     shard_num ASC,
  12.     replica_num ASC
  13. Query id: 47c42f7b-50b6-4988-bff6-49b9792662ff
  14.    ┌─cluster──┬─shard_num─┬─replica_num─┬─host_name─┬─port─┐
  15. 1. │ cluster1 │         1 │           1 │ ck1       │ 9000 │
  16. 2. │ cluster1 │         1 │           2 │ ck3       │ 9000 │
  17. 3. │ cluster1 │         2 │           1 │ ck2       │ 9000 │
  18. 4. │ cluster1 │         2 │           2 │ ck4       │ 9000 │
  19.    └──────────┴───────────┴─────────────┴───────────┴──────┘
  20. 4 rows in set. Elapsed: 0.004 sec.
复制代码
   为了集群复制需要设置zookeeper字段
   无论是使用clickhouse keeper还是zookeeper,设置都使用zookeeper节点
  1.     <zookeeper>
  2.         <node>
  3.             <host>10.1.248.23</host>
  4.             <port>2181</port>
  5.         </node>
  6.         <node>
  7.             <host>10.1.248.59</host>
  8.             <port>2181</port>
  9.         </node>
  10.         <node>
  11.             <host>10.1.248.173</host>
  12.             <port>2181</port>
  13.         </node>
  14.     </zookeeper>
复制代码
   设置完成后重启clickhouse,查抄并检察zookeeper体系数据库是否存在:
  1. ck1 :) SELECT * FROM system.zookeeper WHERE path IN ('/','clickhouse');
  2. SELECT *
  3. FROM system.zookeeper
  4. WHERE path IN ('/', 'clickhouse')
  5. Query id: cc0bbdb1-60b8-4be6-8f56-b73ad616586b
  6.    ┌─name───────┬─value─┬─path───────┐
  7. 1. │ sessions   │       │ clickhouse │
  8. 2. │ task_queue │       │ clickhouse │
  9. 3. │ zookeeper  │       │ /          │
  10. 4. │ clickhouse │       │ /          │
  11.    └────────────┴───────┴────────────┘
  12. 4 rows in set. Elapsed: 0.003 sec.
复制代码
   其他clickhouse节点也需要进行同样的设置
   在clickhouse中执行的DDL语句在zookeeper中就存在消息队列,检察zookeeper中的消息队列:
  1. [zk: localhost:2181(CONNECTED) 4] ls /clickhouse/task_queue/ddl
  2. [query-0000000000]
复制代码
   删除消息队列:
  1. deleteall /clickhouse/task_queue/ddl
复制代码
5. 在每个实例上创建当地表

  1. ck1 :) CREATE TABLE default.local_table(EventDate DateTime, Number UInt32, id UInt32 ) ENGINE = MergeTree() PARTITION BY toYYYYMM(EventDate) ORDER BY (Number, EventDate, intHash32(id)) SAMPLE BY intHash32(id);
  2. CREATE TABLE default.local_table
  3. (
  4.     `EventDate` DateTime,
  5.     `Number` UInt32,
  6.     `id` UInt32
  7. )
  8. ENGINE = MergeTree
  9. PARTITION BY toYYYYMM(EventDate)
  10. ORDER BY (Number, EventDate, intHash32(id))
  11. SAMPLE BY intHash32(id)
  12. Query id: f9b0e82d-f288-424b-9d2e-f9ef13b0f389
  13. Ok.
  14. 0 rows in set. Elapsed: 0.007 sec.
  15. ck1 :) show tables;
  16. SHOW TABLES
  17. Query id: 3a3c8a9e-5a5c-4e0c-96b5-e71cdd91b819
  18.    ┌─name────────┐
  19. 1. │ local_table │
  20.    └─────────────┘
  21. 1 row in set. Elapsed: 0.003 sec.
复制代码
   加上on cluster创建一个集群表:
   使用on cluster子句对cluster1执行创建数据库的DDL查询,clickhouse将自动在所有4台主机上创建数据库:
  1. ck1 :) CREATE DATABASE my_db ON CLUSTER cluster1;
  2. CREATE DATABASE my_db ON CLUSTER cluster1
  3. Query id: 20cfe4e4-8001-4009-a132-076c0e13c816
  4.    ┌─host─┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
  5. 1. │ ck1  │ 9000 │      0 │       │                   3 │                0 │
  6. 2. │ ck4  │ 9000 │      0 │       │                   2 │                0 │
  7. 3. │ ck2  │ 9000 │      0 │       │                   1 │                0 │
  8. 4. │ ck3  │ 9000 │      0 │       │                   0 │                0 │
  9.    └──────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
  10. 4 rows in set. Elapsed: 0.069 sec.
复制代码
   如果创建失败,检察clickhouse日志报如下错误:
  1. tail -f /var/log/clickhouse-server/clickhouse-server.log
  2. 2024.07.15 05:02:11.192027 [ 26122 ] {} <Debug> DDLWorker: Scheduling tasks
  3. 2024.07.15 05:02:11.192125 [ 26141 ] {} <Debug> DDLWorker: Cleaning queue
  4. 2024.07.15 05:02:11.194949 [ 26122 ] {} <Trace> DDLWorker: scheduleTasks: initialized=true, size_before_filtering=5, queue_size=5, entries=query-0000000000..query-0000000004, first_failed_task_name=none, current_tasks_size=0, last_current_task=none, last_skipped_entry_name=query-0000000003
  5. 2024.07.15 05:02:11.194979 [ 26122 ] {} <Debug> DDLWorker: Will schedule 1 tasks starting from query-0000000004
  6. 2024.07.15 05:02:11.194992 [ 26122 ] {} <Trace> DDLWorker: Checking task query-0000000004
  7. 2024.07.15 05:02:11.197936 [ 26122 ] {} <Debug> DDLWorker: Will not execute task query-0000000004: There is no a local address in host list
  8. 2024.07.15 05:02:11.197985 [ 26122 ] {} <Debug> DDLWorker: Waiting for queue updates
复制代码
  故障原因是因为我使用的云主机,在remote_servers的设置中肯定要使用网卡的IP地址,不要使用云主机的弹性IP
  1. ck4 :) show databases
  2. SHOW DATABASES
  3. Query id: c8f89ea9-9125-4c47-8ba7-a7937ce5d670
  4.    ┌─name───────────────┐
  5. 1. │ INFORMATION_SCHEMA │
  6. 2. │ default            │
  7. 3. │ information_schema │
  8. 4. │ my_db              │
  9. 5. │ system             │
  10.    └────────────────────┘
  11. 5 rows in set. Elapsed: 0.002 sec.
复制代码
   接下来创建一个复制表,再次使用on cluster子句针对cluster1,clickhouse将自动在每个主机上创建四个表(2个分片2个副本),主机1和主机3上的两个表是两个分片中的一个的两个副本。主机2和主机4上的两个表是另一个分片的副本。
   复制型归并树表需要两个参数才可以正常工作,第一个是zookeeper的路径,这是指表的元数据路径,第二个是表的副本名称。所有其其它副本会定期从类似的zookeeper路径中拉取复制日志,他们会留意新的条目并读取它,然后通过内部http复制协议从源副本下载新数据
  1. CREATE TABLE my_db.my_table1 ON CLUSTER cluster1
  2. (
  3.     `WatchID` UInt64,
  4.     `JavaEnable` UInt8,
  5.     `Title` String,
  6.     `GoodEvent` Int16,
  7.     `EventTime` DateTime,
  8.     `EventDate` Date,
  9.     `CounterID` UInt32,
  10.     `ClientIP` UInt32,
  11.     `ClientIP6` FixedString(16),
  12.     `RegionID` UInt32,
  13.     `UserID` UInt64,
  14.     `CounterClass` Int8,
  15.     `OS` UInt8,
  16.     `UserAgent` UInt8,
  17.     `URL` String,
  18.     `Referer` String,
  19.     `URLDomain` String,
  20.     `RefererDomain` String,
  21.     `Refresh` UInt8,
  22.     `IsRobot` UInt8,
  23.     `RefererCategories` Array(UInt16),
  24.     `URLCategories` Array(UInt16),
  25.     `URLRegions` Array(UInt32),
  26.     `RefererRegions` Array(UInt32),
  27.     `ResolutionWidth` UInt16,
  28.     `ResolutionHeight` UInt16,
  29.     `ResolutionDepth` UInt8,
  30.     `FlashMajor` UInt8,
  31.     `FlashMinor` UInt8,
  32.     `FlashMinor2` String,
  33.     `NetMajor` UInt8,
  34.     `NetMinor` UInt8,
  35.     `UserAgentMajor` UInt16,
  36.     `UserAgentMinor` FixedString(2),
  37.     `CookieEnable` UInt8,
  38.     `JavascriptEnable` UInt8,
  39.     `IsMobile` UInt8,
  40.     `MobilePhone` UInt8,
  41.     `MobilePhoneModel` String,
  42.     `Params` String,
  43.     `IPNetworkID` UInt32,
  44.     `TraficSourceID` Int8,
  45.     `SearchEngineID` UInt16,
  46.     `SearchPhrase` String,
  47.     `AdvEngineID` UInt8,
  48.     `IsArtifical` UInt8,
  49.     `WindowClientWidth` UInt16,
  50.     `WindowClientHeight` UInt16,
  51.     `ClientTimeZone` Int16,
  52.     `ClientEventTime` DateTime,
  53.     `SilverlightVersion1` UInt8,
  54.     `SilverlightVersion2` UInt8,
  55.     `SilverlightVersion3` UInt32,
  56.     `SilverlightVersion4` UInt16,
  57.     `PageCharset` String,
  58.     `CodeVersion` UInt32,
  59.     `IsLink` UInt8,
  60.     `IsDownload` UInt8,
  61.     `IsNotBounce` UInt8,
  62.     `FUniqID` UInt64,
  63.     `HID` UInt32,
  64.     `IsOldCounter` UInt8,
  65.     `IsEvent` UInt8,
  66.     `IsParameter` UInt8,
  67.     `DontCountHits` UInt8,
  68.     `WithHash` UInt8,
  69.     `HitColor` FixedString(1),
  70.     `UTCEventTime` DateTime,
  71.     `Age` UInt8,
  72.     `Sex` UInt8,
  73.     `Income` UInt8,
  74.     `Interests` UInt16,
  75.     `Robotness` UInt8,
  76.     `GeneralInterests` Array(UInt16),
  77.     `RemoteIP` UInt32,
  78.     `RemoteIP6` FixedString(16),
  79.     `WindowName` Int32,
  80.     `OpenerName` Int32,
  81.     `HistoryLength` Int16,
  82.     `BrowserLanguage` FixedString(2),
  83.     `BrowserCountry` FixedString(2),
  84.     `SocialNetwork` String,
  85.     `SocialAction` String,
  86.     `HTTPError` UInt16,
  87.     `SendTiming` Int32,
  88.     `DNSTiming` Int32,
  89.     `ConnectTiming` Int32,
  90.     `ResponseStartTiming` Int32,
  91.     `ResponseEndTiming` Int32,
  92.     `FetchTiming` Int32,
  93.     `RedirectTiming` Int32,
  94.     `DOMInteractiveTiming` Int32,
  95.     `DOMContentLoadedTiming` Int32,
  96.     `DOMCompleteTiming` Int32,
  97.     `LoadEventStartTiming` Int32,
  98.     `LoadEventEndTiming` Int32,
  99.     `NSToDOMContentLoadedTiming` Int32,
  100.     `FirstPaintTiming` Int32,
  101.     `RedirectCount` Int8,
  102.     `SocialSourceNetworkID` UInt8,
  103.     `SocialSourcePage` String,
  104.     `ParamPrice` Int64,
  105.     `ParamOrderID` String,
  106.     `ParamCurrency` FixedString(3),
  107.     `ParamCurrencyID` UInt16,
  108.     `GoalsReached` Array(UInt32),
  109.     `OpenstatServiceName` String,
  110.     `OpenstatCampaignID` String,
  111.     `OpenstatAdID` String,
  112.     `OpenstatSourceID` String,
  113.     `UTMSource` String,
  114.     `UTMMedium` String,
  115.     `UTMCampaign` String,
  116.     `UTMContent` String,
  117.     `UTMTerm` String,
  118.     `FromTag` String,
  119.     `HasGCLID` UInt8,
  120.     `RefererHash` UInt64,
  121.     `URLHash` UInt64,
  122.     `CLID` UInt32,
  123.     `YCLID` UInt64,
  124.     `ShareService` String,
  125.     `ShareURL` String,
  126.     `ShareTitle` String,
  127.     `ParsedParams` Nested(
  128.         Key1 String,
  129.         Key2 String,
  130.         Key3 String,
  131.         Key4 String,
  132.         Key5 String,
  133.         ValueDouble Float64),
  134.     `IslandID` FixedString(16),
  135.     `RequestNum` UInt32,
  136.     `RequestTry` UInt8
  137. )
  138. ENGINE = ReplicatedMergeTree ('/clickhouse/tables/my_table1/{shard}', '{replica}')
  139. PRIMARY KEY(CounterID,EventDate,UserID,EventTime,WatchID);
  140. Query id: 4f2bd2f5-1e2f-49b9-bb67-5db6c8842efa
  141.    ┌─host─┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
  142. 1. │ ck1  │ 9000 │      0 │       │                   3 │                0 │
  143. 2. │ ck4  │ 9000 │      0 │       │                   2 │                0 │
  144. 3. │ ck2  │ 9000 │      0 │       │                   1 │                0 │
  145. 4. │ ck3  │ 9000 │      0 │       │                   0 │                0 │
  146.    └──────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
  147. 4 rows in set. Elapsed: 0.167 sec.
复制代码
  1. // 修改数据类型
  2. ALTER table my_db.my_table1_distributed on cluster cluster1 MODIFY COLUMN ClientIP Int64;
复制代码
   建表语句基本语法如下:
  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name ON CLUSTER cluster
  2. (
  3.     name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
  4.     name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
  5.     ...
  6.     INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1,
  7.     INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2
  8. ) ENGINE = engine_name()
  9. [PARTITION BY expr]
  10. [ORDER BY expr]
  11. [PRIMARY KEY expr]
  12. [SAMPLE BY expr]
  13. [SETTINGS name=value, ...];
复制代码
   选项描述:
   ● db:指定命据库名称,如果当前语句没有包含‘db’,则默认使用当前选择的数据库为‘db’。
   ● cluster:指定集群名称,现在固定为default。ON CLUSTER 将在每一个节点上都创建一个当地表。
   ● type:该列数据范例,比方 UInt32。
   ● DEFAULT:该列缺省值。如果INSERT中不包含指定的列,那么将通过表达式盘算它的默认值并填充它。
   ● MATERIALIZED:物化列表达式,表示该列不能被INSERT,是被盘算出来的; 在INSERT语句中,不需要写入该列;在SELECT *查询语句结果集不包含该列。
   ● ALIAS :别名列,如许的列不会存储在表中。 它的值不可以或许通过INSERT写入,同时使用SELECT查询星号时,这些列也不会被用来替换星号。 但是它们可以用于SELECT中,在这种情况下,在查询分析中别名将被替换。
   物化列与别名列的区别: 物化列是会保存数据,查询的时候不需要盘算,而别名列不会保存数据,查询的时候需要盘算,查询时候返回表达式的盘算结果
     以下选项与表引擎相关,只有MergeTree系列表引擎支持:
   ● PARTITION BY:指定分区键。通常按照日期分区,也可以用其他字段或字段表达式。
   ● ORDER BY:指定 排序键。可以是一组列的元组或任意的表达式。
   ● PRIMARY KEY: 指定主键,默认情况下主键跟排序键类似。因此,大部分情况下不需要再专门指定一个 PRIMARY KEY 子句。
   ● SAMPLE BY :抽样表达式,如果要用抽样表达式,主键中必须包含这个表达式。
   ● SETTINGS:影响 性能的额外参数。
   ● GRANULARITY :索引粒度参数。
   ● {shard},{replica} 参数不需要赋值。
   高可用集群(双副本),要用ReplicatedMergeTree等Replicated系列引擎,否则副本之间不进行数据复制,导致数据查询结果不一致。
     分别在4个节点都检察下是否同步了新建表:
  1. ck1 :) use my_db;
  2. USE my_db
  3. Query id: 4b1be3f0-f408-41fc-ac85-044190111512
  4. Ok.
  5. 0 rows in set. Elapsed: 0.002 sec.
  6. ck1 :) show tables;
  7. SHOW TABLES
  8. Query id: 92639317-9f4d-4dfd-8b6f-25fc475b9cc9
  9.    ┌─name──────┐
  10. 1. │ my_table1 │
  11.    └───────────┘
  12. 1 row in set. Elapsed: 0.004 sec.
复制代码
  1. ck2 :) use my_db;
  2. USE my_db
  3. Query id: 732b124f-4c5e-4224-8a40-0dbe6cd45071
  4. Ok.
  5. 0 rows in set. Elapsed: 0.002 sec.
  6. ck2 :) show tables;
  7. SHOW TABLES
  8. Query id: 2a6b6ccb-bf35-4542-87a1-3c2125a7c381
  9.    ┌─name──────┐
  10. 1. │ my_table1 │
  11.    └───────────┘
  12. 1 row in set. Elapsed: 0.004 sec.
复制代码
  1. ck3 :) use my_db;
  2. USE my_db
  3. Query id: 3282e619-d2b4-4227-9e86-2bef0705e77d
  4. Ok.
  5. 0 rows in set. Elapsed: 0.002 sec.
  6. ck3 :) show tables;
  7. SHOW TABLES
  8. Query id: 8352c44e-13e6-435c-aace-c2337e262b4f
  9.    ┌─name──────┐
  10. 1. │ my_table1 │
  11.    └───────────┘
  12. 1 row in set. Elapsed: 0.006 sec.
复制代码
  1. ck4 :) use my_db;
  2. USE my_db
  3. Query id: b8e06690-7a12-45a5-bc2c-01d8575a0f1b
  4. Ok.
  5. 0 rows in set. Elapsed: 0.002 sec.
  6. ck4 :) show tables;
  7. SHOW TABLES
  8. Query id: 9720c5d2-b8c0-4509-87d3-e426859c56f0
  9.    ┌─name──────┐
  10. 1. │ my_table1 │
  11.    └───────────┘
  12. 1 row in set. Elapsed: 0.004 sec.
复制代码
6. 创建一个分布式表

   分布式表本身不存储任何数据, 但是它提供了一个单一的表接口,用于同一访问位于不同主机上的远程表。
   可以在一台或者多台主机上创建分布式表,当查询针对分布式表时,分布式表会将查询转发给所有主机,等待来自分片的查询结果,然后盘算并返回整个查询结果
   直接将一个查询发送到其中一个远程表也是可以的,有时出于特定的数据分布原因如许做也是公道的

 
   分片和复制是完全独立的,复制用于数据完备性和自动故障转移,分片用于集群的程度扩展,也可以不使用分片。仅使用复制来保证数据的高度可用
  

   host3主机故障之后,host1也会正常对外提供服务,当host3主机恢复后,host1的会自动同步副本给host3
   clickhouse keeper或者zookeeper提供了数据复制的协调体系,用于关照分片的副本状态变化,以确保分片的所有副本按照类似的顺序执行类似的操作
   clickhouse keeper只在clickhouse中存储元数据
  

   接下来创建一个分布式表,以便位于不同主机上的远程表提供单一表接口的同一登录。可以在一个或者多个主机上创建分布式表:
   当分布式表指向当前服务器上创建的表时,可以使用as子句自动接纳该表的模式
  1. ck1 :) CREATE TABLE my_db.my_table1_distributed ON CLUSTER cluster1
  2. AS my_db.my_table1
  3. ENGINE = Distributed(cluster1,my_db,my_table1, rand());
  4. CREATE TABLE my_db.my_table1_distributed ON CLUSTER cluster1 AS my_db.my_table1
  5. ENGINE = Distributed(cluster1, my_db, my_table1, rand())
  6. Query id: 4e8d8a6b-defe-4436-bfd8-36c50c888af9
  7.    ┌─host─┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
  8. 1. │ ck1  │ 9000 │      0 │       │                   3 │                0 │
  9. 2. │ ck2  │ 9000 │      0 │       │                   2 │                0 │
  10. 3. │ ck4  │ 9000 │      0 │       │                   1 │                0 │
  11. 4. │ ck3  │ 9000 │      0 │       │                   0 │                0 │
  12.    └──────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
  13. 4 rows in set. Elapsed: 0.069 sec.
复制代码
   为展示ClickHouse的性能,下载一个ClickBench数据集,包含一亿条记载。接下来将一亿条记载加载到刚才创建的分布式表中。
   因为上面创建的分布式表使用了rand()函数作为分布式表的分片键,rand函数返回一个在所有整数中均匀分布的伪随机整数,因此一亿条记载将均匀分布在ClickHouse集群的两个分片上,每个分片应该包含约莫五万万条记载,每个分片的两个副本最终应该包含类似的五万万行
   ClickBench下载地址:https://github.com/ClickHouse/ClickBench
https://datasets.clickhouse.com/hits_compatible/hits.tsv.gz

  1. wget https://datasets.clickhouse.com/hits_compatible/hits.tsv.gz --no-check-certificate
  2. gzip -d hits.tsv.gz
  3. ls -lh hits.tsv
  4. -rw-r--r-- 1 root root 70G Jul 17 14:10 hits.tsv
  5. clickhouse-client --password --time --query "INSERT INTO my_db.my_table1_distributed FORMAT TSV" < hits.tsv
复制代码
  1. ck1 :) SELECT formatReadableQuantity(count()) FROM my_db.my_table1_distributed;
复制代码
   接下来发送一个范例的分析查询,将查询发送到分布式表,分布式表将查询转发给分片,更精确的说是转发给每个分片的一个副本,然后它返回一个组合结果
  1. ck3 :) SELECT
  2.            URL,
  3.            COUNT(*) AS PageViews
  4.            FROM my_db.my_table1_distributed
  5.        WHERE
  6.            (CounterID = 62) AND
  7.            (EventDate >= '2013-07-01') AND
  8.            (EventDate <= '2013-07-31') AND
  9.            (DontCountHits = 0) AND
  10.            (Refresh = 0) AND
  11.            (URL != '')
  12.        GROUP BY URL
  13.        ORDER BY PageViews DESC
  14.        LIMIT 10;
复制代码
7. 同一环境创建多个ClickHouse集群

  在该环境下再设置一个cluster2的集群,它由四个非复制的分片组成,可以用于一些非关键任务数据,这些数据需要高吞吐量
  1.     <remote_servers>
  2.         <cluster1>
  3.             <shard>
  4.                 <replica>
  5.                     <host>ck1</host>
  6.                     <port>9000</port>
  7.                     <user>default</user>
  8.                     <password>123.com</password>
  9.                 </replica>
  10.                 <replica>
  11.                     <host>ck3</host>
  12.                     <port>9000</port>
  13.                     <user>default</user>
  14.                     <password>123.com</password>
  15.                 </replica>
  16.                 <internal_replication>true</internal_replication>
  17.             </shard>
  18.             <shard>
  19.                 <replica>
  20.                     <host>ck2</host>
  21.                     <port>9000</port>
  22.                     <user>default</user>
  23.                     <password>123.com</password>
  24.                 </replica>
  25.                 <replica>
  26.                     <host>ck4</host>
  27.                     <port>9000</port>
  28.                     <user>default</user>
  29.                     <password>123.com</password>
  30.                 </replica>
  31.                 <internal_replication>true</internal_replication>
  32.             </shard>
  33.         </cluster1>
  34.         <cluster2>
  35.             <shard>
  36.                 <replica>
  37.                     <host>ck1</host>
  38.                     <port>9000</port>
  39.                 </replica>
  40.                 <replica>
  41.                     <host>ck4</host>
  42.                     <port>9000</port>
  43.                     <user>default</user>
  44.                     <password>123.com</password>
  45.                 </replica>
  46.                 <internal_replication>true</internal_replication>
  47.             </shard>
  48.         </cluster1>
  49.         <cluster2>
  50.             <shard>
  51.                 <replica>
  52.                     <host>ck1</host>
  53.                     <port>9000</port>
  54.                     <user>default</user>
  55.                     <password>123.com</password>
  56.                 </replica>
  57.             </shard>
  58.             <shard>
  59.                 <replica>
  60.                     <host>ck2</host>
  61.                     <port>9000</port>
  62.                     <user>default</user>
  63.                     <password>123.com</password>
  64.                 </replica>
  65.             </shard>
  66.             <shard>
  67.                 <replica>
  68.                     <host>ck3</host>
  69.                     <port>9000</port>
  70.                     <user>default</user>
  71.                     <password>Newtv123.com</password>
  72.                 </replica>
  73.             </shard>
  74.             <shard>
  75.                 <replica>
  76.                     <host>ck4</host>
  77.                     <port>9000</port>
  78.                     <user>default</user>
  79.                     <password>123.com</password>
  80.                 </replica>
  81.             </shard>
  82.         </cluster2>
  83.     </remote_servers>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

十念

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表