乐趣区

关于数据库:PostgreSQL使用clickhousedbfdw访问ClickHouse

作者:杨杰

简介

PostgreSQL FDW 是一种内部拜访接口,它能够被用来拜访存储在内部的数据,这些数据能够是内部的 PG 数据库,也能够 mysql、ClickHouse 等数据库。

ClickHouse 是一款疾速的开源 OLAP 数据库管理系统,它是面向列的,容许应用 SQL 查问实时生成剖析报告。

clickhouse_fdw 是一个开源的内部数据包装器 (FDW) 用于拜访 ClickHouse 列存数据库。

目前有以下两款 clickhouse_fdw:
https://github.com/adjust/cli…

始终继续一直的有提交,目前反对 PostgreSQL 11-13
https://github.com/Percona-La…

之前有一年工夫没有动静,最近一段时间刚从 adjust/clickhouse_fdw merge 了一下,目前也反对 PostgreSQL 11-13。

本文就以 adjust/clickhouse_fdw 为例。

装置

# libcurl >= 7.43.0
yum install libcurl-devel libuuid-devel

git clone https://github.com/adjust/clickhouse_fdw.git
cd clickhouse_fdw
mkdir build && cd build
cmake ..
make && make install

应用

CH 端:
生成测试表及数据,这里咱们应用 CH 官网提供的 Star Schema Benchmark

https://clickhouse.tech/docs/…

模仿数据量:5 张数据表,数据次要集中在 lineorder* 表,单表 9000w rows 左右、22G 存储。

[root@vm101 ansible]# clickhouse client
ClickHouse client version 20.8.9.6.
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.8.9 revision 54438.

vm101 :) show tables;

SHOW TABLES

┌─name───────────┐
│ customer │
│ lineorder │
│ lineorder_flat │
│ part │
│ supplier │
└────────────────┘

5 rows in set. Elapsed: 0.004 sec. 

vm101 :) select count(*) from lineorder_flat;

SELECT count(*)
FROM lineorder_flat

┌──count()─┐
│ 89987373 │
└──────────┘

1 rows in set. Elapsed: 0.005 sec. 

[root@vm101 ansible]# du -sh /clickhouse/data/default/lineorder_flat/
22G /clickhouse/data/default/lineorder_flat/

PG 端:
创立 FDW 插件

postgres=# create extension clickhouse_fdw ;
CREATE EXTENSION
postgres=# \dew
List of foreign-data wrappers
Name | Owner | Handler | Validator 
----------------+----------+--------------------------+----------------------------
clickhouse_fdw | postgres | clickhousedb_fdw_handler | clickhousedb_fdw_validator
(1 row)

创立 CH 内部服务器

postgres=# CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhouse_fdw 
OPTIONS(host '10.0.0.101', port '9000', dbname 'default', driver 'binary');
CREATE SERVER
postgres=# \des
List of foreign servers
Name | Owner | Foreign-data wrapper 
----------------+----------+----------------------
clickhouse_svr | postgres | clickhouse_fdw
(1 row)

创立用户映射

postgres=# CREATE USER MAPPING FOR CURRENT_USER SERVER clickhouse_svr 
OPTIONS (user 'default', password '');
CREATE USER MAPPING
postgres=# \deu
List of user mappings
Server | User name 
----------------+-----------
clickhouse_svr | postgres
(1 row)

创立内部表

postgres=# IMPORT FOREIGN SCHEMA "default" FROM SERVER clickhouse_svr INTO public;
IMPORT FOREIGN SCHEMA
postgres=# \det
List of foreign tables
Schema | Table | Server 
--------+----------------+----------------
public | customer | clickhouse_svr
public | lineorder | clickhouse_svr
public | lineorder_flat | clickhouse_svr
public | part | clickhouse_svr
public | supplier | clickhouse_svr
(5 rows)

查问

postgres=# select count(*) from lineorder_flat ;
count 
----------
89987373
(1 row)

postgres=# select "LO_ORDERKEY","C_NAME" from lineorder_flat limit 5;
LO_ORDERKEY | C_NAME 
-------------+--------------------
3271 | Customer#000099173
3271 | Customer#000099173
3271 | Customer#000099173
3271 | Customer#000099173
5607 | Customer#000273061
(5 rows)

须要留神的是 CH 是辨别大小写的以及一些函数兼容问题,下面的示例也有展现。

测试 SQL 间接应用 CH SSB 提供的 13 条 SQL,SQL 根本相似,选一条做下测试,运行工夫根本是统一的。

CH:

vm101 :) SELECT
:-] toYear(LO_ORDERDATE) AS year,
:-] C_NATION,
:-] sum(LO_REVENUE - LO_SUPPLYCOST) AS profit
:-] FROM lineorder_flat
:-] WHERE C_REGION = 'AMERICA' AND S_REGION = 'AMERICA' AND (P_MFGR = 'MFGR#1' OR P_MFGR = 'MFGR#2')
:-] GROUP BY
:-] year,
:-] C_NATION
:-] ORDER BY
:-] year ASC,
:-] C_NATION ASC;

SELECT 
toYear(LO_ORDERDATE) AS year,
C_NATION,
sum(LO_REVENUE - LO_SUPPLYCOST) AS profit
FROM lineorder_flat
WHERE (C_REGION = 'AMERICA') AND (S_REGION = 'AMERICA') AND ((P_MFGR = 'MFGR#1') OR (P_MFGR = 'MFGR#2'))
GROUP BY 
year,
C_NATION
ORDER BY 
year ASC,
C_NATION ASC

┌─year─┬─C_NATION──────┬───────profit─┐
│ 1992 │ ARGENTINA │ 157402521853 │
...
│ 1998 │ UNITED STATES │ 89854580268 │
└──────┴───────────────┴──────────────┘

35 rows in set. Elapsed: 0.195 sec. Processed 89.99 million rows, 1.26 GB (460.70 million rows/s., 6.46 GB/s.)
PG:
postgres=# SELECT
date_part('year', "LO_ORDERDATE") AS year,
"C_NATION",
sum("LO_REVENUE" - "LO_SUPPLYCOST") AS profit
FROM lineorder_flat
WHERE "C_REGION" = 'AMERICA' AND "S_REGION" = 'AMERICA' AND ("P_MFGR" = 'MFGR#1' OR "P_MFGR" = 'MFGR#2')
GROUP BY
year,
"C_NATION"
ORDER BY
year ASC,
"C_NATION" ASC;
year | C_NATION | profit 
------+---------------+--------------
1992 | ARGENTINA | 157402521853
...
1998 | UNITED STATES | 89854580268
(35 rows)

Time: 195.102 ms

相干

https://github.com/adjust/cli…
https://github.com/Percona-La…
https://github.com/ClickHouse…
https://clickhouse.tech/docs/…

理解更多 PostgreSQL 技术干货、热点文集、行业动态、新闻资讯、精彩流动,请拜访中国 PostgreSQL 社区网站:www.postgresqlchina.com

退出移动版