簡體   English   中英

使用dbt時如何手動創建表或使用postgres分區?

[英]How to create table manually or use postgres partition when using dbt?

我想使用 dbt 將數據插入到分區表中,但發現不支持 dbt postgres 分區。

通過另一種方式,我在 pre_hook 中創建表和分區,但在運行 dbt 時出現錯誤“關系‘download_counts_p’已經存在”

有什么建議嗎? 這是我的 SQL 和 pre_hook 配置

 {{ config( materialized = 'table', indexes = [ ], pre_hook=[ 'CREATE TABLE IF NOT EXISTS "download_counts_p" ( "channel_id" int8 NOT NULL, "product_id" int8 NOT NULL, "country_code" text NOT NULL, "year" int2 NULL, "month" int2 NOT NULL, "count" int8 NOT NULL, "count" int8 NOT NULL, "months" int8 NOT NULL ) partition by list(country_code)', "DO $$ Declare unique_country_code varchar; BEGIN FOR unique_country_code IN SELECT country_code as unique_country_code FROM download_counts group by country_code LOOP EXECUTE format('create table IF NOT EXISTS download_counts_p_%s partition of download_counts_p for values in (''%s'')', upper(unique_country_code), unique_country_code); END LOOP; END; $$;"] )}} select 1

在此處輸入圖像描述

這里發生了一些不同的事情。

  1. 您是正確的,dbt 的 postgres 適配器當前不支持分區表的配置。 這里的解決方案是創建一個新的物化,插入您需要的額外 DDL。 您不想在 dbt 中手動編寫 DDL——這被認為是一個很大的反模式。 您可能還想在 dbt-core(postgres 適配器代碼所在的位置)中打開一個問題以提出功能請求
  2. 您的FOR... LOOP... END LOOP掛鈎實際上應該使用 jinja 循環編寫為它自己的 model。 您可以使用run_query宏將數據返回到神社上下文。 該頁面底部有一個示例,它將查詢結果提取到神社上下文中,然后使用{% for payment_method in results_list %}循環遍歷它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM