ClickHouse调研1-使用文档、介绍

ClickHouse调研1-使用文档、介绍

一、ClickHouse安装方式

  • 源码编译安装
  • Docker安装
  • RPM包安装

为了方便使用,一般采用RPM包方式安装,其他两种方式这里不做说明。

二、下载安装包

官方没有提供rpm包,但是Altinity第三方公司提供了。

地址:https://packagecloud.io/altinity/clickhouse

三、配置文件

安装好rpm包后,默认配置文件在/etc/clickhouse-server/目录下,主要涉及以下3种配置文件,也可以自定义配置文件位置,如果修改了目录记得连带修改启动脚本。

默认启动脚本,注意,这个名字虽然叫server,其实是个shell脚本

vi /etc/init.d/clickhouse-server

12 CLICKHOUSE_USER=clickhouse

14 SHELL=/bin/bash

15 PROGRAM=clickhouse-server

16 GENERIC_PROGRAM=clickhouse

18 SYSCONFDIR=/data/clickhouse

19 CLICKHOUSE_LOGDIR=/data/clickhouse/logs

20 CLICKHOUSE_LOGDIR_USER=root

21 CLICKHOUSE_DATADIR_OLD=/data/clickhouse

22 LOCALSTATEDIR=/data/clickhouse/lock

 

config.xml 全局信息配置文件

<?xml version="1.0"?>

<yandex>

<!--日志-->

<logger>

<level>error</level>

<log>/export/data/clickhouse/logs/clickhouse.log</log>

<errorlog>/export/data/clickhouse/logs/error.log</errorlog>

<size>500M</size>

<count>5</count>

</logger>

<!--全日志-->

<query_log>

<database>system</database>

<table>query_log</table>

<partition_by>toMonday(event_date)</partition_by>

<flush_interval_milliseconds>7500</flush_interval_milliseconds>

</query_log>

<!--本地节点信息-->

<http_port>8123</http_port>

<tcp_port>9000</tcp_port>

<interserver_http_port>9009</interserver_http_port>

<interserver_http_host>localhost</interserver_http_host> <!--本机域名或IP-->

<!--本地配置-->

<listen_host>0.0.0.0</listen_host>

<max_connections>4096</max_connections>

<receive_timeout>800</receive_timeout>

<send_timeout>800</send_timeout>

<keep_alive_timeout>300</keep_alive_timeout>

<max_concurrent_queries>2000</max_concurrent_queries>

<uncompressed_cache_size>8589934592</uncompressed_cache_size>

<mark_cache_size>5368709120</mark_cache_size>

<timezone>Asia/Shanghai</timezone>

<path>/export/data/clickhouse/</path>

<tmp_path>/export/data/clickhouse/tmp/</tmp_path>

<users_config>users.xml</users_config>

<default_profile>default</default_profile>

<default_database>default</default_database>

<!--集群相关配置-->

<remote_servers incl="clickhouse_remote_servers" />

<zookeeper incl="zookeeper-servers" optional="true" />

<macros incl="macros" optional="true" />

<builtin_dictionaries_reload_interval>3600</builtin_dictionaries_reload_interval>

<max_session_timeout>3600</max_session_timeout>

<default_session_timeout>300</default_session_timeout>

<max_insert_block_size>4194304</max_insert_block_size>

<merge_tree>

<max_replicated_merges_in_queue>100</max_replicated_merges_in_queue>

<parts_to_delay_insert>3000</parts_to_delay_insert>

<parts_to_throw_insert>6000</parts_to_throw_insert>

<max_delay_to_insert>2</max_delay_to_insert>

<max_bytes_to_merge_at_max_space_in_pool>107374182400</max_bytes_to_merge_at_max_space_in_pool>

</merge_tree>

<max_table_size_to_drop>0</max_table_size_to_drop>

<max_partition_size_to_drop>0</max_partition_size_to_drop>

<format_schema_path>/export/data/clickhouse/format_schemas/</format_schema_path>

<include_from>/export/data/clickhouse/metrika.xml</include_from>

<resharding>

<task_queue_path>/clickhouse/task_queue</task_queue_path>

</resharding>

<!-- Path in ZooKeeper to queue with DDL queries -->

<distributed_ddl>

<path>/clickhouse/task_queue/ddl</path>

</distributed_ddl>

</yandex>

 

users.xml 用户信息配置文件

<?xml version="1.0"?>

<yandex>

<profiles>

<default>

<distributed_aggregation_memory_efficient>1</distributed_aggregation_memory_efficient> <!--启动distributed引擎内存节省模式-->

<max_memory_usage>21474836480</max_memory_usage> <!--单个查询最大内存使用-->

<max_memory_usage_for_all_queries>21474836480</max_memory_usage_for_all_queries> <!--所有并发查询最大内存使用量-->

<max_bytes_before_external_group_by>10737418240</max_bytes_before_external_group_by> <!--group by最大允许使用内存,超出用磁盘-->

