dbt modules 宏简单说明

dbt modules 宏提供了让dbt 访问python 包的能力,处理上基于了jinja 的上下问处理,但是目前官方没有完整暴露此方法
核心还是因为安全问题,只提供了一些时间、正则、迭代处理的,实际使用上基于了python 模块的动态加载(getattr)

modules 宏的使用参考 

{% set now = modules.datetime.datetime.now() %}
{% set three_days_ago_iso = (now - modules.datetime.timedelta(3)).isoformat() %}

modules 上下文

  • 定义
def get_context_modules() -> Dict[str, Dict[str, Any]]:  
    return {                                             
        "pytz": get_pytz_module_context(),               
        "datetime": get_datetime_module_context(),       
        "re": get_re_module_context(),                   
        "itertools": get_itertools_module_context(),     
    }                                                    
  • 加载
def get_datetime_module_context() -> Dict[str, Any]:                       
    context_exports = ["date", "datetime", "time", "timedelta", "tzinfo"]  
 
    return {name: getattr(datetime, name) for name in context_exports}                                                                                
说明

dbt 有不少需要时间以及正则处理的就使用了此模块提供的能力

参考资料

core/dbt/context/base.py
https://docs.getdbt.com/reference/dbt-jinja-functions/modules

posted on   荣锋亮  阅读(67)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-08-08 jprofiler Alpine 容器集成
2022-08-08 .net 调用spring boot rest api 的100-Continue问题
2019-08-08 apache pinot-docker-compose 运行试用
2019-08-08 Thanos prometheus 集群以及多租户解决方案docker-compose 试用(一)
2018-08-08 opencrud 中文参考翻译(完成部分)
2018-08-08 strapi 开源api && 内容管理平台试用
2018-08-08 使用Spec Markdown 编写手册文档

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示