snowflake-cloud-data-platform - DBT 向雪花列添加注释
问题描述
我们在雪花中将 DBT 用于 ELT。想要为 Snowflake 中的每一列添加评论。每次完全刷新后使用 COMMENT 或 ALTER 命令。
决定添加带有命令的宏,并在 on-run-end 挂钩下调用它。
{% 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
几乎在每个物化中,所以你应该没问题。让我知道这是否有帮助。
推荐阅读
- matlab-deployment - 如何使用用户预定义的数据设置 mwArray?是否可以混合使用 Matlab 库编译器和 mex 运行时?
- firebase - 应用程序处于前台时未收到 Firebase 推送通知
- excel - 自定义订单排序
- r - 在数据类型列表上使用 cat 函数
- python - Google Vision - 从 xls 读取网址
- javascript - Angular HttpInterceptor:如何在多个条件下使用 RxJS
- java - Talend:将 oracle 输入保存到全局变量
- c# - C# MVC 将 DB2 添加到数据源列表
- php - YII2 我如何处理多对多关系?
- hyperledger-fabric - 在超级账本结构中创建组织单元时出错