<max_bytes_before_external_sort>10737418240</max_bytes_before_external_sort> <!--order by最大允许使用内存,超出用磁盘-->

<use_uncompressed_cache>0</use_uncompressed_cache> <!--使用未压缩数据块的缓存,适用于短查询-->

<load_balancing>random</load_balancing> <!--random\nearest_hostname\in_order-->

<max_query_size>262144000</max_query_size>

<max_threads>32</max_threads> <!--最大线程数-->

<log_queries>1</log_queries> <!--打开全日志-->

<background_pool_size>32</background_pool_size> <!--表后台工作线程数,加快ClickHouse的内部合并,但会增加磁盘和CPU的消耗-->

<queue_max_wait_ms>5000000</queue_max_wait_ms>

<join_default_strictness>ALL</join_default_strictness> <!--JOIN默认严格性,如果为空,则不严格的查询将抛出异常-->

<enable_optimize_predicate_expression>1</enable_optimize_predicate_expression> <!--默认为1,如果将其设置为1则将谓词优化为子查询。但是19+版本如果打开的话会导致ck服务异常,所以新版本需要改成1-->

<!--insert_distributed_sync>1</insert_distributed_sync-->

<!--max_execution_time>1000</max_execution_time--> <!--最大执行时间-->

<max_ast_elements>10000000</max_ast_elements><!--解决AST is too big-->

<max_expanded_ast_elements>10000000</max_expanded_ast_elements><!--解决AST is too big-->

<connect_timeout_with_failover_ms>1000</connect_timeout_with_failover_ms>

</default>

<readonly>

<readonly>1</readonly>

<distributed_aggregation_memory_efficient>1</distributed_aggregation_memory_efficient>

<max_memory_usage>21474836480</max_memory_usage>

<max_memory_usage_for_all_queries>21474836480</max_memory_usage_for_all_queries>

<max_bytes_before_external_group_by>10737418240</max_bytes_before_external_group_by>

<use_uncompressed_cache>0</use_uncompressed_cache>

<load_balancing>random</load_balancing>

<max_threads>16</max_threads>

<log_queries>1</log_queries>

<queue_max_wait_ms>5000000</queue_max_wait_ms>

<enable_optimize_predicate_expression>1</enable_optimize_predicate_expression>

<!--max_execution_time>1000</max_execution_time-->

</readonly>

</profiles>

<quotas>

<!-- Name of quota. -->

<default>

<interval>

<duration>3600</duration>

<queries>0</queries>

<errors>0</errors>

<result_rows>0</result_rows>

<read_rows>0</read_rows>

<execution_time>0</execution_time>

</interval>

</default>

</quotas>

<users>

<app_user><!--用户名-->

<password_sha256_hex>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</password_sha256_hex>

<networks>

<ip>::/0</ip>

</networks>

<profile>readonly</profile>

<quota>default</quota>

</app_user>

</users>

</yandex>

 

metrika.xml 集群信息配置文件

<yandex>

<!--ck集群节点-->

<clickhouse_remote_servers>

<dbname_ck_cluster> <!--集群名称-->

<!--分片01-->

<shard>

<internal_replication>true</internal_replication> <!--true,是否只将数据写入其中一个副本(默认false,将数据写入所有副本)-->

<replica>

<host>1.1.1.1</host>

<port>9000</port>

<user>default</user>

<password>password</password>

</replica>

<replica>

<host>1.1.1.2</host>

<port>9000</port>

<user>default</user>

<password>Password</password>

</replica>

</shard>

<!--分片02-->

<shard>

<internal_replication>true</internal_replication>

<replica>

<host>1.1.1.3</host>

<port>9000</port>

<user>default</user>

<password>Password</password>

</replica>

<replica>

<host>1.1.1.4</host>

<port>9000</port>

<user>default</user>

<password>Password</password>

</replica>

</shard>

</dbname_ck_cluster>

</clickhouse_remote_servers>

<!--zookeeper相关配置-->

<zookeeper-servers>

<node index="1">

<host>1.1.1.5</host>

<port>2181</port>

</node>

<node index="2">

<host>1.1.1.6</host>

<port>2181</port>

</node>

<node index="3">

<host>1.1.1.7</host>

<port>2181</port>

</node>

</zookeeper-servers>

<macros>

<layer>01</layer>

<shard>01</shard>

<replica>1.1.1.1</replica> <!--当前节点IP-->

</macros>

<networks>

<ip>::/0</ip>

</networks>

<!--压缩相关配置-->

<clickhouse_compression>

<case>

<min_part_size>10000000000</min_part_size>

<min_part_size_ratio>0.01</min_part_size_ratio>

<method>lz4</method> <!--压缩算法lz4压缩比zstd快, 更占磁盘-->

</case>

</clickhouse_compression>

</yandex>

四、ClickHouse常用架构

单实例(不推荐)

  • 就是单机部署,安装好rpm包后,简单修改配置文件即可启动;
  • 单实例不建议线上使用,只做功能测试;
  • MergeTree,引擎适用于单机实例,查询性能非常高。

