clickhouse安装及简单使用

本文介绍了ClickHouse数据库的特性、优点,包括其高性能、易用性和快速版本迭代。详细步骤包括下载、安装、配置服务器和客户端,以及展示如何创建表、插入数据和执行查询。还提到了官方案文资源的重要性。
摘要由CSDN通过智能技术生成

前言

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 代码中clickhouse驱动连接所用端口,例如 python中连接clickhouse 就得使用这个端口,默认是9000,由于端口冲突才改成8900的

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'

经验总结

  1. clickhouse的官网文档有中文版,写的还是比较全的,可以多去参考查阅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值