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

posted on   荣锋亮  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用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 的代码统计分析情况

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示