本文帮助您快速理解数据湖插件的基本用法。
适用范围
您的集群版本需为:PostgreSQL 14,且修订版本为2.0.14.13.28.0及以上。
创建及使用数据湖相关功能需要使用超级用户操作,如有需要请提交工单联系我们处理。
创建插件
数据湖插件依赖
pg_duckdb插件,请提前将pg_duckdb添加至shared_preload_libraries参数中。您可以通过控制台修改shared_preload_libraries参数,修改该参数后集群将会重启,请在修改参数前做好业务安排,谨慎操作。执行以下命令安装插件。
CREATE EXTENSION lakehouse WITH SCHEMA PUBLIC CASCADE;
数据访问
URL格式
支持基于阿里云对象存储OSS的URL访问。URL的格式如下:
oss://<access_key>:<secret_key>@<endpoint-internal>/<bucket>/path_to/file参数说明
oss需为小写。access_key和secret_key为访问OSS的密钥,请替换为您实际的AccessKey。endpoint-internal可以被省略,省略后系统会自动寻找相应的endpoint。如果endpoint被省略,路径必须以/开头。说明endpoint-internal为OSS的地域节点。为保证数据可访问性,请确保云数据库与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添加
sum和order 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;