【问题标题】:Unable to use Jinja variable inside adapter.get_relation in DBT无法在 DBT 中的 adapter.get_relation 中使用 Jinja 变量
【发布时间】:2021-08-30 11:48:39
【问题描述】:

我正在尝试在我的 adapter.get_relation 中使用 Jinja 变量,但我无法这样做 :confused: 代码


{% set the_var = 'amazon_full_orders_denormalized_' ~ company_uuid %}

{{ log(the_var, info=True) }}

{%- set source_relation = adapter.get_relation(
      database='282615',
      schema='airbyte_mumbai',
      identifier= the_var ) -%} 



我正在使用的命令

dbt compile --vars '{"company_uuid":"0703afd3_496b_4ed5_8e0c_594b71f4718b","dataset":"airbyte_mumbai"}' --models tag:copy_reports

变量 the_var 被设置为amazon_full_orders_denormalized_

Found 6 models, 0 tests, 0 snapshots, 0 analyses, 165 macros, 0 operations, 0 seed files, 0 sources, 0 exposures

17:11:08 | Concurrency: 3 threads (target='dev')
17:11:08 | 
amazon_full_orders_denormalized_
Table does not exist
17:11:08 | Done.

而我希望将其设置为 amazon_full_orders_denormalized_0703afd3_496b_4ed5_8e0c_594b71f4718b

【问题讨论】:

  • 代替identifier = the_var,试试这个identifier=the_var+var('company_uuid')。我认为company_uuid 变量没有附加到变量the_var

标签: jinja2 dbt


【解决方案1】:

在 dbt 中使用变量有时会很困难!我认为评论者@Kay 在这里是正确的,因为这里发生了三个变量:the_varcompany_uuiddataset。看起来您想要一个由the_varcompany_uuid 串联而成的表名,您可以使用jinja 的concat 运算符~ 来实现,如下所示:

-- just for testing
{{ log(the_var, info=True) }}
{{ log(var('company_uuid'), info=True) }}
{{ log(the_var ~ var('company_uuid'), info=True) }}

-- see change to `identifier`
{%- set source_relation = adapter.get_relation(
      database='282615',
      schema='airbyte_mumbai',
      identifier= the_var ~ var('company_uuid') ) -%} 

【讨论】:

  • 谢谢安德斯。这就是我一直在寻找的。我尝试了多种使用~ company_uuid / ~ {{ company_uuid }} / ~ {{ var("company_uuid") }} 的方法,但它们都没有工作
  • 我明白了——神社有时很奇怪!请记住,如果您已经在大括号内,则不需要大括号来引用变量
猜你喜欢
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
相关资源
最近更新 更多