clickhouse安装及简单使用
前言
ClickHouse架构简洁且功能强大,在数据处理能力和查询效率方面表现优异。该系统采用先进的压缩存储方案,并支持高效的单机部署模式与传统大数据集群平起平伏
由于使用体验简单直接,使得运维工作同样变的简单。
版本更新频率较高,并主动支持并兼容jdbc、MySQL以及PostgreSQL等成熟数据库系统;其周边生态系统也较为完善;特别适合用于构建企业级的数据仓库环境
下载&安装
下载
官网文档 https://clickhouse.com/docs/zh/getting-started/install
下载地址 https://packages.clickhouse.com/tgz/stable/
选择近半年内的版本进行下载,没必要选择最新版本,这里选择23.1.1.3077
必须安装ClickHouse Server及其相关的组件包ClickHouse Common Static和ClickHouse Client三种
可以获取安装包地址,在Linux下使用wget 下载
安装
tar -xzvf clickhouse-common-static-23.1.1.3077-amd64.tgz
//进入目录,执行脚本,安装组件
./install/doinst.sh
//安装服务端
tar -xzvf clickhouse-server-23.1.1.3077-amd64.tgz
./install/doinst.sh
//安装客户端
tar -xzvf clickhouse-client-23.1.1.3077-amd64.tgz
./install/doinst.sh
//启动服务端
/etc/init.d/clickhouse-server start

启动后 日志文件将输出在/var/log/clickhouse-server/文件夹
配置文件是/etc/clickhouse-server/config.xml
如果服务无法启动,请检查错误日志记录以获取详细信息。通常建议避免使用已被占用的端口以确保服务正常运行。
配置文件
配置文件只截取端口配置部分进行说明
<!-- Port for HTTP API. See also 'https_port' for secure connections.
This interface is also used by ODBC and JDBC drivers (DataGrip, Dbeaver, ...)
and by most of web interfaces (embedded UI, Grafana, Redash, ...).
-->
<http_port>8123</http_port>
<!-- Port for interaction by native protocol with:
- clickhouse-client and other native ClickHouse tools (clickhouse-benchmark, clickhouse-copier);
- clickhouse-server with other clickhouse-servers for distributed query processing;
- ClickHouse drivers and applications supporting native protocol
(this protocol is also informally called as "the TCP protocol");
See also 'tcp_port_secure' for secure connections.
-->
<tcp_port>8900</tcp_port>
<!-- Compatibility with MySQL protocol.
ClickHouse will pretend to be MySQL for applications connecting to this port.
-->
<mysql_port>9004</mysql_port>

配置文件列出了三个端口:
http_port 8123 客户端工具连接接口,比如 Dbeaver
TCP port 8900 is used in the code for establishing connections to the ClickHouse database. For instance, in a Python environment, connecting to a ClickHouse database requires using this port. By default, the connection attempts to use port 9000, but due to potential conflicts with existing configurations, it was changed to 89Oo.
mysql_port 9004允许用户将clickhouse视为MySQL数据库进行操作
特别需要注意的是,在这些端口中都存在可进行设置的地方,并且在实际操作过程中必须确保与系统原有配置保持一致;否则会导致无法建立连接的问题
客户端连接
客户端连接方式分为两种不同的情况:第一种主要采用内置的命令行界面工具进行操作;第二种则是采用了第三方提供的客户端工具作为连接手段
自带客户端
//进入clickhouse-client解压目录,执行脚本
cd ./usr/bin
//调用客户端脚本连接数据库
clickhouse-client --port 8900 -u default --password xxxxx@xxxxxx --host localhost
第三方客户端
建议采用DBeaver作为工具。其连接模式与MySQL相似。选择ClickHouse数据库类型时,请确保安装相应的驱动程序。方能完成后续的连接操作。
使用方式和Navicat类似,不再赘述。
常用SQL
建表
CREATE TABLE lotto
(
`id` String COMMENT '主键ID',
`number` String COMMENT '期号',
`award_date` Date32 COMMENT '开奖日期',
`award_result` String COMMENT '开奖结果',
`f1` String COMMENT '前区01',
`f2` String COMMENT '前区02',
`f3` String COMMENT '前区03',
`f4` String COMMENT '前区04',
`f5` String COMMENT '前区05',
`b1` String COMMENT '后区01',
`b2` String COMMENT '后区02',
`create_date` Date32 COMMENT '爬取时间'
)
ENGINE = MergeTree
ORDER BY number ;

插入
INSERT INTO `default`.lotto
(id, `number`, award_date, award_result, f1, f2, f3, f4, f5, b1, b2, create_date)
VALUES(generateUUIDv4(), '123', '2024-01-17', '01 23 25', '01', '', '', '', '', '', '', '2024-01-17');
查询
SELECT * from lotto WHERE id = '7bd04621-3496-45ed-b778-36ca152744f8' 类似MySQL
删除
//由于clickhouse不推荐删除数据,所以语法搞的跟其他SQL数据库不一样
ALTER table lotto DELETE where id = '7bd04621-3496-45ed-b778-36ca152744f8'
经验总结
- clickhouse的官网文档有中文版,写的还是比较全的,可以多去参考查阅
