| |
| |
| |
| |
| |
| <!DOCTYPE html> |
| <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> |
| <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> |
| <head> |
| <meta charset="utf-8"> |
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <title>Macros reference — Airflow Documentation</title> |
| |
| |
| |
| |
| |
| |
| |
| |
| <script type="text/javascript" src="_static/js/modernizr.min.js"></script> |
| |
| |
| <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> |
| <script type="text/javascript" src="_static/jquery.js"></script> |
| <script type="text/javascript" src="_static/underscore.js"></script> |
| <script type="text/javascript" src="_static/doctools.js"></script> |
| <script type="text/javascript" src="_static/language_data.js"></script> |
| |
| <script type="text/javascript" src="_static/js/theme.js"></script> |
| |
| |
| |
| |
| <link rel="stylesheet" href="_static/css/theme.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
| <link rel="index" title="Index" href="genindex.html" /> |
| <link rel="search" title="Search" href="search.html" /> |
| <link rel="next" title="Python API Reference" href="_api/index.html" /> |
| <link rel="prev" title="FAQ" href="faq.html" /> |
| |
| <script> |
| document.addEventListener('DOMContentLoaded', function() { |
| var el = document.getElementById('changelog'); |
| if (el !== null ) { |
| // [AIRFLOW-...] |
| el.innerHTML = el.innerHTML.replace( |
| /\[(AIRFLOW-[\d]+)\]/g, |
| `<a href="https://issues.apache.org/jira/browse/$1">[$1]</a>` |
| ); |
| // (#...) |
| el.innerHTML = el.innerHTML.replace( |
| /\(#([\d]+)\)/g, |
| `<a href="https://github.com/apache/airflow/pull/$1">(#$1)</a>` |
| ); |
| }; |
| }) |
| </script> |
| <script type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-140539454-1']); |
| _gaq.push(['_trackPageview']); |
| </script> |
| <style> |
| .example-header { |
| position: relative; |
| background: #9AAA7A; |
| padding: 8px 16px; |
| margin-bottom: 0; |
| } |
| .example-header--with-button { |
| padding-right: 166px; |
| } |
| .example-header:after{ |
| content: ''; |
| display: table; |
| clear: both; |
| } |
| .example-title { |
| display:block; |
| padding: 4px; |
| margin-right: 16px; |
| color: white; |
| overflow-x: auto; |
| } |
| .example-header-button { |
| top: 8px; |
| right: 16px; |
| position: absolute; |
| } |
| .example-header + .highlight-python { |
| margin-top: 0 !important; |
| } |
| .viewcode-button { |
| display: inline-block; |
| padding: 8px 16px; |
| border: 0; |
| margin: 0; |
| outline: 0; |
| border-radius: 2px; |
| -webkit-box-shadow: 0 3px 5px 0 rgba(0,0,0,.3); |
| box-shadow: 0 3px 6px 0 rgba(0,0,0,.3); |
| color: #404040; |
| background-color: #e7e7e7; |
| cursor: pointer; |
| font-size: 16px; |
| font-weight: 500; |
| line-height: 1; |
| text-decoration: none; |
| text-overflow: ellipsis; |
| overflow: hidden; |
| text-transform: uppercase; |
| -webkit-transition: background-color .2s; |
| transition: background-color .2s; |
| vertical-align: middle; |
| white-space: nowrap; |
| } |
| .viewcode-button:visited { |
| color: #404040; |
| } |
| .viewcode-button:hover, .viewcode-button:focus { |
| color: #404040; |
| background-color: #d6d6d6; |
| } |
| </style> |
| |
| </head> |
| |
| <body class="wy-body-for-nav"> |
| |
| |
| <div class="wy-grid-for-nav"> |
| |
| <nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| <div class="wy-side-scroll"> |
| <div class="wy-side-nav-search" > |
| |
| |
| |
| <a href="index.html" class="icon icon-home"> Airflow |
| |
| |
| |
| </a> |
| |
| |
| |
| |
| <div class="version"> |
| 1.10.5 |
| </div> |
| |
| |
| |
| |
| <div role="search"> |
| <form id="rtd-search-form" class="wy-form" action="search.html" method="get"> |
| <input type="text" name="q" placeholder="Search docs" /> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </form> |
| </div> |
| |
| |
| </div> |
| |
| <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> |
| |
| |
| |
| |
| |
| |
| <ul class="current"> |
| <li class="toctree-l1"><a class="reference internal" href="project.html">Project</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="start.html">Quick Start</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="howto/index.html">How-to Guides</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="ui.html">UI / Screenshots</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="concepts.html">Concepts</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="profiling.html">Data Profiling</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="cli.html">Command Line Interface Reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="scheduler.html">Scheduling & Triggers</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="plugins.html">Plugins</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="security.html">Security</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="timezone.html">Time zones</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="api.html">REST API Reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="integration.html">Integration</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="metrics.html">Metrics</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="kubernetes.html">Kubernetes</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="lineage.html">Lineage</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li> |
| <li class="toctree-l1 current"><a class="current reference internal" href="#">Macros reference</a><ul> |
| <li class="toctree-l2"><a class="reference internal" href="#default-variables">Default Variables</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="#macros">Macros</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.datetime_diff_for_humans">datetime_diff_for_humans</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.ds_add">ds_add</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.ds_format">ds_format</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.random">random</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.hive.closest_ds_partition">closest_ds_partition</a></li> |
| </ul> |
| <ul> |
| <li class="toctree-l2"><a class="reference internal" href="#airflow.macros.hive.max_partition">max_partition</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="_api/index.html">Python API Reference</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="privacy_notice.html">Privacy Notice</a></li> |
| </ul> |
| |
| |
| |
| </div> |
| </div> |
| </nav> |
| |
| <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| |
| |
| <nav class="wy-nav-top" aria-label="top navigation"> |
| |
| <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| <a href="index.html">Airflow</a> |
| |
| </nav> |
| |
| |
| <div class="wy-nav-content"> |
| |
| <div class="rst-content"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <div role="navigation" aria-label="breadcrumbs navigation"> |
| |
| <ul class="wy-breadcrumbs"> |
| |
| <li><a href="index.html">Docs</a> »</li> |
| |
| <li>Macros reference</li> |
| |
| |
| <li class="wy-breadcrumbs-aside"> |
| |
| |
| <a href="_sources/macros.rst.txt" rel="nofollow"> View page source</a> |
| |
| |
| </li> |
| |
| </ul> |
| |
| |
| <hr/> |
| </div> |
| <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| <div itemprop="articleBody"> |
| |
| <blockquote> |
| <div></div></blockquote> |
| <div class="section" id="macros-reference"> |
| <h1>Macros reference<a class="headerlink" href="#macros-reference" title="Permalink to this headline">¶</a></h1> |
| <p>Variables and macros can be used in templates (see the <a class="reference internal" href="concepts.html#id1"><span class="std std-ref">Jinja Templating</span></a> section)</p> |
| <p>The following come for free out of the box with Airflow. |
| Additional custom macros can be added globally through <a class="reference external" href="https://docs.sqlalchemy.org/en/13/orm/extensions/index.html#plugins" title="(in SQLAlchemy v1.3)"><span>ORM Extensions</span></a>, or at a DAG level through the <code class="docutils literal notranslate"><span class="pre">DAG.user_defined_macros</span></code> argument.</p> |
| <div class="section" id="default-variables"> |
| <h2>Default Variables<a class="headerlink" href="#default-variables" title="Permalink to this headline">¶</a></h2> |
| <p>The Airflow engine passes a few variables by default that are accessible |
| in all templates</p> |
| <table class="docutils align-default"> |
| <colgroup> |
| <col style="width: 31%" /> |
| <col style="width: 69%" /> |
| </colgroup> |
| <thead> |
| <tr class="row-odd"><th class="head"><p>Variable</p></th> |
| <th class="head"><p>Description</p></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the execution date as <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the execution date as <code class="docutils literal notranslate"><span class="pre">YYYYMMDD</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_ds</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the previous execution date as <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code> |
| if <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">2018-01-08</span></code> and <code class="docutils literal notranslate"><span class="pre">schedule_interval</span></code> is <code class="docutils literal notranslate"><span class="pre">@weekly</span></code>, |
| <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_ds</span> <span class="pre">}}</span></code> will be <code class="docutils literal notranslate"><span class="pre">2018-01-01</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_ds_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the previous execution date as <code class="docutils literal notranslate"><span class="pre">YYYYMMDD</span></code> if exists, else <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">next_ds</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the next execution date as <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code> |
| if <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">2018-01-01</span></code> and <code class="docutils literal notranslate"><span class="pre">schedule_interval</span></code> is <code class="docutils literal notranslate"><span class="pre">@weekly</span></code>, |
| <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">next_ds</span> <span class="pre">}}</span></code> will be <code class="docutils literal notranslate"><span class="pre">2018-01-08</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">next_ds_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the next execution date as <code class="docutils literal notranslate"><span class="pre">YYYYMMDD</span></code> if exists, else <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">yesterday_ds</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the day before the execution date as <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">yesterday_ds_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the day before the execution date as <code class="docutils literal notranslate"><span class="pre">YYYYMMDD</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">tomorrow_ds</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the day after the execution date as <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">tomorrow_ds_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the day after the execution date as <code class="docutils literal notranslate"><span class="pre">YYYYMMDD</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ts</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">execution_date.isoformat()</span></code>. Example: <code class="docutils literal notranslate"><span class="pre">2018-01-01T00:00:00+00:00</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ts_nodash</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">ts</span></code> without <code class="docutils literal notranslate"><span class="pre">-</span></code>, <code class="docutils literal notranslate"><span class="pre">:</span></code> and TimeZone info. Example: <code class="docutils literal notranslate"><span class="pre">20180101T000000</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ts_nodash_with_tz</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">ts</span></code> without <code class="docutils literal notranslate"><span class="pre">-</span></code> and <code class="docutils literal notranslate"><span class="pre">:</span></code>. Example: <code class="docutils literal notranslate"><span class="pre">20180101T000000+0000</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">execution_date</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the execution_date (<a class="reference external" href="https://pendulum.eustace.io/docs/1.x/#introduction">pendulum.Pendulum</a>)</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_execution_date</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the previous execution date (if available) (<a class="reference external" href="https://pendulum.eustace.io/docs/1.x/#introduction">pendulum.Pendulum</a>)</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_execution_date_success</span> <span class="pre">}}</span></code></p></td> |
| <td><p>execution date from prior succesful dag run (if available) (<a class="reference external" href="https://pendulum.eustace.io/docs/1.x/#introduction">pendulum.Pendulum</a>)</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">prev_start_date_success</span> <span class="pre">}}</span></code></p></td> |
| <td><p>start date from prior successful dag run (if available) (<a class="reference external" href="https://pendulum.eustace.io/docs/1.x/#introduction">pendulum.Pendulum</a>)</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">next_execution_date</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the next execution date (<a class="reference external" href="https://pendulum.eustace.io/docs/1.x/#introduction">pendulum.Pendulum</a>)</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">dag</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the DAG object</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the Task object</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">macros</span> <span class="pre">}}</span></code></p></td> |
| <td><p>a reference to the macros package, described below</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task_instance</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the task_instance object</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">end_date</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds</span> <span class="pre">}}</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">latest_date</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ds</span> <span class="pre">}}</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ti</span> <span class="pre">}}</span></code></p></td> |
| <td><p>same as <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task_instance</span> <span class="pre">}}</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">params</span> <span class="pre">}}</span></code></p></td> |
| <td><p>a reference to the user-defined params dictionary which can be overridden by |
| the dictionary passed through <code class="docutils literal notranslate"><span class="pre">trigger_dag</span> <span class="pre">-c</span></code> if you enabled |
| <code class="docutils literal notranslate"><span class="pre">dag_run_conf_overrides_params`</span> <span class="pre">in</span> <span class="pre">``airflow.cfg</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">var.value.my_var</span> <span class="pre">}}</span></code></p></td> |
| <td><p>global defined variables represented as a dictionary</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">var.json.my_var.path</span> <span class="pre">}}</span></code></p></td> |
| <td><p>global defined variables represented as a dictionary |
| with deserialized JSON object, append the path to the |
| key within the JSON object</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task_instance_key_str</span> <span class="pre">}}</span></code></p></td> |
| <td><p>a unique, human-readable key to the task instance |
| formatted <code class="docutils literal notranslate"><span class="pre">{dag_id}_{task_id}_{ds}</span></code></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">conf</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the full configuration object located at |
| <code class="docutils literal notranslate"><span class="pre">airflow.configuration.conf</span></code> which |
| represents the content of your |
| <code class="docutils literal notranslate"><span class="pre">airflow.cfg</span></code></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">run_id</span> <span class="pre">}}</span></code></p></td> |
| <td><p>the <code class="docutils literal notranslate"><span class="pre">run_id</span></code> of the current DAG run</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">dag_run</span> <span class="pre">}}</span></code></p></td> |
| <td><p>a reference to the DagRun object</p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">test_mode</span> <span class="pre">}}</span></code></p></td> |
| <td><p>whether the task instance was called using |
| the CLI’s test subcommand</p></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Note that you can access the object’s attributes and methods with simple |
| dot notation. Here are some examples of what is possible: |
| <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task.owner</span> <span class="pre">}}</span></code>, <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">task.task_id</span> <span class="pre">}}</span></code>, <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">ti.hostname</span> <span class="pre">}}</span></code>, … |
| Refer to the models documentation for more information on the objects’ |
| attributes and methods.</p> |
| <p>The <code class="docutils literal notranslate"><span class="pre">var</span></code> template variable allows you to access variables defined in Airflow’s |
| UI. You can access them as either plain-text or JSON. If you use JSON, you are |
| also able to walk nested structures, such as dictionaries like: |
| <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">var.json.my_dict_var.key1</span> <span class="pre">}}</span></code></p> |
| </div> |
| <div class="section" id="macros"> |
| <h2>Macros<a class="headerlink" href="#macros" title="Permalink to this headline">¶</a></h2> |
| <p>Macros are a way to expose objects to your templates and live under the |
| <code class="docutils literal notranslate"><span class="pre">macros</span></code> namespace in your templates.</p> |
| <p>A few commonly used libraries and methods are made available.</p> |
| <table class="docutils align-default"> |
| <colgroup> |
| <col style="width: 42%" /> |
| <col style="width: 58%" /> |
| </colgroup> |
| <thead> |
| <tr class="row-odd"><th class="head"><p>Variable</p></th> |
| <th class="head"><p>Description</p></th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">macros.datetime</span></code></p></td> |
| <td><p>The standard lib’s <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">macros.timedelta</span></code></p></td> |
| <td><p>The standard lib’s <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.timedelta" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timedelta</span></code></a></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">macros.dateutil</span></code></p></td> |
| <td><p>A reference to the <code class="docutils literal notranslate"><span class="pre">dateutil</span></code> package</p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">macros.time</span></code></p></td> |
| <td><p>The standard lib’s <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.time" title="(in Python v3.7)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.time</span></code></a></p></td> |
| </tr> |
| <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">macros.uuid</span></code></p></td> |
| <td><p>The standard lib’s <a class="reference external" href="https://docs.python.org/3/library/uuid.html#module-uuid" title="(in Python v3.7)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uuid</span></code></a></p></td> |
| </tr> |
| <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">macros.random</span></code></p></td> |
| <td><p>The standard lib’s <a class="reference external" href="https://docs.python.org/3/library/random.html#module-random" title="(in Python v3.7)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a></p></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Some airflow specific macros are also defined:</p> |
| <span class="target" id="module-airflow.macros"></span><dl class="function"> |
| <dt id="airflow.macros.datetime_diff_for_humans"> |
| <code class="sig-prename descclassname">airflow.macros.</code><code class="sig-name descname">datetime_diff_for_humans</code><span class="sig-paren">(</span><em class="sig-param">dt</em>, <em class="sig-param">since=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/macros.html#datetime_diff_for_humans"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.macros.datetime_diff_for_humans" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Return a human-readable/approximate difference between two datetimes, or |
| one and now.</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters</dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>dt</strong> (<em>datetime</em>) – The datetime to display the diff for</p></li> |
| <li><p><strong>since</strong> (<a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.7)"><em>None</em></a><em> or </em><em>datetime</em>) – When to display the date from. If <code class="docutils literal notranslate"><span class="pre">None</span></code> then the diff is |
| between <code class="docutils literal notranslate"><span class="pre">dt</span></code> and now.</p></li> |
| </ul> |
| </dd> |
| <dt class="field-even">Return type</dt> |
| <dd class="field-even"><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)">str</a></p> |
| </dd> |
| </dl> |
| </dd></dl> |
| |
| <dl class="function"> |
| <dt id="airflow.macros.ds_add"> |
| <code class="sig-prename descclassname">airflow.macros.</code><code class="sig-name descname">ds_add</code><span class="sig-paren">(</span><em class="sig-param">ds</em>, <em class="sig-param">days</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/macros.html#ds_add"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.macros.ds_add" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Add or subtract days from a YYYY-MM-DD</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters</dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>ds</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – anchor date in <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code> format to add to</p></li> |
| <li><p><strong>days</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – number of days to add to the ds, you can use negative values</p></li> |
| </ul> |
| </dd> |
| </dl> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ds_add</span><span class="p">(</span><span class="s1">'2015-01-01'</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> |
| <span class="go">'2015-01-06'</span> |
| <span class="gp">>>> </span><span class="n">ds_add</span><span class="p">(</span><span class="s1">'2015-01-06'</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">)</span> |
| <span class="go">'2015-01-01'</span> |
| </pre></div> |
| </div> |
| </dd></dl> |
| |
| <dl class="function"> |
| <dt id="airflow.macros.ds_format"> |
| <code class="sig-prename descclassname">airflow.macros.</code><code class="sig-name descname">ds_format</code><span class="sig-paren">(</span><em class="sig-param">ds</em>, <em class="sig-param">input_format</em>, <em class="sig-param">output_format</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/macros.html#ds_format"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.macros.ds_format" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Takes an input string and outputs another string |
| as specified in the output format</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters</dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>ds</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – input string which contains a date</p></li> |
| <li><p><strong>input_format</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – input string format. E.g. %Y-%m-%d</p></li> |
| <li><p><strong>output_format</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – output string format E.g. %Y-%m-%d</p></li> |
| </ul> |
| </dd> |
| </dl> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">ds_format</span><span class="p">(</span><span class="s1">'2015-01-01'</span><span class="p">,</span> <span class="s2">"%Y-%m-</span><span class="si">%d</span><span class="s2">"</span><span class="p">,</span> <span class="s2">"%m-</span><span class="si">%d</span><span class="s2">-%y"</span><span class="p">)</span> |
| <span class="go">'01-01-15'</span> |
| <span class="gp">>>> </span><span class="n">ds_format</span><span class="p">(</span><span class="s1">'1/5/2015'</span><span class="p">,</span> <span class="s2">"%m/</span><span class="si">%d</span><span class="s2">/%Y"</span><span class="p">,</span> <span class="s2">"%Y-%m-</span><span class="si">%d</span><span class="s2">"</span><span class="p">)</span> |
| <span class="go">'2015-01-05'</span> |
| </pre></div> |
| </div> |
| </dd></dl> |
| |
| <dl class="function"> |
| <dt id="airflow.macros.random"> |
| <code class="sig-prename descclassname">airflow.macros.</code><code class="sig-name descname">random</code><span class="sig-paren">(</span><span class="sig-paren">)</span> → x in the interval [0, 1).<a class="headerlink" href="#airflow.macros.random" title="Permalink to this definition">¶</a></dt> |
| <dd></dd></dl> |
| |
| <dl class="function"> |
| <dt id="airflow.macros.hive.closest_ds_partition"> |
| <code class="sig-prename descclassname">airflow.macros.hive.</code><code class="sig-name descname">closest_ds_partition</code><span class="sig-paren">(</span><em class="sig-param">table</em>, <em class="sig-param">ds</em>, <em class="sig-param">before=True</em>, <em class="sig-param">schema='default'</em>, <em class="sig-param">metastore_conn_id='metastore_default'</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/macros/hive.html#closest_ds_partition"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.macros.hive.closest_ds_partition" title="Permalink to this definition">¶</a></dt> |
| <dd><p>This function finds the date in a list closest to the target date. |
| An optional parameter can be given to get the closest before or after.</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters</dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>table</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – A hive table name</p></li> |
| <li><p><strong>ds</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.7)"><em>list</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(in Python v3.7)"><em>datetime.date</em></a><em>]</em>) – A datestamp <code class="docutils literal notranslate"><span class="pre">%Y-%m-%d</span></code> e.g. <code class="docutils literal notranslate"><span class="pre">yyyy-mm-dd</span></code></p></li> |
| <li><p><strong>before</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.7)"><em>bool</em></a><em> or </em><a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.7)"><em>None</em></a>) – closest before (True), after (False) or either side of ds</p></li> |
| </ul> |
| </dd> |
| <dt class="field-even">Returns</dt> |
| <dd class="field-even"><p>The closest date</p> |
| </dd> |
| <dt class="field-odd">Return type</dt> |
| <dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)">str</a> or <a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.7)">None</a></p> |
| </dd> |
| </dl> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">tbl</span> <span class="o">=</span> <span class="s1">'airflow.static_babynames_partitioned'</span> |
| <span class="gp">>>> </span><span class="n">closest_ds_partition</span><span class="p">(</span><span class="n">tbl</span><span class="p">,</span> <span class="s1">'2015-01-02'</span><span class="p">)</span> |
| <span class="go">'2015-01-01'</span> |
| </pre></div> |
| </div> |
| </dd></dl> |
| |
| <dl class="function"> |
| <dt id="airflow.macros.hive.max_partition"> |
| <code class="sig-prename descclassname">airflow.macros.hive.</code><code class="sig-name descname">max_partition</code><span class="sig-paren">(</span><em class="sig-param">table</em>, <em class="sig-param">schema='default'</em>, <em class="sig-param">field=None</em>, <em class="sig-param">filter_map=None</em>, <em class="sig-param">metastore_conn_id='metastore_default'</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/airflow/macros/hive.html#max_partition"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#airflow.macros.hive.max_partition" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Gets the max partition for a table.</p> |
| <dl class="field-list simple"> |
| <dt class="field-odd">Parameters</dt> |
| <dd class="field-odd"><ul class="simple"> |
| <li><p><strong>schema</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – The hive schema the table lives in</p></li> |
| <li><p><strong>table</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – The hive table you are interested in, supports the dot |
| notation as in “my_database.my_table”, if a dot is found, |
| the schema param is disregarded</p></li> |
| <li><p><strong>metastore_conn_id</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – The hive connection you are interested in. |
| If your default is set you don’t need to use this parameter.</p></li> |
| <li><p><strong>filter_map</strong> (<em>map</em>) – partition_key:partition_value map used for partition filtering, |
| e.g. {‘key1’: ‘value1’, ‘key2’: ‘value2’}. |
| Only partitions matching all partition_key:partition_value |
| pairs will be considered as candidates of max partition.</p></li> |
| <li><p><strong>field</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – the field to get the max value from. If there’s only |
| one partition field, this will be inferred</p></li> |
| </ul> |
| </dd> |
| </dl> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">max_partition</span><span class="p">(</span><span class="s1">'airflow.static_babynames_partitioned'</span><span class="p">)</span> |
| <span class="go">'2015-01-01'</span> |
| </pre></div> |
| </div> |
| </dd></dl> |
| |
| </div> |
| </div> |
| |
| |
| </div> |
| |
| </div> |
| |
| |
| <footer> |
| |
| <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> |
| |
| <a href="_api/index.html" class="btn btn-neutral float-right" title="Python API Reference" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> |
| |
| |
| <a href="faq.html" class="btn btn-neutral float-left" title="FAQ" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> |
| |
| </div> |
| |
| |
| <hr/> |
| |
| <div role="contentinfo"> |
| <p> |
| |
| </p> |
| </div> |
| Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| <div class="footer">This page uses <a href="https://analytics.google.com/"> |
| Google Analytics</a> to collect statistics. You can disable it by blocking |
| the JavaScript coming from www.google-analytics.com. Check our |
| <a href="privacy_notice.html">Privacy Policy</a> |
| for more details. |
| <script type="text/javascript"> |
| (function() { |
| var ga = document.createElement('script'); |
| ga.src = ('https:' == document.location.protocol ? |
| 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| ga.setAttribute('async', 'true'); |
| var nodes = document.documentElement.childNodes; |
| var i = -1; |
| var node; |
| do { |
| i++; |
| node = nodes[i] |
| } while(node.nodeType !== Node.ELEMENT_NODE); |
| node.appendChild(ga); |
| })(); |
| </script> |
| </div> |
| |
| |
| </footer> |
| |
| </div> |
| </div> |
| |
| </section> |
| |
| </div> |
| |
| |
| |
| <script type="text/javascript"> |
| jQuery(function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }); |
| </script> |
| |
| |
| |
| |
| |
| |
| </body> |
| </html> |