简体   繁体   English

DBT 使用宏设置变量

[英]DBT set variable using macros

my goal is to get the last 2 dates from the tables and run insert_overwrite to load incremental on a large table.我的目标是从表中获取最后 2 个日期并运行 insert_overwrite 以在大表上加载增量。 I am trying to set a variable inside the model by calling on the macros I wrote.我试图通过调用我编写的宏在 model 中设置一个变量。 The SQL query is in BigQuery. SQL 查询位于 BigQuery 中。

I get an error message.我收到一条错误消息。

'None' has no attribute 'table'

inside model model内部

{% set dates = get_last_two_dates('window_start',source('raw.event','tmp')) %}

macros

{% macro get_last_two_dates(target_column_name, target_table = this) %}

{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}

{% set max_value = run_query(query).columns[0][0] %}
{% do return(max_value) %}

{% endmacro %}

Thanks in advance.提前致谢。 let me know if you have any other questions.如果您还有其他问题,请告诉我。

You probably need to wrap {% set max_value... %} with an {% if execute %} block:您可能需要用{% if execute %}块包装{% set max_value... %}

{% macro get_last_two_dates(target_column_name, target_table = this) %}

{% set query %}
select string_agg(format('%T',target_date),',') target_date_string
from (
SELECT distinct date({{ target_column_name }}) target_date
FROM {{ target_table }}
order by 1 desc
LIMIT 2
) a
{% endset %}

{% if execute %}
{% set max_value = run_query(query).columns[0][0] %}
{% else %}
{% set max_value = "" %}
{% endif %}
{% do return(max_value) %}

{% endmacro %}

The reason for this is that your macro actually gets run twice -- once when dbt is scanning all of the models to build the DAG, and a second time when the model is actually run.原因是您的宏实际上运行了两次——一次是在 dbt 扫描所有模型以构建 DAG 时,第二次是在实际运行 model 时。 execute is only true for this second pass. execute仅在第二次通过时才成立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 BigQuery 中使用 Jinja 的 dbt 宏 - dbt macro using Jinja in BigQuery 使用 DBT 在 bigquery 中更新表 - Updating a table in bigquery using DBT dbt Jinja 宏将列集传递给列表并执行计算和重命名 - dbt Jinja macro to pass set of columns to a list and perform calculations and renaming DBT Cloud:尝试根据变量在数据库中有条件地创建表 - DBT Cloud: Trying to conditional create tables in a DB based on variable 如何使用 jinja 访问 DBT 中的 BigQuery 表元数据? - How to access BigQuery table metadata in DBT using jinja? 使用 Kube.netesPodOperator 错误从 Airflow 在 GKE / Kube.netes 上部署 DBT pod - Deploying DBT pod on GKE / Kubernetes from Airflow using KubernetesPodOperator Error 使用参数设置器的 Datafusion 中的宏 - Macros in Datafusion using Argument setter 使用 aws redshift 在 dbt 中实现 scd2,如何定义条件自然键? - Implementing scd2 in dbt using aws redshift, how do I define conditional natural keys? Gitlab CI - 使用导出设置变量 - Gitlab CI - set variable using export 使用 Cloud Datapreb 与使用 DBT 在层之间转换数据有什么区别? - What are the differences between using Cloud Datapreb vs using DBT for transforming data between layers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM