debian下的clickhouse初体验
背景
由于MySQL在大数据量时太慢了,本文记录一下clickhouse的使用过程。
初体验
安装
安装官方最新版本
sudo apt-get install apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4
echo "deb https://repo.clickhouse.tech/deb/stable/ main/" | sudo tee
/etc/apt/sources.list.d/clickhouse.list
sudo apt-get update
sudo apt-get install -y clickhouse-server clickhouse-client
sudo service clickhouse-server start
clickhouse-client
由于下载慢,本文未安装最新版本 21.2.3.15
。
安装debian源版本
apt-get install clickhouse-server
确认安装成功
$ clickhouse-client
ClickHouse client version 18.16.1.
Connecting to localhost:9000.
Connected to ClickHouse server version 18.16.1 revision 54412.
box :) select 1+1;
SELECT 1 + 1
┌─plus(1, 1)─┐
│ 2 │
└────────────┘
1 rows in set. Elapsed: 0.003 sec.
配置
开放外网访问
<listen_host>0.0.0.0</listen_host>
存储路径
默认路径为 /var/lib/clickhouse/
<path>/var/lib/clickhouse/</path>
<tmp_path>/var/lib/clickhouse/tmp/</tmp_path>
<user_files_path>/var/lib/clickhouse/user_files/</user_files_path>
<format_schema_path>/var/lib/clickhouse/format_schemas/</format_schema_path>
用户名密码
修改文件 /etc/clickhouse-server/users.xml
中的 password 中的内容。
然后连接:
clickhouse-client -h ip地址 -d default -m -u default --password 密码明文
数据压缩
<compression incl="clickhouse_compression">
<case>
<method>zstd</method>
</case>
</compression>
建表
建立每日价格表
CREATE TABLE data.us_stock
(
name String,
date Date,
open Float32,
high Float32,
low Float32,
close Float32,
volume Int64
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY (name, date)
ORDER BY (name, date)
Ok.
0 rows in set. Elapsed: 0.031 sec.
建立股票名称表
CREATE TABLE data.us_stock_name
(
name String,
cname String,
symbol String,
create_time DateTime,
last_update_time DateTime
)
ENGINE = ReplacingMergeTree(last_update_time)
PRIMARY KEY symbol
ORDER BY symbol
Ok.
0 rows in set. Elapsed: 0.011 sec.
使用python插入数据
安装库
python3 -m pip install clickhouse-driver
执行插入语句