我正在尝试创建我的第一个气流插件操作员,但是我无法使execute方法正常运行。
我设法找到了一个很好的示例,说明如何构建插件模块并将其导入到airflow中。
这是结构和代码:
airflow
├── plugins
├── generic_plugin
├── __init__.py
└── operators
├── __init__.py
└── generic_operator.py
airflow / plugins / generic_plugin / __ init __。py
from airflow.plugins_manager import AirflowPlugin
from generic_plugin.operators.generic_operator import GenericOperator
class GenericPlugin(AirflowPlugin):
name = "generic_plugin"
operators = [GenericOperator]
airflow / plugins / generic_plugin / operators / generic_operator.py
from airflow.models import BaseOperator
import json
class GenericOperator(BaseOperator):
def __init__(self,
data,
destination_path,
*args,
**kwargs):
super(GenericOperator, self).__init__(*args, **kwargs)
self.data = data
self.destination_path = destination_path
def execute(self, context):
self.dump_data(
data=self.data,
destination_path=self.destination_path)
def dump_data(self, data, destination_path):
with open(destination_path, 'w') as out_file:
json.dump(data, out_file)
在python控制台中运行此代码,所有内容均已正确导入,但execute方法未运行。
from airflow.operators.generic_plugin import GenericOperator
generic_op = GenericOperator(
task_id='gen_test',
data={'some_json_data': [
{'id': 1, 'value': 10}, {'id': 2, 'value': 35}]},
destination_path='./some_json_data.json')
但是,如果我手动运行dump_data方法,它将按预期工作:
generic_op.dump_data(
data=generic_op.data,
destination_path=generic_op.destination_path)
我在如何设置execute方法时是否缺少某些东西,或者execute方法不是应该像这样工作吗?