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內查找數據,獲取需要的行,再拼裝需要的其他列數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM