在Apache Airflow 1.10中,可以使用BashOperator
任务来运行Shell命令,并在运行中访问SQL文件。下面是一个使用BashOperator
和psql
命令来访问SQL文件的示例代码:
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime
default_args = {
'start_date': datetime(2022, 1, 1)
}
dag = DAG('access_sql_file', default_args=default_args, schedule_interval='@once')
access_sql_task = BashOperator(
task_id='access_sql_task',
bash_command='psql -U your_username -d your_database -f /path/to/your/sql/file.sql',
dag=dag
)
access_sql_task
在上述代码中,BashOperator
的bash_command
参数使用了psql
命令来访问SQL文件。你需要将your_username
替换为你的数据库用户名,your_database
替换为你的数据库名称,/path/to/your/sql/file.sql
替换为你的SQL文件的路径。
注意,你需要确保在Airflow的环境中安装了psql
命令,并且已经配置了正确的数据库连接信息。
请确保在Airflow的安装目录下运行此代码,或者将SQL文件的路径修改为Airflow可以访问的路径。
此外,你还可以根据需要添加其他任务操作符来构建更复杂的工作流程。