PostgreSQL使用clickhousedb_fdw訪問ClickHouse

作者:楊杰

簡介

PostgreSQL FDW是一種外部訪問接口,它可以被用來訪問存儲在外部的數(shù)據(jù),這些數(shù)據(jù)可以是外部的PG數(shù)據(jù)庫,也可以mysql、ClickHouse等數(shù)據(jù)庫。

ClickHouse是一款快速的開源OLAP數(shù)據(jù)庫管理系統(tǒng),它是面向列的,允許使用SQL查詢實時生成分析報告。

clickhouse_fdw是一個開源的外部數(shù)據(jù)包裝器(FDW)用于訪問ClickHouse列存數(shù)據(jù)庫。

目前有以下兩款clickhouse_fdw:

https://github.com/adjust/clickhouse_fdw

一直持續(xù)不斷的有提交,目前支持PostgreSQL 11-13

https://github.com/Percona-Lab/clickhousedb_fdw

之前有一年時間沒有動靜,最近一段時間剛從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端:

生成測試表及數(shù)據(jù),這里我們使用CH官網(wǎng)提供的Star Schema Benchmark

https://clickhouse.tech/docs/en/getting-started/example-datasets/star-schema/#star-schema-benchmark

模擬數(shù)據(jù)量:5張數(shù)據(jù)表,數(shù)據(jù)主要集中在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端:

創(chuàng)建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)

創(chuàng)建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)

創(chuàng)建用戶映射

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)

創(chuàng)建外部表

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是區(qū)分大小寫的以及一些函數(shù)兼容問題,上面的示例也有展示。

測試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


相關(guān)

https://github.com/adjust/clickhouse_fdw

https://github.com/Percona-Lab/clickhousedb_fdw

https://github.com/ClickHouse/ClickHouse

https://clickhouse.tech/docs/en/getting-started/example-datasets/star-schema/


了解更多PostgreSQL技術(shù)干貨、熱點文集、行業(yè)動態(tài)、新聞資訊、精彩活動,請訪問中國PostgreSQL社區(qū)網(wǎng)站:www.postgresqlchina.com

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容