INFO-调用MammutOpenApi实现上传下载任务包
更新时间: 2024-03-11 02:51:23
阅读 930
调用MammutOpenApi实现上传下载任务包
功能说明
适用模块
中台api
具体说明
调用MammutOpenApi实现上传下载任务包
使用示例
开发者账号密码获取方式
- 先使用需要获取开发者账号密码的用户登陆猛犸平台
http://xxxxxxxx.com/main#/activity/dashboard
- 保留猛犸页面网址部分,后缀部分改为
https://xxxxxxxx.com/api/user/apikey/set?email= xx@xxxxxxxxxx.com
其中https://xxxxxxxx.com
就是登陆后猛犸页面地址email=xx@xx
就是指通过该邮箱账户设置openapi的 apikey 和 masterkey。 - 接下来调用
/api/user/apikey/get
接口用来获取之前通过set接口设置的两个keyhttps:// xxxxxxxx.com/api/user/apikey/get?email= xx@xx
- 以admin账号为例执行结果如下图: http://xxxxxxxx.com//api/user/apikey/set?email=admin.mammut@163.com
代码样例
导入相关依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
代码示例:
package org.example;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.UUID;
/***********************************
*@Desc TODO
*@ClassName MammutUploadTaskDemo
*@Author DLX
*@Data 2021/8/16 14:23
*@Since JDK1.8
*@Version 1.0
***********************************/
public class MammutUploadTaskDemo {
//服务器地址
public static String HOST;
//app_key 需要更换成自己的api_key
public static String API_KEY;
//秘钥用来生成token 需要更换成自己的master_key
public static String MASTER_KEY;
//项目文件的openApi URL
public static String URL;
//本地文件路径
public static String LOCAL_FILE_PATH;
public RestTemplate restApiTemplate;
public BASE64Encoder base64en;
public MammutUploadTaskDemo(String HOST, String API_KEY, String MASTER_KEY, String URL, String LOCAL_FILE_PATH) throws NoSuchAlgorithmException {
this.HOST = HOST;
this.API_KEY = API_KEY;
this.MASTER_KEY = MASTER_KEY;
this.URL = URL;
this.LOCAL_FILE_PATH = LOCAL_FILE_PATH;
this.restApiTemplate = new RestTemplate();
this.base64en = new BASE64Encoder();
}
/* 获取token
* */
public String getToken(String uri) throws UnsupportedEncodingException,NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return base64en.encode(md5.digest((MASTER_KEY + uri).getBytes("utf-8")));
}
/**
* 组装请求体
* @return
* @throws Exception
*/
public static MultiValueMap<String, Object> getPostBody(String HOST, String API_KEY, String MASTER_KEY, String URL, String LOCAL_FILE_PATH,String product,String taskName,String clusterId) throws Exception{
MammutUploadTaskDemo mammutApiHandler = new MammutUploadTaskDemo(HOST, API_KEY, MASTER_KEY, URL, LOCAL_FILE_PATH);
String token = mammutApiHandler.getToken(URL);
String apiKey = MammutUploadTaskDemo.API_KEY;
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String,Object>();
FileSystemResource file = new FileSystemResource(LOCAL_FILE_PATH);
body.add("file",file);
body.add("token", URLEncoder.encode(token,"utf-8"));
body.add("apiKey",URLEncoder.encode(apiKey,"utf-8"));
body.add("product",product);
body.add("taskName",taskName);
body.add("clusterId",clusterId);
return body;
}
/**
* demo main函数入口
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
if ("upload".equals(args[0])){
RestTemplate restApiTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(getPostBody(args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]), headers);
JSONObject ret = restApiTemplate.postForObject(MammutUploadTaskDemo.HOST + URL,request, JSONObject.class);
System.out.println(ret);
}else if ("download".equals(args[0])){
MammutUploadTaskDemo mammutApiHandler = new MammutUploadTaskDemo(args[1],args[2],args[3],args[4],args[5]);
String token = mammutApiHandler.getToken(URL);
String apiKey = MammutUploadTaskDemo.API_KEY;
String taskName = args[7];
RestTemplate restApiTemplate = new RestTemplate();
String url = MammutUploadTaskDemo.HOST + URL
+ "?product=" + URLEncoder.encode(args[6], "utf-8")
+ "&clusterId=" + URLEncoder.encode(args[8], "utf-8")
+ "&taskName=" + URLEncoder.encode(taskName, "utf-8")
+ "&online=" + URLEncoder.encode(Boolean.FALSE.toString(), "utf-8")
+ "&apiKey=" + URLEncoder.encode(apiKey, "utf-8")
+ "&token=" + URLEncoder.encode(token, "utf-8");
ResponseEntity<byte[]> rsp = restApiTemplate.getForEntity(url, byte[].class);
// 校验http状态码
System.out.println("HTTP状态码:" + rsp.getStatusCode());
if (!rsp.getStatusCode().equals(HttpStatus.OK)) {
System.out.println("HTTP状态码异常");
return;
}
// 校验业务异常,如果返回Content-Type=[application/json;charset=UTF-8] 说明有业务上的异常
// Content-Type=[multipart/form-data] 一般是正确下载到了文件
System.out.println(rsp.getHeaders());
if (rsp.getHeaders().getContentType().toString().equals(MediaType.APPLICATION_JSON_UTF8_VALUE)) {
System.out.println("业务错误信息:" + JSON.parse(rsp.getBody()));
return;
}
// 下载文件并保存在本地
try {
String targetPath = LOCAL_FILE_PATH
+ taskName + "_" + UUID.randomUUID().toString().substring(0, 6) + ".zip";
Files.write(Paths.get(targetPath), Objects.requireNonNull(rsp.getBody(), "未获取到下载文件"));
System.out.println("文件下载完毕,路径:" + targetPath);
} catch (IOException e) {
System.out.println("文件写入失败:" + e.getMessage());
}
}else {
System.out.println("请检查输入参数");
}
}
}
参数样例
以该任务为例: 上传时所需的参数为:
{"upload","http://easydemo-workbench.163yun.com/","a8xxxxxxx4-xxxx-xxxx-xxxx-4xxxxxx80","b1xxxx05-xxxx-xxxx-xxxx-7xxxxxxxx87e","/v1/openapi/etl/task/upload","C:\\Users\\Desktop\\测试\\test11_6f6c19.zip","bdms","test11","easyops-cluster"};
下载时所需的参数为:
{"download","http://easydemo-workbench.163yun.com/","a8xxxxxxx4-xxxx-xxxx-xxxx-4xxxxxx80","b1xxxx05-xxxx-xxxx-xxxx-7xxxxxxxx87e","/v1/openapi/etl/task/download","C:\\Users\\Desktop\\测试\\","bdms","test11","easyops-cluster"};
下载任务包API相关信息
请求信息
GET: /v1/openapi/etl/task/download?apiKey={apiKey}&token={token}&product={product}&clusterId={clusterId}&taskName={taskName}&online={online}
请求头:无
请求参数:无
范例展示:无
响应信息
响应头:
名称 | 默认值 | 描述 |
---|---|---|
Content-Type | application/octet-stream | - |
返回数据:无
范例展示:无
上传任务包API相关信息
请求信息
POST: /v1/openapi/etl/task/upload
请求头:
名称 | 默认值 | 描述 |
---|---|---|
Content-Type | multipart/form-data | - |
请求参数:哈希
名称 | 类型 | 描述 | 必需 | 默认值 |
---|---|---|---|---|
product | String | 产品账号 | 是 | intern |
clusterId | String | 集群ID | 是 | 集群ID |
file | File | 上传⽂件 | 是 | - |
taskName | String | 任务名 | 是 | flow_task |
apiKey | String | 开发者账号 | 是 | 96479111-4159-805a-bda8-f5765ddc4768 |
token | String | 开发者账号密码 | 是 | qajHvy6W1uykxOB74Od6UA%3D%3D |
范例展示:
{
"apiKey": "96479111-4159-805a-bda8-f5765ddc4768",
"token": "qajHvy6W1uykxOB74Od6UA%3D%3D",
"product": "intern",
"clusterId": "集群ID",
"file": "<File>",
"taskName": "flow_task"
}
响应信息
响应头:
名称 | 默认值 | 描述 |
---|---|---|
Content-Type | application/json | - |
返回数据:哈希
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
code | Number | 状态码 | 200 |
msg | String | 状态描述。若code不等于200,则返回简短的出错原因 | success |
范例展示:
{
"code": 200,
"msg": "success"
}
作者:邓崃翔
文档反馈
以上内容对您是否有帮助?