分布式+高可用集群

  • ClickHouse引擎有十几个,不同引擎实现不同功能,实现分布式高可用主要通过以下两种引擎:
  • ClickHouse分布式通过配置文件来实现,同一集群配置多个分片,每个节点,都配置同样的配置文件;而高可用需要借助zookeeper来实现,ReplicatedMergeTree里共享同一个ZK路径的表,会相互同步数据。
  • ReplicatedMergeTree,复制引擎,基于MergeTree,实现数据复制,即高可用;
  • Distributed,分布式引擎,本身不存储数据,将数据分发汇总。

六、安装zookeeper集群,至少3台

  1. 下载安装包,建议3.4.9之后的版本
  1. http://archive.apache.org/dist/zookeeper/
  1. 解压tar zxf /usr/local/test/clickhouse/zookeeper-3.4.13.tar.gz -C /usr/local
  1. mv /usr/local/zookeeper-3.4.13 /usr/local/zookeeper
  1. 创建目录:mkdir -p /data/zookeeper/data /data/zookeeper/logs
  1. 指定节点号echo '1' > /data/zookeeper/data/myid
  1. zookeeper配置文件/usr/local/zookeeper/conf/zoo.cfg
tickTime=2000
基本时间单位, 毫秒值
initLimit=30000
tickTime的倍数,follower和leader之间的最长心跳时间
syncLimit=10
tickTime的倍数,leader和follower之间发送消息, 请求和应答的最大时间
dataDir=/data
数据目录
dataLogDir=/logs
日志目录,如果没设定,默认和dataDir相同
clientPort=2181
监听client连接的端口号
maxClientCnxns=2000
zookeeper最大连接
maxSessionTimeout=60000000
最大的会话超时时间
autopurge.snapRetainCount=10
保留的文件数目,默认3个
autopurge.purgeInterval=1
自动清理snapshot和事务日志,清理频率,单位是小时
globalOutstandingLimit=200
等待处理的最大请求数量
preAllocSize=131072
日志文件大小Kb,切换快照生成日志
snapCount=3000000
两次事务快照之间可执行事务的次数,默认的配置值为100000
leaderServes=yes
leader是否接受client请求,默认为yes即leader可以接受client的连接,当节点数为>3时,建议关闭
server.1=192.168.1.5:2888:3888
2888 leader\follower传输信息端口,3888推举端口
server.2=192.168.1.6:2888:3888
2888 leader\follower传输信息端口,3888推举端口
server.3=192.168.1.7:2888:3888
2888 leader\follower传输信息端口,3888推举端口
server.3=192.168.1.7:2888:3888:observer
2888 leader\follower传输信息端口,3888推举端口,observer不参与选举
  1. 启动/usr/local/zookeeper/bin/zkServer.sh [start\status\stop]

七、安装clickhouse

  1. 下载并安装上面rpm安装包
  1. 参考配置上面的配置文件
  1. 创建目录
  1. mkdir -p /data/clickhouse/tmp /data/clickhouse/logs /data/clickhouse/lock/
  1. 修改权限chown clickhouse.clickhouse -R /data/clickhouse
  1. 启动(有两种方式)/etc/init.d/clickhouse-serve start或clickhouse-server --daemon --config-file=/etc/clickhouse-server/config.xml
  1. 默认提供2个端口,http:8123和tcp:9000
  1. 登录验证clickhouse-client -u xxx --password xxx -h127.0.0.1 --port 9000

八、目录结构

Metadata:元数据目录存档库表的sql文件

Data:数据目录

Logs:日志目录,错误日志再次目录下

其他目录暂时无需了解

九、数据文件

在items表的每一个part子目录中,存放如下文件下面这些文件:

columns.txt:记录列信息

count.txt:记录总数

checksums.txt:数据校验

.bin:每列都有,存储实际数据

.mrk文件:bin数据文件中,索引粒度偏移量(index_granularity (usually, N = 8192)

minmax_EventDate.idx:时间字段索引

primary.idx:存储主键信息,存储在内存中

这里展示了mrk文件和primary文件的具体结构,可以看到,数据是按照主键排序的,并且会每隔一定大小分隔出很多个block。每个block中也会抽出一个数据作为索引,放到primary.idx和各列的mrk文件中。

而利用mergetree进行查询时,最关键的步骤就是定位block,这里会根据查询的列是否在主键内有不同的方式。根据主键查询时性能会较好,但是非主键查询时,由于按列存储的关系,虽然会做一次全扫描,性能也没有那么差。所以索引在clickhouse里并不像mysql那么关键。实际使用时一般需要添加按日期的查询条件,保障非主键查询时的性能。

找到对应的block之后,就是在block内查找数据,获取需要的行,再拼装需要的其他列数据。

posted on 2021-04-27 07:25  Sunnynanbing  阅读(926)  评论(0编辑  收藏  举报

导航