dbt adapter macro 简单说明
dbt 的adapter macro 提供了方便的在macro 中对于db 操作的能力,让macro 具有了动态能力,默认包含了不少实现
而且在不少dbt 项目中经常看到
参考使用
结合了api 这个macro 提供了能力,进行ddl 的维护
{%- set target_relation = api.Relation.create(
database='database_name',
schema='schema_name',
identifier='table_name') -%}
{% for col in adapter.get_missing_columns(target_relation, this) %}
alter <Term id="table" /> {{this}} add column "{{col.name}}" {{col.data_type}};
{% endfor %}
参考处理
- 实现代码
# 基于dbt contextproperty装饰器处理的,包装的dbt 项目特定的adapter 操作,比如dispatch,create_schema,drop_schema,get_relation
。。。。
@contextproperty("adapter")
def ctx_adapter(self) -> BaseDatabaseWrapper:
"""`adapter` is a wrapper around the internal database adapter used by
dbt. It allows users to make calls to the database in their dbt models.
The adapter methods will be translated into specific SQL statements
depending on the type of adapter your project is using.
"""
# db_wrapper 实际上就是实现了adapter protocol 的实现,只是额外包装了一些新的功能
return self.db_wrapper
说明
dbt adapter macro 提供的一些能力比较方便,可以让macro 实现不少动态能力
参考资料
core/dbt/context/providers.py
https://docs.getdbt.com/reference/dbt-classes
https://docs.getdbt.com/reference/dbt-jinja-functions/adapter
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2023-04-18 ipp-server 基于python实现的不错的ipp 服务
2021-04-18 使用k6 压测cube.js 请求接口
2021-04-18 cube.js TimeoutError: ResourceRequest timed out 问题
2020-04-18 easy-batch job 调度
2020-04-18 easy-batch job 监控
2020-04-18 easy-batch job 配置
2020-04-18 使用bloom 加速sqler + gitbase 的代码统计分析情况