通過Ajax api 動態傳參 調用 Azkaban
前提
數據倉庫中, 使用 azkaban 作爲任務調度的組件.
倉庫搭建完成後, 需要執行過去兩年的數據, 因爲 Web UI 沒有提供這個功能, 所以調用官方提供的 api, 然後動態傳入日期變量. 達到目的.
Execute a Flow
This API executes a flow via an ajax call, supporting a rich selection of different options. Running an individual job can also be achieved via this API by disabling all other jobs in the same flow.
- Method: GET
- Request URL: /executor?ajax=executeFlow
- Parameter Location: Request Query String
Request Parameters
Parameter | Description |
---|---|
session.id | The user session id.Example Values: 30d538e2-4794-4e7e-8a35-25a9e2fd5 300 |
ajax=executeFlow | The fixed parameter indicating the current ajax action is executeFlow. |
project | The project name of the executing flow.Example Values: run-all-jobs |
flow | The flow id to be executed.Example Values: test-flow |
disabled (optional) | A list of job names that should be disabled for this execution. Should be formatted as a JSON Array String.Example Values: [“job_name_1”, “job_name_2”, “job_name_N”] |
successEmails (optional) | A list of emails to be notified if the execution succeeds. All emails are delimitted with [,|;|\s+].Example Values: [email protected],[email protected] |
failureEmails (optional) | A list of emails to be notified if the execution fails. All emails are delimitted with [,|;|\s+].Example Values: [email protected],[email protected] |
successEmailsOverride (optional) | Whether uses system default email settings to override successEmails.Possible Values: true, false |
failureEmailsOverride (optional) | Whether uses system default email settings to override failureEmails.Possible Values: true, false |
notifyFailureFirst (optional) | Whether sends out email notifications as long as the first failure occurs.Possible Values: true, false |
notifyFailureLast (optional) | Whether sends out email notifications as long as the last failure occurs.Possible Values: true, false |
failureAction (Optional) | If a failure occurs, how should the execution behaves.Possible Values: finishCurrent, cancelImmediately, finishPossible |
concurrentOption (Optional) | Concurrent choices. Use ignore if nothing specifical is required.Possible Values: ignore, pipeline, skip |
flowOverride[flowProperty] (Optional) | Override specified flow property with specified value.Example Values : flowOverride[failure.email]=test@ gmail.com |
Response Object
Parameter | Description |
---|---|
error | Error message if the call has failed |
flow | The executed flow id |
execid | The execution id |
Here is a curl command example:
curl -k --get --data 'session.id=189b956b-f39f-421e-9a95-e3117e7543c9' --data 'ajax=executeFlow' --data 'project=azkaban-test-project' --data 'flow=test' https://localhost:8443/executor
Sample response:
{
message: "Execution submitted successfully with exec id 295",
project: "foo-demo",
flow: "test",
execid: 295
}
在官方文檔中, 請求參數的最後一個
flowOverride[flowProperty] (Optional)
就是相當於 Web UI 中的自定義參數.
示例代碼如下:
package com.whdx.paper.upload.utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.Objects;
public class AzkaBanInvok {
public static void main(String[] args) {
String body = HttpRequest.post("https://192.168.109.159:8443/")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("X-Requested-With", "XMLHttpRequest")
.form("action", "login")
.form("username", "admin")
.form("password", "admin")
.execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
if (Objects.equals(jsonObject.getStr("status"), "success")) {
String sessionId = jsonObject.getStr("session.id");
String result = HttpRequest.get("https://192.168.109.159:8443/executor")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("X-Requested-With", "XMLHttpRequest")
.form("session.id", sessionId)
.form("ajax", "executeFlow")
.form("project", "whadmin")
.form("flow", "export_all")
// 添加自定義參數, dt=2020-04-05.
.form("flowOverride[dt]", "2020-04-05")
.execute().toString();
System.out.println(result);
}
}
}
示例中利用了 hutool 工具庫, 簡化了請求. 使用 HttpClient, HttpURLConnection 同理