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. 1月21日 Reference Data Type 数据类型,算法基础说明,二分搜索算法。(课程内容)

    Reference Datat Types 引用参考数据类型 -> 组合数据类型 Array, Hash和程序员自定义的复合资料类型 组合数据的修改: 组合数据类型的变量,不是直接存值,而是存一 ...

  2. android--------AndroidStudio 关闭 Install Run

    前面讲热修复的时候说到了一个 AndroidStudio关闭Instant Run的问题 ,今天来简单的写一下. Android Studio 工具中是有很多好东西的,要全部的知道的话,还是要下点功夫 ...

  3. Memory and Casinos CodeForces - 712E (概率,线段树)

    题目链接 题目大意:$n$个点, 每个点$i$有成功率$p_i$, 若成功走到$i+1$, 否则走到走到$i-1$, 多组询问, 求从$l$出发, 在$l$处不失败, 最后在$r$处胜利的概率 设$L ...

  4. Neo4j视频教程 Neo4j 图数据库视频教程

    课程名称 课程发布地址 地址: 腾讯课堂<Neo4j 图数据库视频教程> https://ke.qq.com/course/327374?tuin=442d3e14 作者 庞国明,< ...

  5. Leetcode 115

    Ø r a b b b i t Ø r a b b i t class Solution { public: int numDistinct(string s, string t) { ; ; int ...

  6. Leetcode 114

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  7. javascript里的偏函数——本质函数式编程+闭包,返回函数

    最终效果: var greet = function(greeting, name) { return greeting + ' ' + name; }; var sayHelloTo = _.par ...

  8. 深入理解angularjs $watch ,$apply 和 $digest --- 理解数据绑定过程

    转自:http://www.angularjs.cn/A0a6 Angular用户都想知道数据绑定是怎么实现的.你可能会看到各种各样的词汇:$watch,$apply,$digest,dirty-ch ...

  9. java并发编程:线程安全管理类--原子操作类--AtomicReferenceArray<E>

    1.类 AtomicReferenceArray<E> public class AtomicReferenceArray<E>extends Objectimplements ...

  10. BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...