gpt4 book ai didi

snowflake-cloud-data-platform - DBT为雪花专栏添加评论

转载 作者:行者123 更新时间:2023-12-05 02:40:11 28 4
gpt4 key购买 nike

我们在雪花中使用 DBT 进行 ELT。想要为 Snowflake 中的每一列添加评论。每次完全刷新后使用 COMMENT 或 ALTER 命令。

决定用命令添加宏,并在on-run-end hook下调用。

{​​​​​​​% macro comment_transactions_master() %}​​​​​​​

{% if execute %}
(COMMENT ON COLUMN
"DEV_SCHEMA"."DBT_TEMP"."TR_MASTER"."TR_ID" IS 'testing comment';​​​​​​​)
{% endif %}

{​​​​​​​% endmacro %}​​​​​​​

由于有 100+ 列并且我是 DBT 新手,是否有更好的方法来执行此操作?

最佳答案

我不知道雪花,但我知道在其他数据库中,您可以像这样向表中的多个列添加注释:

comment on column schema.table (
a is 'just a comment',
b is 'just another comment'
)

为此你可以使用这个宏:

{% macro snowflake__alter_column_comment(relation, column_dict) %}

COMMENT on COLUMN {{ relation }} (
{% for column_name in column_dict %}
{% set comment = column_dict[column_name]['description'] %}
{{ column_name }} is '{{ comment }}'{%- if not loop.last %}, {% endif -%}
{% endfor %}
)

{% endmacro %}

并将其添加到雪花 persist_docs 宏中:

{% macro snowflake__persist_docs(relation, model, for_relation, for_columns) -%}
{# -- Override the persist_docs default behaviour to add the short descriptions --#}

.........


{# Add the new macro here #}
{% if for_columns and config.persist_column_docs() and model.columns %}
{% do run_query(alter_column_comment(relation, model.columns)) %}
{% endif %}

{% endmacro %}

Persist_docs 几乎在每个具体化中都有,所以你应该没问题。如果这有帮助,请告诉我。

关于snowflake-cloud-data-platform - DBT为雪花专栏添加评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68675793/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com