dbt Relation 扩展简单说明

dbt 的Relation 实际上就是包含关系数据库表,数据库,schema 一些信息的描述,dbt 官方提供了api.Relation.create 等操作
进行Relation 的维护,当然系统的builtins 也提供了一些基本的操作能力, 内部使用上会使用dbt BaseRelation 中的各类实现
比如pg 就自己扩展了一个,同时在自己的macro 中使用

参考扩展

  • 定义
def get_materialized_view_config_change_collection(
    self, relation_results: RelationResults, relation_config: RelationConfig
) -> Optional[PostgresMaterializedViewConfigChangeCollection]:
    config_change_collection = PostgresMaterializedViewConfigChangeCollection()
 
    existing_materialized_view = PostgresMaterializedViewConfig.from_relation_results(
        relation_results
    )
    new_materialized_view = PostgresMaterializedViewConfig.from_config(relation_config)
 
    config_change_collection.indexes = self._get_index_config_changes(
        existing_materialized_view.indexes, new_materialized_view.indexes
    )
 
    # we return `None` instead of an empty `PostgresMaterializedViewConfigChangeCollection` object
    # so that it's easier and more extensible to check in the materialization:
    # `core/../materializations/materialized_view.sql` :
    #     {% if configuration_changes is none %}
    if config_change_collection.has_changes:
        return config_change_collection
    return None
  • 使用
{% macro postgres__get_materialized_view_configuration_changes(existing_relation, new_config) %}
    {% set _existing_materialized_view = postgres__describe_materialized_view(existing_relation) %}
    {% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config.model) %}
    {% do return(_configuration_changes) %}
{% endmacro %}

一个简单扩展

如果为了测试我们可以直接修改一个dbt adapter relation.py 中的一个实现,然后通过macro 就可以调用了,参考如下

  • 修改

为了简单我直接修改的dremio 的relation,添加了一个新的方法

def dalongapp(self):
    return "dalongapp"
  • 测试

直接在一个模型中引用

{% set rel = builtins.ref("app") %}
{{log("demo  " ~ rel.dalongapp(), info=True)}}
  • 效果

如下日志

说明

实际上dbt 不少东西都是可以扩展的,而且扩展地方不少,只要我们上下文传递了一些信息,dbt 模型中基本都能使用到(和方法可见范围也有关系),以上是一个简单说明,也有利于我们去学习一些三方的dbt adapter

参考资料

https://docs.getdbt.com/reference/dbt-jinja-functions/builtins
https://docs.getdbt.com/reference/dbt-classes

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

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-05-01 juicefs 方便mac 进行utm 虚拟机的文件共享
2023-05-01 通过sshfs 方便mac 进行utm 虚拟机的文件共享
2023-05-01 bytehound 参考试用
2023-05-01 bytehound centos 7构建说明
2023-05-01 bytehound linux 内存profiler工具
2022-05-01 openresty 提供的几个新的生命周期阶段指令
2022-05-01 openresty lua 文件处理的一些技巧

导航

< 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
点击右上角即可分享
微信分享提示