使用 Python 中的 NamedTemporaryFile
方法创建新的临时文件,并将字符串数据写入该文件。然后,从该文件中读取数据并将其传递给任务。例如:
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
import io
import tempfile
def my_task():
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
# write data to temp file
f.write('my data')
f.close()
with open(f.name, 'r') as read_file:
data = read_file.read()
print(data)
dag = DAG('my_dag', description='example DAG', schedule_interval='@once')
task = PythonOperator(
task_id='my_task',
dag=dag,
python_callable=my_task,
)