ClickHouse02-小白快速部署和使用

ClickHouse

Posted by Claire on February 29, 2024

通过上篇已经了解到在OLAP领域,甚至开始侵占传统数据库市场的ClickHouse的一些特点,本篇就是小白快速上手篇,搭建一个试用。

快速上手体验功能

可以下载二进制文件,可用于运行 ClickHouse 服务器、clickhouse-client、clickhouse-local、ClickHouse Keeper 和其他工具

curl https://clickhouse.com/ | sh

./clickhouse server

./clickhouse client

生产使用:Linux平台安装

这边展示通过RPM包安装,此外还有DEB包、Tgz包

配置官方YUM仓库

[ clickhouse]# yum install -y yum-utils
Last metadata expiration check: 0:18:30 ago on Fri 26 Jan 2024 09:31:27 PM CST.
Package yum-utils-4.0.21-14.1.al8.noarch is already installed.
Dependencies resolved.
=========================================================================================================================================================================================
 Package                                                             Architecture                   Version                                Repository                               Size
=========================================================================================================================================================================================
Upgrading:
 dnf-plugins-core                                                    noarch                         4.0.21-19.al8                          alinux3-updates                          75 k
 python3-dnf-plugin-post-transaction-actions                         noarch                         4.0.21-19.al8                          alinux3-updates                          57 k
 python3-dnf-plugins-core                                            noarch                         4.0.21-19.al8                          alinux3-updates                         261 k
 yum-utils                                                           noarch                         4.0.21-19.al8                          alinux3-updates                          75 k
Installing dependencies:
 python3-systemd                                                     x86_64                         234-8.2.al8                            alinux3-os                               81 k

Transaction Summary
=========================================================================================================================================================================================
Install  1 Package
Upgrade  4 Packages

Total download size: 550 k
Downloading Packages:
(1/5): python3-dnf-plugin-post-transaction-actions-4.0.21-19.al8.noarch.rpm                                                                              4.1 MB/s |  57 kB     00:00    
(2/5): python3-systemd-234-8.2.al8.x86_64.rpm                                                                                                            4.7 MB/s |  81 kB     00:00    
(3/5): yum-utils-4.0.21-19.al8.noarch.rpm                                                                                                                8.3 MB/s |  75 kB     00:00    
(4/5): python3-dnf-plugins-core-4.0.21-19.al8.noarch.rpm                                                                                                  11 MB/s | 261 kB     00:00    
(5/5): dnf-plugins-core-4.0.21-19.al8.noarch.rpm                                                                                                         1.3 MB/s |  75 kB     00:00    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                    9.3 MB/s | 550 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                 1/1 
  Installing       : python3-systemd-234-8.2.al8.x86_64                                                                                                                              1/9 
  Upgrading        : python3-dnf-plugins-core-4.0.21-19.al8.noarch                                                                                                                   2/9 
  Upgrading        : dnf-plugins-core-4.0.21-19.al8.noarch                                                                                                                           3/9 
  Upgrading        : yum-utils-4.0.21-19.al8.noarch                                                                                                                                  4/9 
  Upgrading        : python3-dnf-plugin-post-transaction-actions-4.0.21-19.al8.noarch                                                                                                5/9 
  Cleanup          : yum-utils-4.0.21-14.1.al8.noarch                                                                                                                                6/9 
  Cleanup          : dnf-plugins-core-4.0.21-14.1.al8.noarch                                                                                                                         7/9 
  Cleanup          : python3-dnf-plugin-post-transaction-actions-4.0.21-14.1.al8.noarch                                                                                              8/9 
  Cleanup          : python3-dnf-plugins-core-4.0.21-14.1.al8.noarch                                                                                                                 9/9 
  Running scriptlet: python3-dnf-plugins-core-4.0.21-14.1.al8.noarch                                                                                                                 9/9 
  Verifying        : python3-systemd-234-8.2.al8.x86_64                                                                                                                              1/9 
  Verifying        : dnf-plugins-core-4.0.21-19.al8.noarch                                                                                                                           2/9 
  Verifying        : dnf-plugins-core-4.0.21-14.1.al8.noarch                                                                                                                         3/9 
  Verifying        : python3-dnf-plugin-post-transaction-actions-4.0.21-19.al8.noarch                                                                                                4/9 
  Verifying        : python3-dnf-plugin-post-transaction-actions-4.0.21-14.1.al8.noarch                                                                                              5/9 
  Verifying        : python3-dnf-plugins-core-4.0.21-19.al8.noarch                                                                                                                   6/9 
  Verifying        : python3-dnf-plugins-core-4.0.21-14.1.al8.noarch                                                                                                                 7/9 
  Verifying        : yum-utils-4.0.21-19.al8.noarch                                                                                                                                  8/9 
  Verifying        : yum-utils-4.0.21-14.1.al8.noarch                                                                                                                                9/9 

Upgraded:
  dnf-plugins-core-4.0.21-19.al8.noarch  python3-dnf-plugin-post-transaction-actions-4.0.21-19.al8.noarch  python3-dnf-plugins-core-4.0.21-19.al8.noarch  yum-utils-4.0.21-19.al8.noarch 
Installed:
  python3-systemd-234-8.2.al8.x86_64                                                                                                                                                     

Complete!

[ clickhouse]# yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
Adding repo from: https://packages.clickhouse.com/rpm/clickhouse.repo

