dbt docs block 简单说明

dbt docs block 是一个jinja2 bblock 的扩展,以下是一个简单的说明

参考使用

  • 定义
{% docs table_events %}
 
This table contains clickstream events from the marketing website.
 
The events in this table are recorded by Snowplow and piped into the warehouse on an hourly basis. The following pages of the marketing site are tracked:
 - /
 - /about
 - /team
 - /contact-us
 
{% enddocs %}
  • 使用

通过类似ref 模式引用

version: 2
models:
  - name: events
    description: '{{ doc("table_events") }}'
    columns:
      - name: event_id
        description: This is a unique identifier for the event
        tests:
            - unique
            - not_null

参考处理

  • DocumentationExtension 扩展

处理上与MaterializationExtension 类似

class DocumentationExtension(jinja2.ext.Extension):
    tags = ["docs"]
 
    def parse(self, parser):
        node = jinja2.nodes.Macro(lineno=next(parser.stream).lineno)
        docs_name = parser.parse_assign_target(name_only=True).name
 
        node.args = []
        node.defaults = []
        node.name = get_docs_macro_name(docs_name)
        node.body = parser.parse_statements(("name:enddocs",), drop_needle=True)
        return node
  • get_docs_macro_name

工具类进行docs macro 的前缀

def get_dbt_docs_name(name):
    if name is None:
        raise dbt.exceptions.DbtInternalError("Got None for a doc name!")
    # 前缀 dbt_docs__
    return f"{DOCS_PREFIX}{name}"

说明

dbt docs block 的处理实际上就是一个标准jinja2 block 扩展,只是方便使用

参考资料

https://docs.getdbt.com/docs/collaborate/documentation
https://docs.getdbt.com/reference/project-configs/docs-paths
https://docs.getdbt.com/reference/dbt-jinja-functions/doc

posted on 2024-04-21 08:00  荣锋亮  阅读(3)  评论(0编辑  收藏  举报

导航