第三方应用如何通过HTTP接入Linkis
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,第三方应用如何通过HTTP接入Linkis,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。DSS(DataSphereStudio)和Li
千家信息网最后更新 2025年02月02日第三方应用如何通过HTTP接入LinkisDSS(DataSphereStudio)和Linkis的介绍说明不在文中赘述,下面侧重将第三方应用通过http(rest)接入DSS。"
第三方应用如何通过HTTP接入Linkis,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
一、环境说明
环境说明:dss-0.7.0,linkis-0.9.3二、登录 login
发送请求采用过okhttp,RestTemplate,HTTPClient,json与java bean采用过fastjson,gson,Jackson;因为dss的登录状态保持在cookie中,在经过n次踩雷后最终采用RestTemplate(spring-web:5.0.7)+fastjson(1.2.58) 的方式进行请求,并将登录cookie传递给后续其他请求;
POST /api/rest_j/v1/user/login请求参数 { "userName": "hadoop", "password": "hadoop" }返回示例(返回json可能我略有修改,以官方为准) { "method": null, "status": 0, "message": "login successful(登录成功)!", "data": { "isFirstLogin": false, "isAdmin": true, "userName": "hadoop" }}
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* 简单工具类,可以根据自己需求扩展
*/
public class HttpUtil {
public static RestTemplate getRestClient() {
// 下面代码HttpClientBuilder会自动管理cookie。
// 如果 HttpClientBuilder.create().disableCookieManagement,则禁用cookie的管理
CloseableHttpClient build = HttpClientBuilder.create().useSystemProperties().build();
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(build));
}
}
import org.springframework.http.ResponseEntity; /** * 发送post请求进行登录 * @param restClient * @return ResponseEntity */private ResponseEntitylogin(RestTemplate restClient) { JSONObject postData = new JSONObject(); postData.put("userName", "hadoop"); postData.put("password", "hadoop"); String loginUrl = "http://ip:port/api/rest_j/v1/user/login"; return restClient.postForEntity(loginUrl, postData, JSONObject.class); }
三、执行任务 execute
源代码在linkis模块的EntranceRestfulApi
POST /api/rest_j/v1/entrance/execute
请求参数
{
"method":"/api/rest_j/v1/entrance/execute",
"params": {
"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,后面要根据它或者执行文件
}
}
/** * * @param restClient * @param sql 要执行的sql代码 * @return */ private ResponseEntityexecuteSql(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";ResponseEntitystatusResp = 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}"
}
}
}
String historyUrl = "http://ip:port/api/rest_j/v1/jobhistory/" + taskID + "/get";ResponseEntityhisResp = 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查询结果数据"
}
}
String resUrl = "http://ip:port/api/rest_j/v1/filesystem/openFile?path=" + resultLocation + "/_0.dolphin";ResponseEntityresResp = restTemplate.getForEntity(resUrl, JSONObject.class); if (resResp!= null && resResp.getStatusCode().value() == HttpStatus.SC_OK) { //do something}
看完上述内容,你们掌握第三方应用如何通过HTTP接入Linkis的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
任务
文件
状态
示例
登录
成功
参数
模块
源代码
结果
第三方
应用
接入
代码
内容
方法
更多
环境
用户
路径
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
无纸化会议管理服务器厂家
同上一堂网络安全课通知
城投集团软件开发
国家网络安全周宣传短视频蚌埠
临沂大学计算机网络技术编码
文渊数据库
画服务器后原生数据会重置吗
网络技术会计分录
网络安全模式qq不能用
网络安全上墙制度
移动终端与服务器有什么区别
传奇连接服务器已断开
软件开发工具模拟试题
网络安全手抄报红色主题
关于网络安全学习心得怎么写
网络安全警钟海报
诈骗网络安全知识竞赛
中国网络安全图谱大全
网络安全训练样本数据
随着科技进步互联网发展
c 判断是否连接数据库
小米计算器用什么软件开发的
河南科技学院网络安全等级保护
计算机及网络技术基本知识
雷达数据库
软件开发工具模拟试题
萤石云服务器视频怎么下载
腾讯云服务器安全隔离后怎么开启
私募数据库
什么是数据库的数据增量