目录
概述
为了便于管理,Apache Airflow在其对象中支持RESTAPI。其官网其实针对该API的使用方法有介绍,详细的可以参考:参考文献 - Airflow REST API。由于版本升级,在Airflow 2.0以后发布了“stable REST API”。Airflow的Webserver能够接收json形式的请求,并返回json形式的响应,在请求中需定义头:
Content-type: application/json
Accept: application/json
更新配置
Airflow的默认配置不允许任何形式的request请求,需要修改airflow.cfg文件:
$ airflow config get-value api auth_backends
airflow.api.auth.backend.basic_auth
官方用例
参考官方历史版本REST API的使用频率比较高的几个测试用例:
- 手动触发DAG&&传参
POST /api/experimental/dags/<DAG_ID>/dag_runs
curl -X POST \
'http://localhost:8080/api/v1/dags/<DAG_ID>/dag_runs' \
--header 'Cache-Control: no-cache' \
--header 'Content-Type: application/json' \
--data '{"conf":"{\"key\":\"value\"}"}'
- 查询指定DAG的执行
GET /api/v1/dags/<DAG_ID>/dag_runs
- 查询指定TASK的状态
GET /api/v1/dags/<DAG_ID>/tasks/<TASK_ID>
- 手动停止DAG
GET /api/v1/dags/<DAG_ID>/paused/<string:paused> #停止时设置<string:paused>为'true'
接口用例(Python)
官方文档中写过,请求路由后面添加update_mask可以直接查询更新对象中的某个字段,相对于更新所有字段更加快捷,不过这里我图个简单,不使用update_mask。
def trigger_dag(dag_id, endpoint, params, user, password):
input_data = dict()
input_data["conf"] = params
headers = {"Content-type":"application/json"}
url = "http://{}/api/v1/dags/{}/dagRuns".format(endpoint, dag_id)
response = request.post(url, data=json.dumps(input_data), auth=(user, password), header=headers)
return response