dbt config macro 简单说明

dbt 不少资源类型都支持config macro 的使用,可以进行灵活的配置管理

参考使用

  • 配置
    模型的执行物化策略
{{ config(
    materialized="<materialization_name>",
    sql_header="<string>"
) }}

快照的

{{ config(
    target_schema="<string>",
    target_database="<string>",
    unique_key="<column_name_or_expression>",
    strategy="timestamp" | "check",
    updated_at="<column_name>",
    check_cols=["<column_name>"] | "all"
) }}
  • 获取config
{% materialization incremental, default -%}
  {%- set unique_key = config.get('unique_key') -%}
  ...
  • 提供的方法
    get,require,

内部处理

实际上就是一个类,实现了一些方法

  • 参考配置
class Config(Protocol):
    def __init__(self, model, context_config: Optional[ContextConfig]):
        ...
 
# 这个是解析的,当然还有不少其他的实现
# Implementation of "config(..)" calls in models
class ParseConfigObject(Config):
    def __init__(self, model, context_config: Optional[ContextConfig]):
        self.model = model
        self.context_config = context_config
 
    def _transform_config(self, config):
        for oldkey in ("pre_hook", "post_hook"):
            if oldkey in config:
                newkey = oldkey.replace("_", "-")
                if newkey in config:
                    raise ConflictingConfigKeysError(oldkey, newkey, node=self.model)
                config[newkey] = config.pop(oldkey)
        return config
 
    def __call__(self, *args, **kwargs):
        if len(args) == 1 and len(kwargs) == 0:
            opts = args[0]
        elif len(args) == 0 and len(kwargs) > 0:
            opts = kwargs
        else:
            raise InlineModelConfigError(node=self.model)
 
        opts = self._transform_config(opts)
 
        # it's ok to have a parse context with no context config, but you must
        # not call it!
        if self.context_config is None:
            raise DbtRuntimeError("At parse time, did not receive a context config")
        self.context_config.add_config_call(opts)
        return ""
 
    def set(self, name, value):
        return self.__call__({name: value})
 
    def require(self, name, validator=None):
        return ""
 
    def get(self, name, default=None, validator=None):
        return ""
 
    def persist_relation_docs(self) -> bool:
        return False
 
    def persist_column_docs(self) -> bool:
        return False
  • 上下文处理
    render_with_context 会使用_context_for ,_context_for会调用generate_parser_model_context 会使用到config
def generate_parser_model_context(
    model: ManifestNode,
    config: RuntimeConfig,
    manifest: Manifest,
    context_config: ContextConfig,
) -> Dict[str, Any]:
    # The __init__ method of ModelContext also initializes
    # a ManifestContext object which creates a MacroNamespaceBuilder
    # which adds every macro in the Manifest.
    ctx = ModelContext(model, config, manifest, ParseProvider(), context_config)
    # The 'to_dict' method in ManifestContext moves all of the macro names
    # in the macro 'namespace' up to top level keys
    return ctx.to_dict()

说明

以上是关于dbt config的简单说明,对于详细使用可以参考官方文档

参考资料

https://docs.getdbt.com/reference/data-test-configs
https://docs.getdbt.com/reference/dbt-jinja-functions/config

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

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-05-12 mountpoint-s3 新rpm包
2020-05-12 inversify 强大&&轻量级的基于typescript 的ioc 框架
2020-05-12 VictoriaMetrics vmagent的一些介绍
2020-05-12 VictoriaMetrics vmalert 说明
2020-05-12 VictoriaMetrics vmauth 说明
2019-05-12 cube.js 学习(十一)cube + gitbase 分析git 代码
2019-05-12 gitbase 集成sqler 进行git 代码分析

导航

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