- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Airflow 1.10.2。我正在尝试定义一个自定义模块,该模块将包含可在多个 dag 和运算符中使用的通用功能。
一个具体的例子可以是一个enum
。我想在自定义运算符中使用它(以修改其行为)。但我也想在 dag
定义中使用它,它可以用作参数。
这是我当前的层次结构
airflow_home
| - dags/
- __init__.py
- my_dag.py
| - plugins/
- operators/
- __init__.py
- my_operator.py
- common/
- __init__.py
- my_enum.py
假设我想定义一个枚举(在 my_enum.py
模块中):
class MyEnum(Enum):
OPTION_1 = 1
OPTION_2 = 2
它被导入到运算符(在 my_operator.py
中)为:
from common.my_enum import MyEnum
并以相同的方式进入 dag(在 my_dag.py
中):
from common.my_enum import MyEnum
奇怪的是(?),这对我有用。但是,我非常不确定这是否是做这种事情的正确方法。一位同事告诉我,他过去曾尝试这样做(可能是在旧版本的 Airflow 上)但它不起作用(当 Airflow 开始时“破损”)。因此,恐怕它在未来或在特定条件下可能不会(可能会停止)工作,因为它既不是运算符(operator),也不是传感器等。
我没有找到任何关于如何分离共享行为的指南。我发现 Airflow 导入系统非常复杂而且不是很直接。我理想的解决方案是将模块 common
移动到与 dags
和 operators
相同的级别。
另外,我不太确定如何从文档中解释这句话:插件文件夹中的 python 模块被导入,钩子(Hook)、运算符、传感器、宏、执行器和 Web View 被集成到 Airflow 的主要集合并可供使用。
这是否意味着我的方法是正确的,因为 plugins/
中的任何 python 模块都被导入了?
这是实现我的目标的好方法,还是有更好的解决方案?
谢谢你的建议
最佳答案
这样做有点古怪。
正确的方法是首先创建一个 hook
比operator
它将使用这个钩子(Hook)。对于如下所示的更简单的情况,您甚至不需要在运算符中调用 Hook 。
#1。 放置
<PROJECT NAME>/<PLUGINS_FOLDER>/<PLUGIN NAME>/__init__.py
<PROJECT NAME>/<PLUGINS_FOLDER>/<PLUGIN NAME>/<some_new>_hook.py
<PROJECT NAME>/<PLUGINS_FOLDER>/<PLUGIN NAME>/<some_new>_operator.py
对于看起来像这样的真实案例场景:
CRMProject/crm_plugin/__init__.py
CRMProject/crm_plugin/crm_hook.py
CRMProject/crm_plugin/customer_operator.py
#2。 代码
CRMProject/crm_plugin/__init__.py
的示例代码:
# CRMProject/crm_plugin/__init__.py
from airflow.plugins_manager import AirflowPlugin
from crm_plugin.crm_hook import CrmHook
from crm_plugin.customer_operator import CreateCustomerOperator, DeleteCustomerOperator, UpdateCustomerOperator
class AirflowCrmPlugin(AirflowPlugin):
name = "crm_plugin" # does not need to match the package name
operators = [CreateCustomerOperator, DeleteCustomerOperator, UpdateCustomerOperator]
sensors = []
hooks = [CrmHook]
executors = []
macros = []
admin_views = []
flask_blueprints = []
menu_links = []
appbuilder_views = []
appbuilder_menu_items = []
global_operator_extra_links = []
operator_extra_links = []
钩子(Hook)类的示例代码 - CRMProject/crm_plugin/crm_hook.py
.永远不要直接从 system\API 调用它。为此使用 Operator(见下文)。
from airflow.hooks.base_hook import BaseHook
from airflow.exceptions import AirflowException
from crm_sdk import crm_api # import external libraries to interact with target system
class CrmHook(BaseHook):
"""
Hook to interact with the ACME CRM System.
"""
def __init__(self, ...):
# your code goes here
def insert_object(self, ...):
"""
Insert an object into the CRM system
"""
# your code goes here
def update_object(self, ...):
"""
Update an object into the CRM system
"""
# your code goes here
def delete_object(self, ...):
"""
Delete an object into the CRM system
"""
# your code goes here
def extract_object(self, ...):
"""
Extract an object into the CRM system
"""
# your code goes here
您将在 DAG 中使用的运算符示例代码 (CRMProject/crm_plugin/customer_operator.py
)。运算符要求您实现一个执行方法。这是 Airflow 操作符的入口点,它会在 DAG 中的任务执行时调用。apply_defaults
装饰器包装 __init__
将在 DAG 脚本中设置的 DAG 默认值应用到运行时运算符的任务实例的类的方法。
我们还可以设置两个重要的类属性。这些是 templated_fields
和 template_ext
.这两个属性是可迭代对象,应包含字段的字符串值和/或文件扩展名,这些字段和/或文件扩展名将允许使用 Airflow 中的 jinja 模板支持进行模板化。
from airflow.exceptions import AirflowException
from airflow.operators import BaseOperator
from airflow.utils.decorators import apply_defauls
from crm_plugin.crm_hook import CrmHook
class CreateCustomerOperator(BaseOperator):
"""
This operator creates a new customer in the ACME CRM System.
"""
template_fields = ['first_contact_date', 'bulk_file']
template_ext = ['.csv']
@apply_defaults
def __init__(self, first_contact_date, bulk_file, ...):
# your code goes here
def _customer_exist(self, ...):
"""
Helper method to check if a customer exist. Raises an exception if it does.
"""
# your code goes here
def execute(self, context):
"""
Create a new customer in the CRM system.
"""
# your code goes here
为了简化执行方法,您可以根据需要在类中创建任意数量的方法。优秀类(class)设计的相同原则在这里仍然很重要。
#3。 部署和使用您的插件
一旦您完成了插件的工作,剩下要做的就是复制您的 <PLUGIN NAME>
包文件夹到 Airflow 插件文件夹。 Airflow 将选择插件并将其提供给您的 DAG。如果我们将简单的 CRM 插件复制到我们的 plugins_folder
文件夹结构如下所示。
<plugins_folder>/crm_plugin/__init__.py
<plugins_folder>/crm_plugin/crm_hook.py
<plugins_folder>/crm_plugin/customer_operator.py
为了使用您的新插件,您只需使用以下语句导入您的 Operators 和 Hooks。
from airflow.hooks.crm_plugin import CrmHook
from airflow.operators.crm_plugin import CreateCustomerOperator, DeleteCustomerOperator, UpdateCustomerOperator
关于python - Apache Airflow——在多个地方使用的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221053/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!