ClickHouse初始+安装

ClickHouse是俄罗斯的Yandex于2016年开源的列式存储数据库(DBMS),使用C++语言编写,主要用于在线分析处理查询(OLAP),能够使用SQL查询实时生成分析数据报告。

一、特点

1.列式存储

对于列的聚合,计数,求和等统计操作优于行式存储
由于某一列的数据类型都是相同的,针对数据存储更容易进行数据压缩,每一列选择更优的数据压缩算法,大大提高了数据的压缩比重。
由于数据压缩比较好,一方面节省了磁盘空间,另一方面对cache也有很大的发挥空间

2.DBMS的功能

几乎覆盖了标准SQL的大部分语法,包括DDL和DML,以及配套的各种函数,用户管理及权限管理,数据的备份与恢复

3.多样化引擎

clickhouse和MySQL类似,把表级的存储引擎插件化,根据表的不同需求可以设定不同的存储引擎。目前包括合并树、日志、接口和其它四大类20多种引擎

4.高吞吐写入能力

clickhouse采用类LSM Tree的结构,数据写入后定期在后台compaction。通过类LSM Tree的结构,clickhouse在数据导入时全部是顺序append写,写入后数据段不可更改,在后台compaction时也是多个段merge sort后顺序写会磁盘。顺序写的特性,充分利用了磁盘的吞吐能力,即便在HDD上也有着优异的写入性能。
官方公开benchmark测试显示能够达到50MB-200MB/s的写入吞吐能力,按照每行100byte估算,大约相当于50w-200w条/s的写入速度

5.数据分区与线程级并行

clickhouse将数据划分为多个partition,每个partition再进一步划分为多个index granularity(索引粒度),然后通过多个CPU核心分别处理其中的一部分来实现并行数据处理。在这种设计下,单条query就能利用整机所有CPU。极致的并行处理能力,极大的降低了查询延时。
所以,clickhouse即使对于大量数据的查询也能够化整为零平行处理。但是有一个弊端就是对于单条查询使用多CPU,就不利于同时并发多条查询。所以对高QPS的查询业务,clickhouse并不是强项。
适合存储大宽表,基于大宽表的聚合操作

6.对比

单表查询
关联查询(尽量避免join)
关联时会将右表加载到内存,和左表比对
结论:clickhouse像很多OLAP数据库一样,单表查询速度优于关联查询,而且clickhouse的两者差距更为明显。

二、安装部署

1.环境准备

Centos操作:
查看系统限制
ulimit -a
MacBook-Pro:bin FengZhen$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 2560
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8176
cpu time               (seconds, -t) unlimited
max user processes              (-u) 5333
virtual memory          (kbytes, -v) unlimited
修改资源限制
vim /etc/security/limits.conf
fengzhen@group soft noffile 65536
fengzhen@group hard noffile 65536
fengzhen@group soft nproc 131072
fengzhen@group hard nproc 131072 
第一列标记用户,如果为所有用户标记,直接*
第二列soft、hard,强软限制,一般软<=硬,标记-则表示两者一起配置,值相同
第三列 noffile:可打开文件数    nproc:进程数
第四列具体数值
vim /etc/security/limits.d/20-nproc.conf
将上边四行也放入这个里边
上述配置重新登录即可
安装依赖
yum install -y libtool
yum install -y *unixODBC*
centos取消selinux
vim /etc/selinux/config
SELINUX=disabled
默认是enforcing开启状态
查看:getenforce
临时生效:setenforce 0(关闭) 1(开启)
*MacOS操作:
vim /Library/LaunchDaemons/limit.maxfiles.plist
 
#添加以下内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>
 
# 然后执行以下命令
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
 
# 然后重启,通过 ulimit -n看是否生效
ulimit -n

2.安装

A.Mac【启动失败】
chmod a+x ./clickhouse
./clickhouse
A.Mac + docker【启动失败】
拉取服务端
docker pull yandex/clickhouse-server
拉取客户端
docker pull yandex/clickhouse-client
启动容器
docker run -d --name ck-server --ulimit nofile=262144:262144 -p 8123:8123 -p 9000:9000 -p 9009:9009 --volume=/Users/FengZhen/Desktop/Hadoop/clickhouse/some_clickhouse_database:/var/lib/clickhouse -v /Users/FengZhen/Desktop/Hadoop/clickhouse/etc/:/etc/clickhouse-server/ yandex/clickhouse-server
修改/etc/config.xml
注释去掉<listen_host>::</listen_host>,表示不做IP限制
重启
报错
MacBook-Pro:clickhouse FengZhen$ docker logs -f -t --since="2018-03-31" --tail=100 ck-server
2022-03-31T08:38:29.202014966Z Configuration file '/etc/clickhouse-server/config.xml' isn't readable by user with id '101'
查看服务 $ docker ps
进入到这个容器中 $ docker exec -it ck-server /bin/bash
然后执行 $ clickhouse-client
进入容器成功,再执行 :) show databases;  即可查看数据库
测试
CREATE TABLE default.user_table(id UInt16, name String, age UInt16 ) ENGINE = TinyLog();
B.centos-自动
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
sudo yum install -y clickhouse-server clickhouse-client
sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.
C.centos-手动【推荐】
下载安装包
https://packages.clickhouse.com/rpm/stable/
 