安装ClickHouse服务端和客户端

[ clickhouse]# yum install -y clickhouse-server clickhouse-client
ClickHouse - Stable Repository                                                                                                                           373  B/s | 833  B     00:02    
ClickHouse - Stable Repository                                                                                                                           5.3 kB/s | 5.7 kB     00:01    
Importing GPG key 0x2B48D754:
 Userid     : "ClickHouse Inc. Repositories Key <packages@clickhouse.com>"
 Fingerprint: 3A9E A119 3A97 B548 BE14 57D4 8919 F6BD 2B48 D754
 From       : https://packages.clickhouse.com/rpm/stable/repodata/repomd.xml.key
ClickHouse - Stable Repository                                                                                                                            52 kB/s | 266 kB     00:05    
Dependencies resolved.
=========================================================================================================================================================================================
 Package                                              Architecture                       Version                                     Repository                                     Size
=========================================================================================================================================================================================
Installing:
 clickhouse-client                                    x86_64                             23.12.2.59-1                                clickhouse-stable                             136 k
 clickhouse-server                                    x86_64                             23.12.2.59-1                                clickhouse-stable                             164 k
Installing dependencies:
 clickhouse-common-static                             x86_64                             23.12.2.59-1                                clickhouse-stable                             276 M

Transaction Summary
=========================================================================================================================================================================================
Install  3 Packages

Total download size: 276 M
Installed size: 862 M
Downloading Packages:
(1/3): clickhouse-server-23.12.2.59.x86_64.rpm                                                                                                            90 kB/s | 164 kB     00:01    
(2/3): clickhouse-client-23.12.2.59.x86_64.rpm                                                                                                            36 kB/s | 136 kB     00:03    
(3/3): clickhouse-common-static-23.12.2.59.x86_64.rpm                     10% [=======-                                                                ] 150 kB/s |  29 MB     28:14 ETA

sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

启动并测试

【推荐】以服务的方式

sudo systemctl enable clickhouse-server
sudo systemctl start clickhouse-server
sudo systemctl status clickhouse-server
clickhouse-client # or "clickhouse-client --password" if you set up a password.

【已过时】手动启动

[ clickhouse]# /etc/init.d/clickhouse-server start
 chown -R clickhouse: '/var/run/clickhouse-server/'
Will run sudo --preserve-env -u 'clickhouse' /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

确认服务

[ clickhouse]# ps -e |grep clickhouse
 243525 ?        00:00:00 clickhouse-serv

生产使用:Docker/K8S环境安装

以下展示单节点,集群形式后续再讨论

单节点主要关注的config.xml内部的配置,做好挂载即可

Docker单节点

启动一个临时容器,用于获取config.xml

docker run --rm -d --name=temp-ch clickhouse/clickhouse-server

docker cp temp-ch:/etc/clickhouse-server/ /Users/chenzy/docker/clickhouse/conf/

修改config.xml,主要打开IPV6支持

 <!-- Same for hosts without support for IPv6: -->
    <listen_host>0.0.0.0</listen_host>

重新创建容器启动

docker run -d \
--name clickhouse \
--ulimit nofile=262144:262144 \
--volume=/Users/chenzy/docker/clickhousenode/data:/var/lib/clickhouse \
--volume=/Users/chenzy/docker/clickhousenode/conf/clickhouse-server/:/etc/clickhouse-server/ \
-p 9000:9000 \
-p 8123:8123 \
-p 9009:9009 \
clickhouse/clickhouse-server

K8S单节点

version: '3.8'
services: 
  cknode:
    image: clickhouse/clickhouse-server
    container_name: cknode
    hostname: cknode
    volumes:
      - /Users/chenzy/docker/clickhousenode/data:/var/lib/clickhouse
      - /Users/chenzy/docker/clickhousenode/conf/clickhouse-server/:/etc/clickhouse-server/
    ports:
      - "9000:9000"
      - "8123:8123"
      - "9009:9009"
      - "9363:9363"

关于端口

  • 8123 是 ClickHouse 提供的 HTTP 端口,用于 HTTP 接口访问,它提供了 RESTful API 以及交互式的查询终端界面。
  • 9000 是 ClickHouse 的默认 TCP 端口,用于 Native TCP 协议进行交互,提供高效的数据交换。
  • 9363 在一些情况下是指 ClickHouse 的分布式表引擎使用的 gRPC 端口,用于 ClickHouse 集群间的内部通信,尤其是在涉及到 ZooKeeper 集群管理和复制时。

确认服务启动

对于Linux平台、Docker或者K8S平台,可以使用 clickhouse-client 进行了连接访问测试

此外还可以书写简单的JAVA访问程序进行测试

                    String url = "jdbc:ch://127.0.0.1:8123/default"; // use http protocol and port 8123 by default
                    Properties properties = new Properties();
                    List<HitsAll> list = new ArrayList<>();
                    try {
                        ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties);
                        try (Connection conn = dataSource.getConnection("default", "");
                             Statement stmt = conn.createStatement()) {
                            ResultSet rs = stmt.executeQuery("select * from default.hits_all ");
                            while (rs.next()) {
                                int id = rs.getInt("id");
                                HitsAll hitsAll = new HitsAll();
                                hitsAll.setId(id);
                                list.add(hitsAll);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

如果喜欢我的文章的话,可以去GitHub上给一个免费的关注吗?