This document describes how to configure the DMSSqlOperator.
Overview
The DMSSqlOperator executes an SQL statement on a DMS-managed database instance and retrieves the results.
Parameters
The parameters instance, database, and sql support Jinja templates.
Parameter | Type | Required | Description |
instance | string | Yes | The DBLink connection name for the DMS-managed database instance. |
database | string | Yes | The database name. |
sql | string | Yes | The SQL statement to execute. Note To run multiple SQL statements, separate them with a semicolon (;). |
csv_null_replace_str | string | No | Used to replace |
callback | function | No | A callback function to process the SQL operation's result. The input parameter is PollAsyncSQLExecuteResult. Note This parameter takes effect only when |
polling_interval | int | No | The polling interval, in seconds, for the execution result. The default is 10. If you set this value to 0 or less, the task is submitted without waiting for the result. The status check includes a built-in retry mechanism. |
show_return_value_in_logs | bool | No | Specifies whether to write the return value of the callback function to the log. Defaults to |
PollAsyncSQLExecuteResult
Parameter | Type | Description |
Status | string | The execution status of the SQL operation. Valid values are:
|
SQLType | string | The type of SQL operation. Valid values are:
|
ResultType | string | The result type of the SQL operation. Note An empty value indicates the SQL operation has not completed.
|
ResultContent | JSON | The result of the SQL operation. Note When you download a file using the |
Example
task_id and dag are Airflow-specific parameters. For more information, see the official Airflow documentation.
from airflow import DAG
from airflow.decorators import task
from airflow.models.param import Param
from airflow.operators.empty import EmptyOperator
from airflow.providers.alibabadms.cloud.operators.dms_sql import DMSSqlOperator
import json
import requests
def callback(result):
print(f"result: {result}")
if 'Data' in result and 'ResultContent' in result['Data']:
link = result['Data']['ResultContent']['ResultSetFileLink']
print(f"get link: {link}")
http_res = requests.get(link, headers={
"x-oss-range-behavior": "standard"
})
print(f"link res: {http_res.text}")
with DAG(
"dms_sql_dblink",
params={
},
) as dag:
sql_operator = DMSSqlOperator(
task_id="sql_test_dblink",
instance="dblink_90",
database="student_db",
sql="show databases;show tables;",
csv_null_replace_str="null",
callback=callback,
polling_interval=5,
dag=dag
)
run_this_last = EmptyOperator(
task_id="run_this_last",
dag=dag,
)
sql_operator >> run_this_last
if __name__ == "__main__":
dag.test(
run_conf={}
)All DMS Airflow operators support common features like task cancellation and automatic retries. For more information, see Airflow DMS Operator.