首頁>技術>

PostgreSQL使用clickhousedb_fdw訪問ClickHouse

簡介

PostgreSQL FDW是一種外部訪問介面,它可以被用來訪問儲存在外部的資料,這些資料可以是外部的PG資料庫,也可以mysql、ClickHouse等資料庫。

ClickHouse是一款快速的開源OLAP資料庫管理系統,它是面向列的,允許使用SQL查詢實時生成分析報告。

clickhouse_fdw是一個開源的外部資料包裝器(FDW)用於訪問ClickHouse列存資料庫。

目前有以下兩款clickhouse_fdw:

https://github.com/adjust/clickhouse_fdw

一直持續不斷的有提交,目前支援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端:

生成測試表及資料,這裡我們使用CH官網提供的Star Schema Benchmark

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

模擬資料量: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/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/

15
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 「行業指南」初學者如何開始學習Hadoop?