clickhouse-client-22.3.2.2-2.noarch.rpm
clickhouse-common-static-22.3.2.2-2.x86_64.rpm
clickhouse-common-static-dbg-22.3.2.2-2.x86_64.rpm
clickhouse-server-22.3.2.2-2.noarch.rpm
20.5版本:final支持多线程
20.6.3版本:支持explain
20.8:新引擎:实时同步MySQL
clickhouse目录下:
rpm -ivh *.rpm
会提示输入密码,用户:default
1234qwer
确认是否安装
rpm -qa|grep clickhouse
[root@superset-BI rpm]# rpm -qa|grep clickhouse
clickhouse-client-22.3.2.2-2.noarch
clickhouse-common-static-22.3.2.2-2.x86_64
clickhouse-server-22.3.2.2-2.noarch
clickhouse-common-static-dbg-22.3.2.2-2.x86_64
默认路径
conf -> /etc/clickhouse-server /etc/clickhouse-client 
bin -> /usr/bin/
lib -> /val/lib/clickhouse
log -> /var/log/clickhouse
查看/etc/clickhouse-server目录下文件
cd /etc/clickhouse-server
[root@superset-BI clickhouse-server]# ll
总用量 80
dr-x------ 2 clickhouse clickhouse  4096 3月  31 18:19 config.d
-r-------- 1 clickhouse clickhouse 61569 3月  18 01:01 config.xml
dr-x------ 2 clickhouse clickhouse  4096 3月  31 18:19 users.d
-r-------- 1 clickhouse clickhouse  6248 3月  18 01:01 users.xml
 
config.xml    服务端配置
users.xml    参数配置(CPU、最大内存使用量)
上述两个文件为只读,添加可编辑权限
chmod +w config.xml 
chmod +w users.xml 
取消IP限制
vim config.xml
不对IP做限制
<listen_host>::</listen_host>
数据目录,可以自己修改,不建议改
<path>/val/lib/clickhouse</path>
日志
<level>trace</level>
<log>/var/log/clickhouse-server/clickhouse-server.log</log>
<errorlog>/var/log/clickhouse-server/clickhouse-server.err.log</lerrorlogog>
常用命令
sudo clickhouse start
sudo clickhouse status
sudo clickhouse stop
sudo clickhouse restart
启动
[root@superset-BI clickhouse-server]# sudo clickhouse start
 chown -R clickhouse: '/var/run/clickhouse-server/'
Will run su -s /bin/sh 'clickhouse' -c '/usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon'
Waiting for server to start
Waiting for server to start
Server started
 
[root@superset-BI clickhouse-server]# sudo clickhouse status
/var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 19428.
The process with pid = 19428 is running.
启动成功后使用client连接(端口默认9000)
clickhouse-client
    -m:支持换行
    -h:主机名
    -p:端口
    --password:如果配置了密码
    --query:可直接写SQL
clickhouse-client -m --password
[root@superset-BI ~]# clickhouse-client -m --password
ClickHouse client version 22.3.2.1.
Password for user (default):
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.3.2 revision 54455.
superset-BI :)

建表测试

superset-BI :) CREATE TABLE default.user_table(id UInt16, name String, age UInt16 ) ENGINE = TinyLog();
CREATE TABLE default.user_table
(
    `id` UInt16,
    `name` String,
    `age` UInt16
)
ENGINE = TinyLog
Query id: 42ed57fd-76a1-4996-be45-d47804b1c013
Ok.
0 rows in set. Elapsed: 0.011 sec. 
superset-BI :) show tables in default;
SHOW TABLES FROM default
Query id: 74e494da-566e-46ae-a32d-4cfca95e363d
┌─name───────┐
│ user_table │
└────────────┘
1 rows in set. Elapsed: 0.002 sec. 

成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值