首页 > 其他分享 >dbt Relation 扩展简单说明

dbt Relation 扩展简单说明

时间:2024-05-01 09:01:27浏览次数:17  
标签:materialized 扩展 existing Relation relation config dbt view

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

标签:materialized,扩展,existing,Relation,relation,config,dbt,view
From: https://www.cnblogs.com/rongfengliang/p/18148771

相关文章

  • dbt 自定义AdapterPlugin 中dependencies 简单说明
    结合dbt-redshift的对于dependencies部分的定义以及使用简单说明下参考代码Plugin:AdapterPlugin=AdapterPlugin(adapter=RedshiftAdapter,#type:ignorecredentials=RedshiftCredentials,include_path=redshift.PACKAGE_PATH,dep......
  • 欧拉函数 整除分块 扩展欧几里得
    欧拉函数\(\varphi(x)\)表示求出\(1\ley\lex,\gcd(x,y)=1\)的\(y\)的数量。对于一个质数\(p\),\(\varphi(p)=p-1\)\(\varphi(p^2)=p^2-\frac{p^2}{p}=p^2-p\)\(\dots\)\(\varphi(p^i)=p^i-p^{i-1}=(p-1)\cdotp^{i-1}\)......
  • 对象属性值按长度拆分扩展一个新集合
    packagecom.xx.xx.core.utils;importcn.hutool.core.util.ReflectUtil;importcom.xx.tools.utils.BeanUtil;importcom.xx.tools.utils.StringUtil;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importlombok.experimen......
  • dbt dbt-audit-helper 包提供的一些方便macro
    dbt-audit-helper从字面意思是dbt的审计帮助工具,但是实际上我们也可以使用此工具做一些数据质量相关的东西dbt-audit-helper提供的macro比较数据输出包含了compare_relations,compare_queries,compare_row_counts比较列compare_column_values,cmpare_all_columns,com......
  • openGauss 扩展FDW与其他openGauss特性
    扩展FDW与其他openGauss特性openGauss基于PostgreSQL,而PostgreSQL没有内置存储引擎适配器,如MySQL的handlerton。为了使MOT存储引擎能够集成到openGauss中,我们利用并扩展了现有的FDW机制。随着FDW引入PostgreSQL9.1,现在可以将这些外表和数据源呈现为统一、本地可访问的关系来访问......
  • Linux扩展根目录磁盘容量
    https://www.cnblogs.com/kk8085/articles/17227008.html1、先查看磁盘分区是否是lvm格式执行lsblk命令,可以看到root为lvm类型,表示磁盘可以扩容 2、查看磁盘容量执行fdisk-l可以看到有一个500多G的盘没有使用3、新建分区根据第2步获取未使用的磁盘路径:/dev/vdb执......
  • Chrome插件(扩展)开发全攻略 写在前面 我
    Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的,所以转载务必保留出处。本文所有涉及到的大部分代码均在这个demo里面:https://github.com/sxei/chrome-plugin-demo ,大家可以直接下载下来运行。......
  • python下载和扩展文件下载
    (三)、练习安装Python扩展库【实验截图】1、在资源管理器中进入Python安装目录的scripts子目录,然后按下Shift键,在空白处单击鼠标右键,在弹出来的菜单中选择“在此处打开命令窗口”进入命令提示符环境   2.使用pip命令在线安装Python扩展库numpy、pandas、scipy......
  • dbt seed 处理简单说明
    dbt支持基于seed的快速建模处理(比较适合测试环境使用),我们只需要提供csv格式的文件,之后执行dbtseed就会创建对应的模型,之后我们就可以在dbt模型中引用了,以下简单说明下内部实现以及处理参考使用seed文件位置一般我们会在dbt项目的seed目录中放对应的seed文件,就是......
  • 为 AntdUI 扩展一个 MessageBox 方法
    AntdUI是个很不错的开源WinFrom界面组件,使用中感觉消息对话框调用有点麻烦,于是按照MessageBox.Show的使用习惯,增加了一个扩展方法来调用,废话不多说,直接上代码。1usingSystem.Windows.Forms;23namespaceAntdUI4{5publicstaticclassWindowExtent......