本手册介绍如何使用 dbt(data build tool)连接阿里云 Hologres 实时数仓,进行数据建模和转换。
概述
本手册介绍如何使用 dbt(data build tool)连接阿里云 Hologres 实时数仓,进行数据建模和转换。
关于 dbt-alibaba-cloud-hologres:
-
维护者:阿里云 Hologres 团队
-
GitHub 仓库:aliyun/dbt-hologres
-
PyPI 包名:dbt-alibaba-cloud-hologres
-
支持的 dbt Core 版本:v1.8.0 及更高版本
-
最低 Hologres 版本要求:4.0 以及以上版本
安装 dbt-alibaba-cloud-hologres
使用 pip 进行安装。从 dbt 1.8 版本开始,安装适配器不再自动安装 dbt-core,因此需要同时安装这两个包:
python -m pip install dbt-core dbt-alibaba-cloud-hologres
dbt 1.8 之前的版本会自动安装 dbt-core,但从 1.8 开始需要手动指定。这样做是为了将适配器和 dbt Core 版本解耦,避免覆盖现有的 dbt-core 安装。
配置 Hologres 连接
配置文件位置
dbt 的连接配置文件位于:
~/.dbt/profiles.yml
配置示例
dbt-alibaba-cloud-hologres: # 此名称需要与 dbt_project.yml 文件中的 profile 匹配
target: dev
outputs:
dev:
type: hologres
host: HOST_NAME
port: 80
user: USER_NAME
password: PASSWORD
database: DATABASE_NAME
schema: SCHEMA_NAME
threads: 4
连接参数说明
|
参数 |
说明 |
是否必填 |
默认值 |
示例 |
|
type |
数据库连接类型,必须设置为 hologres |
必填 |
- |
hologres |
|
host |
Hologres 实例的连接端点 |
必填 |
- |
hgxxx-xxx.hologres.aliyuncs.com |
|
port |
Hologres 连接端口号 |
可选 |
80 |
80 |
|
user |
Hologres 认证用户名(区分大小写) |
必填 |
- |
AccessKey ID |
|
password |
Hologres 认证密码(区分大小写) |
必填 |
- |
AccessKey Secret |
|
database |
Hologres 数据库名称 |
必填 |
- |
my_database |
|
schema |
模型使用的默认 schema(如不需要可使用空字符串) |
必填 |
- |
public |
|
threads |
并行执行的线程数 |
可选 |
1 |
4 |
|
connect_timeout |
连接超时时间(秒) |
可选 |
10 |
10 |
|
sslmode |
SSL 连接模式 |
可选 |
disable |
disable |
|
application_name |
应用程序标识符,用于连接跟踪 |
可选 |
dbt_hologres_{version} |
my_dbt_app |
|
retries |
连接重试次数 |
可选 |
1 |
3 |
身份认证配置
使用 AccessKey 认证
dbt-alibaba-cloud-hologres 使用标准的 PostgreSQL 兼容认证机制,通过用户名和密码(AccessKey)进行认证。Hologres 支持使用阿里云 AccessKey 或 RAM 用户凭证进行认证。
完整配置示例:
jaffle_shop: # 此名称需要与 dbt_project.yml 文件中的 profile 匹配
target: dev
outputs:
dev:
type: hologres
host: hgxxx-cn-shanghai.hologres.aliyuncs.com # 替换为你的 Hologres 端点
port: 80
user: your_access_key_id # 替换为你的 AccessKeyId
password: your_access_key_secret # 替换为你的 AccessKeySecret
database: my_database # 替换为你的数据库名称
schema: public # 替换为你的 schema 名称
threads: 4
connect_timeout: 10
sslmode: disable
-
大小写敏感:Hologres 的用户名和密码区分大小写,请确保输入与配置完全一致。
-
默认端口:Hologres 的默认端口是 80,与标准 PostgreSQL 端口 5432 不同。
-
SSL 模式:Hologres 连接默认禁用 SSL。如需启用,可将 sslmode 设置为适当的值。
-
安全建议:出于安全考虑,建议创建具有适当权限的 RAM 子账号,而不是使用主账号的 AccessKey。
测试连接
配置完 profiles.yml 后,可以运行以下命令验证连接:
dbt debug
此命令将测试与 Hologres 实例的连接,并报告任何配置问题。预期输出包括:显示配置文件路径、验证连接参数、测试数据库连接、报告连接状态。
Hologres 特有功能
动态表(Dynamic Tables)
动态表是 Hologres 对物化视图的实现,支持自动刷新功能。刷新数据时支持多种模式:full(全量刷新)、incremental(增量刷新)。
在 dbt 模型中配置动态表:
models:
my_model:
materialized: dynamic_table
freshness: "30 minutes"
auto_refresh_mode: auto
computing_resource: serverless
动态表支持的配置参数如下表所示。
|
配置项 |
说明 |
示例值 |
|
freshness |
数据新鲜度要求 |
"30 minutes", "1 hours" |
|
auto_refresh_mode |
动态表的刷新模式 |
auto, incremental, full |
|
computing_resource |
用于刷新的计算资源 |
serverless, local, warehouse name |
增量模型
dbt-alibaba-cloud-hologres 支持多种增量策略:append(简单追加)、delete+insert(删除匹配记录并插入新记录)、merge(使用 MERGE 语句进行 upsert)、microbatch(小批量处理)。
增量模型示例:
{{ config(
materialized='incremental',
unique_key='id',
incremental_strategy='merge'
)
}} SELECT id, name, updated_at FROM {{ source('raw', 'users') }} {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% endif %}
约束支持
Hologres 完全支持以下数据库约束:主键(Primary Keys)、非空约束(Not Null Constraints)。
表属性配置
Hologres 支持以下表属性,可以根据业务场景优化性能:
|
属性 |
最佳实践 |
|
orientation |
OLAP 工作负载使用 column(列存),键值查询使用 row(行存) |
|
distribution_key |
选择经常进行连接或分组的列;优先使用单列 |
|
clustering_key |
用于范围过滤的列;最多 3 列;遵循左匹配原则 |
|
event_time_column |
为时间序列数据设置(时间戳列) |
|
bitmap_columns |
用于等值过滤的列 |
|
dictionary_encoding_columns |
用于低基数的字符串列 |
表属性配置示例:
{{ config(
materialized='table',
orientation='column',
distribution_key='user_id',
clustering_key='event_time,user_id',
event_time_column='event_time',
bitmap_columns='status,category',
dictionary_encoding_columns='region,city'
)
}} SELECT user_id, event_time, status, category, region, city, amount FROM {{ source('raw', 'events') }}
常见使用场景
创建基础模型
-- models/staging/stg_orders.sql
{{ config( materialized='view' ) }}
SELECT
order_id,
customer_id,
order_date,
total_amount
FROM {{ source('raw', 'orders') }}
WHERE order_date >= '2024-01-01'
创建物化表
-- models/marts/fact_orders.sql
{{ config(
materialized='table',
orientation='column',
distribution_key='customer_id',
clustering_key='order_date'
)
}}
SELECT
o.order_id,
o.customer_id,
c.customer_name,
o.order_date,
o.total_amount
FROM {{ ref('stg_orders') }} o
LEFT JOIN {{ ref('stg_customers') }} c
ON o.customer_id = c.customer_id
创建增量模型
-- models/incremental/daily_sales.sql
{{ config(
materialized='incremental',
unique_key='date_key',
incremental_strategy='delete+insert'
)
}}
SELECT
DATE(order_date) AS date_key,
COUNT(*) AS order_count,
SUM(total_amount) AS total_sales
FROM {{ ref('stg_orders') }}
{% if is_incremental() %}
WHERE order_date >= (SELECT MAX(date_key) FROM {{ this }})
{% endif %}
GROUP BY DATE(order_date)
常用 dbt 命令
基础命令:
# 测试连接
dbt debug
# 运行所有模型
dbt run
# 运行特定模型
dbt run --select model_name
# 运行特定目录下的模型
dbt run --select staging.*
# 测试数据质量
dbt test
# 生成文档
dbt docs generate
# 启动文档服务
dbt docs serve
增量运行:
# 仅运行增量模型
dbt run --select config.materialized:incremental
# 全量刷新增量模型
dbt run --select model_name --full-refresh
故障排查
连接问题
问题:无法连接到 Hologres。
解决方案:
-
检查 host 配置是否正确;
-
确认端口号为 80;
-
验证 AccessKey ID 和 Secret 是否正确(注意大小写);
-
检查网络连接和防火墙设置;
-
确认 Hologres 实例状态正常。
认证失败
问题:认证失败错误。
解决方案:
-
确认 AccessKey 未过期。
-
检查 RAM 用户权限是否足够。
-
验证用户名和密码没有多余的空格。
-
确保使用正确的 AccessKey(区分主账号和子账号)。
模型运行错误
问题:模型运行失败。
解决方案:
-
检查 SQL 语法是否符合 Hologres 规范。
-
验证表属性配置是否合理。
-
确认 schema 和 database 权限。
-
查看详细错误日志:
dbt run --debug。
性能优化建议
表设计优化
-
选择合适的存储方式:OLAP 分析场景使用列存(column),点查询场景使用行存(row)。
-
合理设置分布键:选择高基数的列;避免数据倾斜;优先考虑关联和分组字段。
-
设置聚簇键:用于范围查询的列;遵循左匹配原则;不超过 3 个列。
模型优化
-
使用增量模型:对于大表,使用增量更新而非全量刷新。
-
合理设置并行度:根据 Hologres 实例规格调整 threads 参数。
-
利用动态表:对于需要定时刷新的汇总表,使用动态表功能。