ClickHouse02-小白快速部署和使用

通过上篇已经了解到在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/ ./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=./clickhousenode/data:/var/lib/clickhouse \
--volume=./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:
      - ./clickhousenode/data:/var/lib/clickhouse
      - ./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上给一个免费的关注吗?

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 clickhouse-driver 库来将 DataFrame 数据传输到 ClickHouse 库中。具体步骤如下: 1. 首先,安装 clickhouse-driver 库。在终端输入以下命令: ``` pip install clickhouse-driver ``` 2. 在 Python 中导入 clickhouse-driver 库: ``` import clickhouse_driver ``` 3. 创建 ClickHouse 客户端对象,连接到 ClickHouse 服务器: ``` client = clickhouse_driver.Client('localhost') ``` 这里的 localhost 是 ClickHouse 服务器的地址,如果不在本机上,需要填写相应的 IP 地址。 4. 创建数据表。可以使用普通的 SQL 语句来创建数据表,例如: ``` client.execute('CREATE TABLE test (id Int32, name String) ENGINE = Memory') ``` 5. 将 DataFrame 转换为 ClickHouse 中的数据格式。clickhouse-driver 库提供了一个将 DataFrame 转换为 ClickHouse 格式的函数,例如: ``` data = [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')] columns = ['id', 'name'] df = pd.DataFrame(data, columns=columns) prepared_data = client.prepare_insert('test', df.columns) prepared_data.executemany(df.values) ``` 这里的 df 是一个 Pandas 的 DataFrame,data 是该 DataFrame 中的数据。使用 client.prepare_insert 函数,将 DataFrame 的列名传递给 ClickHouse。然后,使用 prepared_data.executemany 函数,将 DataFrame 中的数据插入到 ClickHouse 表中。 6. 查询数据。可以使用普通的 SQL 语句来查询数据,例如: ``` data = client.execute('SELECT * FROM test') ``` 这里的 data 是一个包含查询结果的列表。 这样,就可以在 Python 中将 DataFrame 数据传输到 ClickHouse 库中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c_zyer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值