dbt 包的构建

dbt的包是一种可以复用的代码,可以方便进行模型的共享

创建一个包

和普通的dbt 项目类似

  • 初始化(init)
dbt init [packagename]
  • 目录结构
 文件:
  README.md
  dbt_project.yml
 目录:
  models/
  macros/
  tests/
  analysis/

*## 包信息修改

  • dbt_project.yml
修改name version  以及设置profile 同时移除多余的models 配置

参考配置

dbt_project.yml:
name: 'mailchimp'
version: '0.0.1'

# HEY! Make sure you have a `mailchimp` profile
# defined in ~/.dbt/profiles.yml
profile: 'mailchimp'

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

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

models:

~/.dbt/profiles.yml:
# Place this at the bottom of your `~/.dbt/profiles.yml` file,
# below any other profiles that you've already defined

# Use the value you set for `profile:` in the `dbt_project.yml` file here
mailchimp:
  target: dev
  outputs:
    dev:
      type: xxx
      host: xxx
      user: xxx
      pass: xxx
      port: xxx
      dbname: xxx
      schema: package_mailchimp # call this whatever makes sense for you

构建一个dbt 包

{{ config(materialized='ephemeral') }}
select * from {{ var('mailchimp:campaigns_table') }}
  • 模型的前缀
    推荐是添加上包名 ,比如mailchimp_campaigns.sql 使用这个就比campaigns.sql好
  • 默认使用视图
    这个在官方的最佳实践中也说明了,同时基础模型使用 ephemeral
  • 应该进行测试
    可以使用自定义数据进行测试
  • 文档化包的使用说明以及信息
    包含描述、包的依赖图、变量,可以参考 https://github.com/fishtown-analytics/snowplow

发布包

发布包可以使用github 等源代码工具,可以方便的共享,同时我们已经版本化包的代码,同时
有相关的问题说明,对于破坏性的变更可以方便使用者进行权衡。

参考资料

https://docs.getdbt.com/docs/testing
https://docs.getdbt.com/reference#var
https://github.com/fishtown-analytics/snowplow

posted on 2018-10-21 10:09  荣锋亮  阅读(420)  评论(0编辑  收藏  举报

导航