How-to Guide for Mysql using SQLExecuteQueryOperator¶
Use the SQLExecuteQueryOperator
to execute
SQL commands in a MySql database.
Warning
Previously, MySqlOperator was used to perform this kind of operation. But at the moment MySqlOperator is deprecated and will be removed in future versions of the provider. Please consider to switch to SQLExecuteQueryOperator as soon as possible.
Using the Operator¶
Use the conn_id
argument to connect to your MySql instance where
the connection metadata is structured as follows:
Parameter |
Input |
---|---|
Host: string |
MySql hostname |
Schema: string |
Set schema to execute Sql operations on by default |
Login: string |
MySql user |
Password: string |
MySql user password |
Port: int |
MySql port |
An example usage of the SQLExecuteQueryOperator is as follows:
drop_table_mysql_task = SQLExecuteQueryOperator(
task_id="drop_table_mysql", sql=r"""DROP TABLE table_name;""", dag=dag
)
You can also use an external file to execute the SQL commands. Script folder must be at the same level as DAG.py file.
mysql_task = SQLExecuteQueryOperator(
task_id="drop_table_mysql_external_file",
sql="/scripts/drop_table.sql",
dag=dag,
)
Note
Parameters that can be passed onto the operator will be given priority over the parameters already given
in the Airflow connection metadata (such as schema
, login
, password
and so forth).