如何获取execution_date作为纪元时间?

KoK*_*oKu 4 python jinja2 airflow

在常规的 python 代码中我可以这样做:

import time
int(time.time())
Run Code Online (Sandbox Code Playgroud)

这给了我纪元般的时间。我希望能够使用气流宏来做到这一点:execution_date

这是我尝试过的:

"{{  strptime(execution_date.strftime('%Y-%m-%d %H:%M:%S'), '%d.%m.%Y %H:%M:%S') }}"
Run Code Online (Sandbox Code Playgroud)

但这给出了:

jinja2.exceptions.UndefinedError:“strptime”未定义

我正在运行 Airflow 1.9 和 Python 2.7

Bas*_*lak 5

从 Airflow 1.10 开始,Airflow 使用 Pendulum 来表示日期时间,它具有 属性timestamp()int_timestamp并且float_timestamp返回纪元。

所以,你可以这样做:

{{ execution_date.int_timestamp }}
Run Code Online (Sandbox Code Playgroud)

文档: https: //pendulum.eustace.io/docs/#attributes-and-properties

其他选项有:

{{ execution_date.strftime('%s') }} # Also Python 2
{{ execution_date.timestamp() }}    # Python >=3.3
Run Code Online (Sandbox Code Playgroud)