Skip to content

种子

简介

种子是dbt项目中的CSV文件(通常在seeds目录中),dbt可以使用dbt seed命令将其加载到数据仓库中。

种子可以在下游模型中引用,方法与引用模型相同——使用ref函数

因为这些CSV文件位于您的dbt代码库中,所以它们是受版本控制的,并且可以查看代码。种子最适合于很少更改的静态数据。

良好的种子使用案例:

  • 国家/地区代码到国家/地区名称的映射列表
  • 要从分析中排除的测试电子邮件列表
  • 员工帐户ID列表

不良的种子使用案例:

  • 加载已导出到CSV的原始数据
  • 包含敏感信息的任何类型的生产数据。例如个人身份信息和密码.

示例

从你在dbt项目中加载一个种子文件: 1. 将.csv文件放到 seeds 目录, 例如 seeds/country_codes.csv

country_code,country_name
US,United States
CA,Canada
GB,United Kingdom
...

  1. 运行 dbt seed 命令 — 在数据仓库的目标schema里会创建一个新的表, 名称为 country_codes

    $ dbt seed
    
    Found 2 models, 3 tests, 0 archives, 0 analyses, 53 macros, 0 operations, 1 seed file
    
    14:46:15 | Concurrency: 1 threads (target='dev')
    14:46:15 |
    14:46:15 | 1 of 1 START seed file analytics.country_codes........................... [RUN]
    14:46:15 | 1 of 1 OK loaded seed file analytics.country_codes....................... [INSERT 3 in 0.01s]
    14:46:16 |
    14:46:16 | Finished running 1 seed in 0.14s.
    
    Completed successfully
    
    Done. PASS=1 ERROR=0 SKIP=0 TOTAL=1
    

  2. 在下游模型中使用ref函数来引用种子

-- This refers to the table created from seeds/country_codes.csv
select * from {{ ref('country_codes') }}

配置种子

种子是在dbt_project.yml中配置的,请查看种子配置文档以获取可用配置的完整列表。

记录和测试种子

您可以通过声明属性来记录和测试yaml中的种子-有关更多信息,请查看seed属性上的文档。