Azkaban 外部接口的使用与开发
此文档可用于Azkaban 任务的https调用,提供了查看任务的状态信息、执行情况、任务的创建、上传、触发、取消、调度等功能接口。可以使开发者在编程过程中直接通过https api的调用,来对任务进行控制,开发者可参考azkaban官网提供的api文档。
azkaban官网提供的api文档:https://azkaban.github.io/azkaban/docs/latest/#ajax-api
API调研结果:
功能 | 是否支持 |
java api调用 | 支持(https调用开发) |
curl api调用 | 支持 |
支持https | 支持 |
需要认证 | 需要,简单的session会话开发认证(session 24小时过期时间,重复调用上次session会被覆盖) |
项目的创建、删除 | 支持 |
任务的上传 | 支持 |
任务流的触发、取消、暂停、恢复 | 支持 |
任务流状态信息的获取 | 支持(所有执行状态都可获取,执行中、执行完的状态) |
单个job的执行 | 支持(使用禁用模式) |
SLA警报和自动查杀失败任务 | 支持(包括邮件的设置等) |
cron定时设置 | 支持 |
探究中 |
如下是对于Api简单Demo。
使用Hutool工具,直接进行https调用,pom
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
一、认证
获取session.id,在调用接口时,都会进行session.id的认证,获取到的session.id过期时间为24小时。当会话进行退出、机器关机、切换机器、改变浏览器等操作时,session将会失效。若session.id已过期则重新进行获取即可,应为新会话将覆盖老会话。
请求体【请求方式:post】
Parameter | Description |
---|---|
action=login | The fixed parameter indicating the login action. |
username | The Azkaban username. |
password | The corresponding password. |
代码示例:
String body = HttpRequest.post("https://localhost:8443")
.body("action=login&username=azkaban&password=azkaban")
.execute()
.body();
System.out.println(body);
响应体:
Parameter | Description |
---|---|
error | Return an error message if the login attempt fails. |
session.id | Return a session id if the login attempt succeeds. |
{
"status" : "success",
"session.id" : "c001aba5-a90f-4daf-8f11-62330d034c0a"
}
二、远程创建一个任务项目
在上传任务zip包时,进行远程创建任务姓名。
请求体
arameter | Description |
---|---|
session.id | The user session id.(会话id) |
action=create | The fixed parameter indicating the create project action. (执行任务的动作) |
name | The project name to be uploaded.(项目名) |
description | The description for the project. This field cannot be empty.(项目描述) |
代码示例:
String body = HttpRequest.post("https://localhost:8443/manager?action=create")
.body("session.id=9089beb2-576d-47e3-b040-86dbdc7f523e&name=aaaa&description=11")
.execute()
.body();
System.out.println(body);
响应体 (创建失败):
Parameter | Description |
---|---|
message | The error message. |
error | The error name. |
{
"status":"success",
"path":"manager?project=aaaa",
"action":"redirect"
}
三、删除一个任务项目
请求体:
Parameter | Description |
---|---|
session.id | The user session id. |
delete=true | The fixed parameter to indicate the deleting project action. |
project | The project name to be deleted. |
代码示例:
HttpRequest.get("https://localhost:8443/manager")
.body("session.id=bca1d75d-6bae-4163-a5b0-378a7d7b5a91&delete=true&project=test-delete-project")
.execute();
四、获取项目中的流
azkaban一个项目中可以创建多个流并且每个流中又含有多个依赖job,在此只能获取流
请求参数:
Parameter | Description |
---|---|
session.id | The user session id. |
ajax=fetchprojectflows | The fixed parameter indicating the fetchProjectFlows action. |
project | The project name to be fetched. |
示例代码:
String body = HttpRequest.get("https://localhost:8443/manager")
.body("session.id=6c96e7d8-4df5-470d-88fe-259392c09eea&ajax=fetchprojectflows&project=azkaban-test-project")
.execute()
.body();
System.out.println(body);
响应体:
Parameter | Description |
---|---|
project | The project name. |
projectId | The numerical id of the project. |
flows | A list of flow ids.(任务流的id) Example values: [{"flowId": "aaa"}, {"flowId": "bbb"}] |
{
"project" : "test-azkaban",
"projectId" : 192,
"flows" : [ {
"flowId" : "test"
}, {
"flowId" : "test2"
} ]
}
五、从流中获取job
azkaban一个项目中可以创建多个流并且每个流中又含有多个依赖job,在此可以获取流以及所有jobs
请求体:
Parameter | Description |
---|---|
session.id | The user session id. |
ajax=fetchflowgraph | The fixed parameter indicating the fetchProjectFlows action. |
project | The project name to be fetched. |
flow | The project id to be fetched.(返回参数中,含有每个流下所依赖的job id) |
示例代码:
String body = HttpRequest.get("https://localhost:8443/manager")
.body("session.id=bca1d75d-6bae-4163-a5b0-378a7d7b5a91&ajax=fetchflowgraph&project=texter-1-1&flow=test")
.execute()
.body();
响应体:
Parameter | Description |
---|---|
project | The project name. |
projectId | The numerical id of the project. |
flow | The flow id fetched. |
nodes | A list of job nodes belonging to this flow. Structure:
Example values: [{"id": "first_job", "type": "java"}, {"id": "second_job", "type": "command", "in":["first_job"]}] |
{
"project" : "azkaban-test-project",
"nodes" : [ {
"id" : "test-final",
"type" : "command",
"in" : [ "test-job-3" ]
}, {
"id" : "test-job-start",
"type" : "java"
}, {
"id" : "test-job-3",
"type" : "java",
"in" : [ "test-job-2" ]
}, {
"id" : "test-job-2",
"type" : "java",
"in" : [ "test-job-start" ]
} ],
"flow" : "test",
"projectId" : 192
}
六、执行流
此api可对流进行执行,也对流中的单独的job进行触发
请求参数:
Parameter | Description |
---|---|
session.id | The user session id. (所获取的会话id) Example Values: 30d538e2-4794-4e7e-8a35-25a9e2fd5300 |
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. (禁用需要执行的jobs,当一个流中有a,b,c三个依赖job时,若单独执行一个,可将其他两个禁用即可) 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: foo@email.com,bar@email.com |
failureEmails (optional) | A list of emails to be notified if the execution fails. All emails are delimitted with [,|;|\\s+]. Example Values: foo@email.com,bar@email.com |
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 |
代码示例:
String body = HttpRequest.get("https://localhost:8443/executor")
.body("session.id=189b956b-f39f-421e-9a95-e3117e7543c9&ajax=executeFlow&project=azkaban-test-project&flow=test")
.execute()
.body();
System.out.println(body);
响应体:
Parameter | Description |
---|---|
error | Error message if the call has failed |
flow | The executed flow id |
execid | The execution id |
{
message: "Execution submitted successfully with exec id 295",
project: "foo-demo",
flow: "test",
execid: 295
}
七、取消流的执行
若未运行,则返回错误信息,成功则无返回
请求体:
Parameter | Description |
---|---|
session.id | The user session id. |
ajax=cancelFlow | The fixed parameter indicating the current ajax action is cancelFlow. |
execid | The execution id.【执行id,当job执行时,生成的唯一id】 |
代码示例:
String body = HttpRequest.get("https://localhost:8443/executor")
.body("session.id=34ba08fd-5cfa-4b65-94c4-9117aee48dda&ajax=cancelFlow&execid=302")
.execute()
.body();
System.out.println(body);
响应体(失败状态):
{
"error" : "Execution 302 of flow test isn't running."
}
注意:
在开发者进行调用时api调用时,Azkaban管理员创建的project其他角色无法访问。
以上是提取了常用的api方法,若需更全的api使用可以查看官网 https://azkaban.github.io/azkaban/docs/latest/#ajax-api