macro是SQL的片段,可以像模型中的函数一样调用。macro可以在模型之间重复使用SQL,以符合DRY(不要重复自己)的工程原理。
此外,共享包可以公开您可以在自己的dbt项目中使用的macro。
要使用macro,macro-paths请在dbt_project.yml文件中添加配置条目。macro文件必须使用.sql文件扩展名。

macro 的使用

  • 配置位置

    配置 dbt_project.yml

macro-paths: ['macros'] # look for macros in ./macros directory
  • 定义macro
{% macro group_by(n) %}

  GROUP BY
{% for i in range(1, n + 1) %}
{{ i }}
{% if not loop.last %} , {% endif %}
{% endfor %} {% endmacro %}
  • 调用
select
field_1,
field_2,
field_3,
field_4,
field_5,
count(*)
from my_table
{{ group_by(5) }}

合格的macro

上面的macro 是在自己的项目中,但是如何是三方应用的需要使用完全限定名 (加上项目名称)

  • 完全限定名
select
field_1,
field_2,
field_3,
field_4,
field_5,
count(*)
from my_table
{{ this_project.group_by(5) }}
  • 三方导入macro使用
packages:
- git: "https://github.com/fishtown-analytics/snowplow.git" select
{{ snowplow.get_utm_parameter('url_parameters', 'utm_medium') }}
from snowplow.event

参考资料

https://docs.getdbt.com/docs/macros

 
 
 
 

dbt macro 说明的更多相关文章

  1. dbt 包依赖简单测试

    dbt 包含一个自己的包管理,可以使用git 等工具,还是很方便的,可以方便的进行代码共享,实现复用 创建简单包 实际上就是一个简单的dbt 项目,参考项目 https://gitlab.com/da ...

  2. dbt 集成presto试用

    dbt 团队提供了presto 的adapter同时也是一个不错的的参考实现,可以学习 当前dbt presto 对于版本的要求是0.13.1 对于当前最新版本的还不支持,同时需要使用源码安装pip ...

  3. FreeMarker学习(宏<#macro>的使用)

    原文链接:https://my.oschina.net/weiweiblog/blog/506301?p=1 用户定义指令-使用@符合来调用  有两种不同的类型:Macro(宏)和transform( ...

  4. configure.ac:32: error: possibly undefined macro: AC_DEFINE

    在ubuntu 下编译snappy时,在检查依赖关系时,处理autoconf的包时,在相关依赖包都已经安装的情况下,报如下错误,死活不过. configure.ac:32: error: possib ...

  5. 【freemaker】之自定义指令<#macro>

    测试代码 @Test public void test07(){ try { root.put("name", "张三"); freemakerUtil.fpr ...

  6. C++ macro(宏)使用小结

    谈起C++中的宏,我们第一个想到的应该就是“#define”,它的基本语法长得像这样: #define macroname(para1, para2, para3, ... ,paran) macro ...

  7. 关于Depth Bounds Test (DBT)和在CE3的运用

    Depth Bounds Test (DBT) Depth Bounds Test(深度范围检测),是Nvdia GeForce 6系列以后显卡的特性(GPU Programming Guide Ge ...

  8. Macro and SQL

    If you’ve developed anything in the supply chain area, you’ve most probably come across InventDimJoi ...

  9. The difference between macro and function I/Ofunction comparision(from c and pointer )

    macro is typeless and execute faster than funtion ,becaus of the overhead of calling and returnning ...

随机推荐

  1. English trip -- VC(情景课) 6 A Time

    Words eleven  十一 twelve  十二 thirteen  十三 fourteen fifteen sixteen seventeen eighteen nineteen twenty ...

  2. codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)

    题意:求使pi(n)*q<=rub(n)*p成立的最大的n. 先收集所有的质数和回文数.质数好搜集.回文数奇回文就0-9的数字,然后在头尾添加一个数.在x前后加a,就是x*10+a+a*pow( ...

  3. hdu6394Tree lct

    树上弹飞绵羊,现场树分块没写出来= = 先预处理倍增,新建一个n+1节点,能弹到就建一条边,然后每操作2就cut,然后link,1的答案就是x到n+1的距离, //#pragma GCC optimi ...

  4. 线程的创建,pthread_create,pthread_self,pthread_once

    typedef unsigned long int pthread_t; //come from /usr/include/bits/pthreadtypes.h int pthread_create ...

  5. 013PHP文件处理——filemtime filectime fileatime date_default_timezone_set touch

    <?php /** * filemtime filectime fileatime date_default_timezone_set touch */ /*filemtime:获取文件的修改时 ...

  6. MySQL Server类型之MySQL客户端工具的下载、安装和使用(博主推荐)

    本博文的主要内容有 .MySQL Server 5.5系列的下载 .MySQL Server 5.5系列的安装 .MySQL Server 5.5系列的使用 .MySQL Server 5.5系列的卸 ...

  7. 跟我一起学习ASP.NET 4.5 MVC4.0(三)

    今天我们继续ASP.NET 4.5 MVC 4.0,前两天熟悉了MVC4.0在VS11和win8下的更新,以及MVC中的基础语法和几个关键字的使用.了解了这些就可以对MVC进一步认识,相信很多人都对M ...

  8. SHA1加密算法 java

    //下面四个import放在类名前面 包名后面 //import java.io.UnsupportedEncodingException; //import java.security.Messag ...

  9. 51nod1079

    模板题... 代码: #include <iostream> using namespace std; ],p[]; int main() { int n; while(cin>&g ...

  10. mysql 函数 事务

    mysql 中提供了许多内置函数 CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENGTH()返回 ...