自定义架构名称的宏不适用于 dbt 包

2024-03-23

我在 dbt 包中使用自定义架构名称时遇到问题。

我使用中提供的宏数据库测试文档 https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-custom-schemas/#how-does-dbt-generate-a-models-schema-name.

{% macro generate_schema_name(custom_schema_name, node) -%}

    {%- set default_schema = target.schema -%}
    {%- if custom_schema_name is none -%}

        {{ default_schema }}

    {%- else -%}

        {{ default_schema }}_{{ custom_schema_name | trim }}

    {%- endif -%}

{%- endmacro %}

我把这个宏放在我的 dbt 包中数据库备份包 https://github.com/datafuel/covid-france_dbt/tree/0.0.1.

最后我在另一个 dbt 项目中使用这个 dbt 包数据库测试项目 https://github.com/datafuel/DataPlatform_docker/tree/feature/dbt-package/dbt_repo/src.

这是我的 dbt 项目中的 dbt_project.yml :

name: 'covid_france'
version: '0.0.1'
config-version: 2

profile: 'default'

source-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
data-paths: ["data"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"  
clean-targets:         
    - "target"
    - "dbt_modules"

我的 dbt 包中的 dbt_project.yml :

name: 'covid_france'
version: '0.0.1'
config-version: 2

profile: 'default'

source-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
data-paths: ["data"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"  
clean-targets:         
    - "target"
    - "dbt_modules"

models:
    covid_france:
        stg:
            materialized: table
            schema: stg
        ods:
            materialized: table
            process-airbyte-outputs:
                schema: ods
            unions:
                schema: ods
        prs:
            materialized: view
      

当我尝试运行 dbt 项目时,它会导入 dbt 包,但不会应用应该从自定义架构名称中删除主架构前缀(在profiles.yml 中提供)的宏 例如:我的profiles.yml 中提供的模式是“prs”。我还有其他名为 ods 和 stg 的自定义架构。但是当 dbt 运行时,它会创建 prs、prs_ods 和 prs_stg。

当我直接在 dbt 项目中使用该宏时,该宏可以正常工作(而不是将其放入我在 dbt 项目中使用的 dbt 包中)

先感谢您 !


在文档中:https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-custom-schemas#chang-the-way-dbt-generates-a-schema-name https://docs.getdbt.com/docs/building-a-dbt-project/building-models/using-custom-schemas#changing-the-way-dbt-generates-a-schema-name

It says:

注意:dbt 会忽略项目中安装的包中的任何自定义的generate_schema_name 宏。

因此,解决方法是直接在项目中创建一个小的“垫片”或薄包装器,以调用包中的宏。

我认为您的包项目名称与实际项目名称相同(均为 dbt_project.yml 文件的第 1 行),这有点令人困惑,因此为了清楚起见,我会对它们进行不同的命名。

例如假设您将包重命名为package_project_name,使用包中已有的宏代码,但在项目中添加另一个宏,例如

{% macro generate_schema_name(custom_schema_name, node) -%}

  {{- package_project_name.generate_schema_name(custom_schema_name, node) }}

{%- endmacro %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义架构名称的宏不适用于 dbt 包 的相关文章

  • 如何使用 Google 地图 V2 删除单个标记? [复制]

    这个问题在这里已经有答案了 从地图上删除标记的唯一方法是明确的 但是它会清除地图上的所有标记 我只想删除单个标记或一组标记 我怎样才能做到这一点 添加标记后 可以获得其参考 Marker marker map addMarker The M
  • FastCGI超时错误MAMP Pro 4

    我研究了较旧的问题 https craftcms stackexchange com questions 4661 fastcgi error when accessing a local development site using ma
  • 如何找到RecyclerView中当前可见item的位置?

    我正在开发一个使用 RecyclerView 幻灯片显示图像的应用程序 我使用下面的代码水平滚动回收器视图 RecyclerView LayoutManager mLayoutManager new LinearLayoutManager

随机推荐