三方应用通过HTTP接入Linkis

“variable”:{

“k1”:“v1”

},

“configuration”:{

“special”:{

“k2”:“v2”

},

“runtime”:{

“k3”:“v3”

},

“startup”:{

“k4”:“v4”

}

}

},

“executeApplicationName”:“spark”,

“executionCode”:“show tables”,

“runType”:“sql”,

“source”:{

“scriptPath”: “/home/Linkis/Linkis.sql”

}

}

返回示例

{

“method”: “/api/rest_j/v1/entrance/execute”,

“status”: 0,

“message”: “请求执行成功”,

“data”: {

“execID”: “030418IDEhivebdpdwc010004:10087IDE_johnnwang_21”,//执行id,后面要根据它获取任务状态

“taskID”: “123” // 任务id,后面要根据它或者执行文件

}

}

三方应用通过HTTP接入Linkis_第1张图片

/**

  • @param restClient

  • @param sql 要执行的sql代码

  • @return

*/

private ResponseEntity executeSql(RestTemplate restClient, String sql) {

String url = “/api/rest_j/v1/entrance/execute”;

JSONObject map = new JSONObject();

map.put(“method”, url);

map.put(“params”, new HashMap<>()); //用户指定的运行服务程序的参数,必填,里面的值可以为空

map.put(“executeApplicationName”, “hive”);//执行引擎,我用的hive

map.put(“executionCode”, sql);

map.put(“runType”, “sql”);//当用户执行如spark服务时,可以选择python、R、SQL等,不能为空

//因为我没有执行文件脚本,所以没有scriptPath参数

String executeSql = “http://ip:port” + url;

return restClient.postForEntity(executeSql, map, JSONObject.class);

}


[](()四、查看任务状态 status

源代码在linkis模块的EntranceRestfulApi

GET /api/rest_j/v1/entrance/${execID}/status

返回示例

{

“method”: “/api/rest_j/v1/entrance/{execID}/status”,

“status”: 0,

“message”: “获取状态成功”,

“data”: {

“execID”: “${execID}”,

“status”: “Running”

}

}

String statusUrl = “http://ip:port/api/rest_j/v1/entrance/” + execID + “/status”;

ResponseEntity statusResp = restTemplate.getForEntity(statusUrl, JSONObject.class);

if (statusResp != null && statusResp.getStatusCode().value() == HttpStatus.SC_OK) {

String status;

for (; ; ) {

statusResp = restTemplate.getForEntity(statusUrl, JSONObject.class);

status = statusResp.getBody().getJSONObject(“data”).getString(“status”);

//死循环查看任务状态,如果任务成功或者失败,则退出循环

if (“Succeed”.equals(status) || “Failed”.equals(status)) {

break;

}

}

if (“Succeed”.equals(status)) {

// do something

}

}


[](()五、获取执行结果文件 get

源代码在linkis模块 QueryRestfulApi

GET /api/rest_j/v1/jobhistory/${taskId}/get

返回示例

{

“method”: “/api/jobhistory/{id}/get”,

“status”: 0,

“message”: “OK”,

“data”: {

“task”: {

“taskID”: 3111,

“instance”: “test-dn2:9108”,

“execId”: “IDE_hadoop_46”,

“umUser”: “hadoop”,

“engineInstance”: “test-dn2:37301”,

“executionCode”: “show databases”, //执行的sql

“progress”: 1.0,

“logPath”: “file:///linkis/hadoop/log/IDE/2020-09-08/3111.log”,// 日志路径

“resultLocation”: “hdfs:///linkis2/hadoop/dwc/20200908/IDE/3111”,//sql执行结果所存储的文件路径

“status”: “Succeed”,

“createdTime”: 1599551337000,

“updatedTime”: 1599551339000,

“engineType”: null,

“errCode”: null,

“errDesc”: null,

“executeApplicationName”: “hive”,

“requestApplicationName”: “IDE”,

“scriptPath”: null,

“runType”: “sql”,

“paramsJson”: “{}”,

“costTime”: 2000,

“strongerExecId”: “030413IDEhivetest-dn2:9108IDE_hadoop_46”,

“sourceJson”: “{“scriptPath”:null}”

}

}

}
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】

String historyUrl = “http://ip:port/api/rest_j/v1/jobhistory/” + taskID + “/get”;

ResponseEntity hisResp = restTemplate.getForEntity(historyUrl, JSONObject.class);

if (hisResp != null && hisResp.getStatusCode().value() == HttpStatus.SC_OK) {

String resultLocation = hisResp.getBody().getJSONObject(“data”).getJSONObject(“task”).getString(“resultLocation”);

}


[](()六、打开结果文件 openFile

源代码在linkis模块 FsRestfulApi

GET /api/rest_j/v1/filesystem/openFile?path=${resultLocation}/_0.dolphin

返回示例

{

“method”: “/api/filesystem/openFile”,

“status”: 0,

“message”: “OK”,

“data”: {

“sql查询结果数据”

}

你可能感兴趣的:(Java,经验分享)