赞
踩
ClickHouse是一个面向联机分析处理(OLAP)的开源的面向列式存储的DBMS,简称CK, 与Hadoop, Spark相比,ClickHouse很轻量级,由俄罗斯第一大搜索引擎Yandex于2016年6月发布, 开发语言为C++;
1,为了高效的使用CPU,数据不仅仅按列存储,同时还按向量进行处理;
2,数据压缩空间大,减少IO;处理单查询高吞吐量每台服务器每秒最多数十亿行;
3,索引非B树结构,不需要满足最左原则;只要过滤条件在索引列中包含即可;即使在使用的数据不在索引中,由于各种并行处理机制ClickHouse全表扫描的速度也很快;
4,写入速度非常快,50-200M/s,对于大量的数据更新非常适用;
5,压缩性好:相对mysql压缩10倍
1,不支持事务,不支持真正的删除/更新;
2,不支持高并发,官方建议qps为100,可以通过修改配置文件增加连接数,但是在服务器足够好的情况下;
3,SQL满足日常使用80%以上的语法,join写法比较特殊;最新版已支持类似SQL的join,但性能不好,不建议使用;
4,尽量做1000条以上批量的写入,避免逐行insert或小批量的insert操作,因为ClickHouse底层会不断的做异步的数据合并,会影响查询性能,这个在做实时数据写入的时候要尽量避开;
5,Clickhouse快是因为采用了并行处理机制,即使一个查询,也会用服务器一半的CPU去执行,所以ClickHouse不能支持高并发的使用场景,默认单查询使用CPU核数为服务器核数的一半,安装时会自动识别服务器核数,可以通过配置文件修改该参数。
全量数据导入:数据导入临时表 -> 导入完成后,将原表改名为tmp1 -> 将临时表改名为正式表 -> 删除原表
增量数据导入: 增量数据导入临时表 -> 将原数据除增量外的也导入临时表 -> 导入完成后,将原表改名为tmp1-> 将临时表改成正式表-> 删除原数据表
6,只支持自己的协议(没有MySQL协议支持)
集群内每个物理节点使用的操作系统为Centos,在最小部署下,同时需要在一个物理节点上部署Zookeeper、Kafka、ClickHouse三个软件。
#vi /etc/hosts
增加hosts记录
192.168.0.99 cluster01-02-1
192.168.0.99 cluster01-01-2
192.168.0.98 cluster01-01-1
192.168.0.98 cluster01-02-2
1、查询防火墙状态:
service iptables status
2、停止防火墙:
service iptables stop
3、启动防火墙:
service iptables start
4、重启防火墙:
service iptables restart
5、永久关闭防火墙:
chkconfig iptables off
6、永久关闭后启用:
chkconfig iptables on
目前为进行防火墙的端口开关设置,测试环境上关闭iptables。后续会根据需求增加打开iptables状态下的端口设置。
2.1集群配置概况
1. 高可用原理:zookeeper + ReplicatedMergeTree(复制表) + Distributed(分布式表)
2. 前提准备:所有节点防火墙关闭或者开放端口;hosts表和主机名一定要集群保持一致正确配置,因为zookeeper返回的是主机名,配置错误或不配置复制表时会失败.
clickhouse测试节点2个:192.168.0.98 clickhouse1, 192.168.0.99 clickhouse2
zookeeper测试节点2个:server.1: 192.168.0.98 server.2 : 192.168.0.99
配置方案:两个节点各配置两个clickhouse实例,相互备份.
clickhouse1: 实例1, 端口: tcp 9000, http 8123, 同步端口9009, 类型: 分片1, 副本1
clickhouse1: 实例2, 端口: tcp 9001, http 8124, 同步端口9010, 类型: 分片2, 副本2 (clickhouse2的副本)
clickhouse2: 实例1, 端口: tcp 9000, http 8123, 同步端口9009, 类型: 分片2, 副本1
clickhouse2: 实例2, 端口: tcp 9001, http 8124, 同步端口9010, 类型: 分片1, 副本2 (clickhouse1的副本)
2.2ClickHouse集群关系图
<?xml version="1.0"?> <yandex> <!-- 日志 --> <logger> <level>notice</level> <log>/var/log/ClickHouse-server/ClickHouse-server.log</log> <errorlog>/var/log/ClickHouse-server/ClickHouse-server.err.log</errorlog> <size>1000M</size> <count>10</count> </logger> <!-- 每个数据库实例的端口 --> <http_port>8123</http_port> <tcp_port>9000</tcp_port> <openSSL> <server> <certificateFile>/etc/ClickHouse-server/server.crt</certificateFile> <privateKeyFile>/etc/ClickHouse-server/server.key</privateKeyFile> <dhParamsFile>/etc/ClickHouse-server/dhparam.pem</dhParamsFile> <verificationMode>none</verificationMode> <loadDefaultCAFile>true</loadDefaultCAFile> <cacheSessions>true</cacheSessions> <disableProtocols>sslv2,sslv3</disableProtocols> <preferServerCiphers>true</preferServerCiphers> </server> <client> <loadDefaultCAFile>true</loadDefaultCAFile> <cacheSessions>true</cacheSessions> <disableProtocols>sslv2,sslv3</disableProtocols> <preferServerCiphers>true</preferServerCiphers> <invalidCertificateHandler> <name>RejectCertificateHandler</name> </invalidCertificateHandler> </client> </openSSL> <!-- 集群数据同步端口, 各个实例端口号不能相同 --> <interserver_http_port>9009</interserver_http_port> <!-- 本机域名 --> <interserver_http_host>cluster01-02-1</interserver_http_host> <!-- 监听IP --> <listen_host>::</listen_host> <!-- 最大连接数 --> <max_connections>4096</max_connections> <keep_alive_timeout>3</keep_alive_timeout> <!-- 最大并发查询数 --> <max_concurrent_queries>100</max_concurrent_queries> <!-- 单位是B --> <uncompressed_cache_size>8589934592</uncompressed_cache_size> <mark_cache_size>5368709120</mark_cache_size> <!-- 存储路径 --> <path>/var/lib/ClickHouse/</path> <tmp_path>/var/lib/ClickHouse/tmp/</tmp_path> <!-- user配置 --> <user_files_path>/var/lib/ClickHouse/user_files/</user_files_path> <users_config>users.xml</users_config> <default_profile>default</default_profile> <default_database>default</default_database> <!-- 集群分片配置 --> <remote_servers> <distable> <shard> <weight>1</weight> <internal_replication>false</internal_replication> <replica> <host>cluster01-01-1</host> <port>9000</port> </replica> <replica> <host>cluster01-01-2</host> <port>9001</port> </replica> </shard> <shard> <weight>2</weight> <internal_replication>false</internal_replication> <replica> <host>cluster01-02-1</host> <port>9000</port> </replica> <replica> <host>cluster01-02-2</host> <port>9001</port> </replica> </shard> </distable> </remote_servers> <!-- 配置zookeeper --> <zookeeper> <node index="1"> <host>192.168.0.98</host> <port>2181</port> </node> <node index="2"> <host>192.168.0.99</host> <port>2181</port> </node> </zookeeper> <!-- 复制标识的配置,也称为宏配置,这里唯一标识一个副本名称,每个实例都要配置并且都是唯一的 --> <macros> <layer>01</layer> <shard>02</shard> <replica>cluster01-02-1</replica> </macros> <!-- 重新加载内置词典之前的时间间隔(以秒为单位) --> <builtin_dictionaries_reload_interval>3600</builtin_dictionaries_reload_interval> <!-- 最大会话超时配置 --> <max_session_timeout>3600</max_session_timeout> <default_session_timeout>60</default_session_timeout> <query_log> <database>system</database> <table>query_log</table> <partition_by>toYYYYMM(event_date)</partition_by> <!-- Interval of flushing data. --> <flush_interval_milliseconds>7500</flush_interval_milliseconds> </query_log> <!-- 外部词典配置路径 --> <dictionaries_config>*_dictionary.xml</dictionaries_config> <compression incl="ClickHouse_compression"> </compression> <distributed_ddl> <path>/ClickHouse/task_queue/ddl</path> </distributed_ddl> <!-- 讲解路径: https://clickhouse.yandex/docs/zh/operations/table_engines/graphitemergetree/ --> <graphite_rollup_example> <pattern> <regexp>click_cost</regexp> <function>any</function> <retention> <age>0</age> <precision>3600</precision> </retention> <retention> <age>86400</age> <precision>60</precision> </retention> </pattern> <default> <function>max</function> <retention> <age>0</age> <precision>60</precision> </retention> <retention> <age>3600</age> <precision>300</precision> </retention> <retention> <age>86400</age> <precision>3600</precision> </retention> </default> </graphite_rollup_example> <format_schema_path>/var/lib/ClickHouse/format_schemas/</format_schema_path> </yandex>
集群配置默认为 :<remote_servers incl="clickhouse_remote_servers" /> zookeeper默认为 :<zookeeper incl="zookeeper-servers" optional="true" /> macros默认为 :<macros incl="macros" optional="true" /> 首先是集群分片的配置,这个配置所有节点的所有实例完全保持一致: <remote_servers> <distable> <shard> <!-- Optional. Shard weight when writing data. Default: 1. --> <weight>1</weight> <!-- Optional. Whether to write data to just one of the replicas. Default: false (write data to all replicas). --> <internal_replication>false</internal_replication> <replica> <host>cluster01-01-1</host> <port>9000</port> </replica> <replica> <host>cluster01-01-2</host> <port>9001</port> </replica> </shard> <shard> <weight>1</weight> <internal_replication>false</internal_replication> <replica> <host>cluster01-02-1</host> <port>9000</port> </replica> <replica> <host>cluster01-02-2</host> <port>9001</port> </replica> </shard> </distable> </remote_servers> 配置里面的<distable>是分布式标识标签,可以自定义,到最后创建分布式表的时候会用到;然后weight是分片权重,即写数据时有多大的概率落到此分片,因为这里所有分片权重相同所有都设置为1,然后是internal_replication,表示是否只将数据写入其中一个副本,默认为false,表示写入所有副本,在复制表的情况下可能会导致重复和不一致,所以这里一定要改为true,clickhouse分布式表只管写入一个副本,其余同步表的事情交给复制表和zookeeper来进行,然后是replica配置这个好理解,就是一个分片下的所有副本,这里副本的分布一定要手动设计好,保证相互备份,然后再次说明是所有的节点配置一致. 此部分配置严格按照官网配置,参考链接:https://clickhouse.yandex/docs/en/operations/table_engines/distributed/
然后是zookeeper配置,这个也是所有示例配置都一样:
<zookeeper>
<node index="1">
<host>192.168.0.98</host>
<port>2181</port>
</node>
<node index="2">
<host>192.168.0.99</host>
<port>2181</port>
</node>
</zookeeper>
然后是复制标识的配置,也称为宏配置,这里唯一标识一个副本名称,每个实例都要配置并且都是唯一的,这里配置如下:
clickhouse1 9000 分片1, 副本1: <macros> <layer>01</layer> <shard>01</shard> <replica>cluster01-01-1</replica> </macros> clickhouse1 9001 分片2, 副本2: <macros> <layer>01</layer> <shard>02</shard> <replica>cluster01-02-2</replica> </macros> clickhouse2 9000 分片2, 副本1: <macros> <layer>01</layer> <shard>02</shard> <replica>cluster01-02-1</replica> </macros> clickhouse2 9001 分片1, 副本2: <macros> <layer>01</layer> <shard>01</shard> <replica>cluster01-01-2</replica> </macros>
cd /home/ClickHouse/bin
./ClickHouse-server --daemon --pid-file=/var/run/ClickHouse-server/ClickHouse-server.pid --config-file=//home/ClickHouse/etc/ClickHouse-server/config.xml
./ClickHouse-server --daemon --pid-file=/var/run/ClickHouse-server/ClickHouse-server1.pid --config-file=//home/ClickHouse/etc/ClickHouse-server/config.xml
cd /home/OptimnalDB/bin
./ClickHouse-client
./ClickHouse-client --port 9001
查看全部数据库: show databases;
进入指定数据库: use mxs;
查看表 : show tables;
drop table flow;
http://note.youdao.com/noteshare?id=f36dfc357f2c80dc276e3f06ebe534ff&sub=E5475E5FE3A041AC95298CBB12F3F45F
详情参考clickhouse官网 : https://clickhouse.yandex/docs/en/interfaces/cli/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。