PolarDB PostgreSQL版数据湖快速入门(内测)

本文帮助您快速理解数据湖插件的基本用法。

适用范围

您的集群版本需为:PostgreSQL 14,且修订版本为2.0.14.13.28.0及以上。

说明

您可在控制台查看内核小版本号,也可以通过SHOW polardb_version;语句查看。如未满足内核小版本要求,请升级内核小版本

重要

创建及使用数据湖相关功能需要使用超级用户操作,如有需要请提交工单联系我们处理。

创建插件

  1. 数据湖插件依赖pg_duckdb插件,请提前将pg_duckdb添加至shared_preload_libraries参数中。您可以通过控制台修改shared_preload_libraries参数,修改该参数后集群将会重启,请在修改参数前做好业务安排,谨慎操作。

  2. 执行以下命令安装插件。

    CREATE EXTENSION lakehouse WITH SCHEMA PUBLIC CASCADE;

数据访问

URL格式

支持基于阿里云对象存储OSSURL访问。URL的格式如下:

oss://<access_key>:<secret_key>@<endpoint-internal>/<bucket>/path_to/file

参数说明

  • oss需为小写。

  • access_keysecret_key为访问OSS的密钥,请替换为您实际的AccessKey

  • endpoint-internal可以被省略,省略后系统会自动寻找相应的endpoint。如果endpoint被省略,路径必须以/开头。

    说明

    endpoint-internalOSS的地域节点。为保证数据可访问性,请确保云数据库与OSS所在Region相同,并使用internal地址进行访问。相关信息请参见通过IPv6协议访问OSS

访问Parquet数据

使用read_parquet函数,用于读取Parquet数据。

  • 基础示例:

    SELECT r['sepal.length'] 
    FROM read_parquet('oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/file/iris.parquet') r
    ORDER BY r['sepal.length']  
    LIMIT 5;
    
    ---
    4.3
    4.4
    4.4
    4.4
    4.5
  • 添加sumorder by示例:

    SELECT SUM(r['price']) AS total, r['item_id']
    FROM read_parquet('oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/file/iris.parquet') r 
    GROUP BY r['item_id']
    ORDER BY total DESC
    LIMIT 3;
    
    ------------
    59.0  | 3
    58.3  | 2
    57.2  | 12

访问Iceberg数据

使用iceberg_scan函数,用于读取Iceberg数据。其中,Iceberg文件可以指定为JSON文件路径,也可以指定为目录路径。

  • JSON文件路径

    oss://<access_id>:<secret_key>@<endpoint>/<bucket>/path/lineiceberg/metadata/0270*-1e47****-4723-4f8d-a8b3-b5f0********.metadata.json
  • 目录

    oss://<access_id>:<secret_key>@<endpoint>/<bucket>/path/file.iceberg

示例

SELECT COUNT(r['l_orderkey']) FROM iceberg_scan('oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/', allow_moved_paths => true) r;

--- 
51793

写出Parquet文件

使用COPY命令将Parquet数据写出到OSS侧。

-- using copy command to write data to oss
COPY (
    <SQL>
 ) TO 'oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/file.parquet';

其中,<SQL>可以为任意数据查询语句,例如:SELECT * FROM users

混合检索

支持对数据库内表与数据湖内表进行混合检索。

SELECT r['sepal.length'] as length, t.grade  
FROM read_parquet('oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/file/iris.parquet') r, t_grade t 
where r['sepal.length']> a.min 
    and r['sepal.length'] < a.max;

length | grade 
--------+-------
    4.3 |     2
    4.4 |     2
    4.4 |     2
    4.4 |     2
    4.5 |     2
...

其中,表t_grade为数据库内表,iris.parquet为数据湖内Parquet数据。同时,还支持一些SQL聚合操作:

SELECT  a.grade, avg (r['sepal.length'])  
FROM read_parquet('oss://<access_id>:<secrect_key>@<endpoint>/<bucket>/path_to/file/iris.parquet') r, t_grade a  
where r['sepal.length']> a.min 
    and r['sepal.length'] < a.max 
group by a.grade;

 grade |        avg        
-------+-------------------
     2 | 4.690909090909092
     3 | 6.129661016949156

(可选)卸载插件

DROP EXTENSION lakehouse CASCADE;