首页 > 其他分享 >dbt seed 处理简单说明

dbt seed 处理简单说明

时间:2024-04-27 09:05:03浏览次数:13  
标签:model self 简单 seed agate table path dbt

dbt 支持基于seed 的快速建模处理(比较适合测试环境使用),我们只需要提供csv 格式的文件,之后执行dbt seed 就会创建对应的模型,之后我们就可以在
dbt 模型中引用了,以下简单说明下内部实现以及处理

参考使用

  • seed 文件位置

一般我们会在dbt 项目的seed 目录中放对应的seed 文件,就是一个csv 格式文件(目前是强要求)

  • 模型引用
    models/demoapp.sql
select * from {{ref('app')}}

内部处理

内部实现上实际上seed 是一种物化,对于seed 中文件的加载dbt 使用了agate 这个强大的python 数据包,没有使用pandas,对于seed 创建模型的数据schema 信息(列类型信息)dbt 是利用了agate 的自动推导能力

  • 内部参考处理
    core/dbt/include/global_project/macros/materializations/seeds/seed.sql
{% materialization seed, default %}
 
  {%- set identifier = model['alias'] -%}
  {%- set full_refresh_mode = (should_full_refresh()) -%}
 
  {%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
 
  {%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%}
  {%- set exists_as_view = (old_relation is not none and old_relation.is_view) -%}
 
  {%- set grant_config = config.get('grants') -%}
  #  此处使用了dbt 暴露的load_agate_table 上下文方法
  {%- set agate_table = load_agate_table() -%}
  -- grab current tables grants config for comparision later on
  # 此处使用了dbt 暴露的store_result上下文方法进行结果存储,方便复用
  {%- do store_result('agate_table', response='OK', agate_table=agate_table) -%}
 
  {{ run_hooks(pre_hooks, inside_transaction=False) }}
 
  -- `BEGIN` happens here:
  {{ run_hooks(pre_hooks, inside_transaction=True) }}
 
  -- build model
  {% set create_table_sql = "" %}
  {% if exists_as_view %}
    {{ exceptions.raise_compiler_error("Cannot seed to '{}', it is a view".format(old_relation)) }}
  {% elif exists_as_table %}
    {% set create_table_sql = reset_csv_table(model, full_refresh_mode, old_relation, agate_table) %}
  {% else %}
   # 通过create_csv_table macro集合上边的agate_table 数据生成创建表的sql
    {% set create_table_sql = create_csv_table(model, agate_table) %}
  {% endif %}
 
  {% set code = 'CREATE' if full_refresh_mode else 'INSERT' %}
  {% set rows_affected = (agate_table.rows | length) %}
  {% set sql = load_csv_rows(model, agate_table) %}
  # 执行sql
  {% call noop_statement('main', code ~ ' ' ~ rows_affected, code, rows_affected) %}
    {{ get_csv_sql(create_table_sql, sql) }};
  {% endcall %}
 
  {% set target_relation = this.incorporate(type='table') %}
 
  {% set should_revoke = should_revoke(old_relation, full_refresh_mode) %}
  {% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
 
  {% do persist_docs(target_relation, model) %}
 
  {% if full_refresh_mode or not exists_as_table %}
    {% do create_indexes(target_relation) %}
  {% endif %}
 
  {{ run_hooks(post_hooks, inside_transaction=True) }}
 
  -- `COMMIT` happens here
  {{ adapter.commit() }}
 
  {{ run_hooks(post_hooks, inside_transaction=False) }}
 
  {{ return({'relations': [target_relation]}) }}
 
{% endmaterialization %}
  • agate 加载处理
@contextmember
def load_agate_table(self) -> agate.Table:
    if not isinstance(self.model, SeedNode):
        raise LoadAgateTableNotSeedError(self.model.resource_type, node=self.model)
 
    # include package_path for seeds defined in packages
    package_path = (
        os.path.join(self.config.packages_install_path, self.model.package_name)
        if self.model.package_name != self.config.project_name
        else "."
    )
    path = os.path.join(self.config.project_root, package_path, self.model.original_file_path)
    if not os.path.exists(path):
        assert self.model.root_path
        path = os.path.join(self.model.root_path, self.model.original_file_path)
 
    column_types = self.model.config.column_types
    try:
        #  通过包装的agate_helper 加载csv 文件
        table = agate_helper.from_csv(path, text_columns=column_types)
    except ValueError as e:
        raise LoadAgateTableValueError(e, node=self.model)
    table.original_abspath = os.path.abspath(path)
    return table
  • manifest 信息效果

从下图也可以看出是一个seed 的物化,处理上也就比较符合物化的模式

说明

以上是关于seed 使用以及内部处理的简单说明,通过结合源码分析可以更好了解内部处理使用好seed 功能

参考资料

core/dbt/include/global_project/macros/materializations/seeds/seed.sql
core/dbt/clients/agate_helper.py
core/dbt/context/providers.py
https://agate.readthedocs.io/en/latest/cookbook/create.html

标签:model,self,简单,seed,agate,table,path,dbt
From: https://www.cnblogs.com/rongfengliang/p/18110144

相关文章

  • dremio 25.0 KVStore 升级简单说明
    dremio25.0开始对于数据源的存储支持加密了,所以升级上稍有不同,官方给出的操作流程如下参考处理//对于已经运行的,应该先stop,然后进行应用包的替换dremiostop//执行dremio-admin的upgradedremio-adminupgrade//启动dremiostart//停止drem......
  • dotnet C# 简单的追加文件夹到 ZipArchive 压缩文件的方法
    本文将告诉大家一个在ZipArchive里追加文件夹,以及添加过滤文件处理的压缩文件辅助方法实现的方法的代码如下///<summary>///追加文件夹到压缩文件里面///</summary>///<paramname="archive"></param>///<paramname="sourceDirectoryName"></p......
  • git的简单上传
    一、git上传项目1、gitinit2、gitadd.3、gitstatus4、链接地址gitremoteaddoriginhttps://github.com/xxx.git5、上传文件gitpull--rebaseoriginmaster,再用gitpushoriginmaster(仓库带Readme.md)gitpush-uoriginmaster(GitHub上空仓库,没有Readme.md)二......
  • amCharts简单柱形图
    代码案例<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=&qu......
  • 封装两个简单的Jquery组件
    Jquery给我们提供了很大的方便,我们把他看成是一个公共库,以致在这个公共库上延伸出了很多Jquery插件;在项目过程中,有些插件总是不那么令人满意;主要说两个项目用途:1、 遮罩层,跟一般的遮罩层不一样,我需要实现的是对某一个元素进行局部遮罩;2、 冒泡提示,网上有很多,我需要的只是一......
  • dotnet 简单方法在一个进程内同时跑起 WPF 和 ASP.NET Core 框架
    从设计架构上,无论是WPF还是ASP.NETCore框架,都是在dotnet运行时上层的应用,两个框架处于平级的结构。理论上讲,两个平级的框架只要不存在特殊的情况,都是能够相容存在的。本文将和大家介绍一个非常简单的方法,在一个进程内同时跑起WPF和ASP.NETCore框架在一个进程内同时跑......
  • PEcmd是一个命令行工具,用于执行各种操作系统和文件系统相关的任务。通常,它用于在Windo
    PECMD命令帮助-PECMD技术社区:www.pecmd.netPEcmd是一个命令行工具,用于执行各种操作系统和文件系统相关的任务。通常,它用于在Windows操作系统上执行各种文件和目录操作,例如文件复制、移动、删除等。PEcmd提供了一种简单而强大的方式来管理文件和目录,尤其是在自动化和批处......
  • 学习笔记447—本地部署 Llama3 – 8B/70B 大模型!最简单的方法: 支持CPU /GPU运行 【3种
    本地部署Llama3–8B/70B大模型!最简单的方法:支持CPU/GPU运行【3种方案】目前在开源大模型领域,Llama3无疑是最强的!这次Meta不仅免费公布了8B和70B两个性能强悍的大模型,400B也即将发布,这是可以和GPT-4对打的存在!今天我们就来介绍3各本地部署方法,简单易懂,非常适合新手!1.G......
  • 35.mybatis-plus简单整理
    这个可得好好介绍一下了但很多开源项目都还是采用的ssm中的mybatis但这个就是对mybatis的优化优化又优化既然语法优化简便了那自然就有其语法等规范参考:官网https://baomidou.com/pages/2976a3/#spring-boot首先肯定是依赖啦啦啦我的对应springboot版本为2.2.5.RELEASE......
  • Vue3 简单登录管理页面Demo
    目录前言项目基础配置新建项目引入组件项目配置Vue项目配置项目基本结构基础页面布局和路由搭建新增页面,简单跳转LoginViewMainViewrouterApp嵌套路由Test[1-4]Layout.vuerouter给个简单的跳转路由守护,重定向,动态路由,路由传值。这里不做展开描述简单登录页面:烂尾了总结前言这里......