From 523ec5fc45246530c7cfcb1e406b8323af07c372 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 16 Sep 2022 19:54:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E5=AE=8C=E6=95=B4=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/DatasourceOperation.java | 44 ++++++++++++++++++- .../datasource/server/ServerApplication.java | 17 ++++++- .../datasource/server/http/HttpKits.java | 16 +++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fanruan/hihidata/datasource/server/DatasourceOperation.java b/src/main/java/com/fanruan/hihidata/datasource/server/DatasourceOperation.java index b9cdac7..ffb6938 100644 --- a/src/main/java/com/fanruan/hihidata/datasource/server/DatasourceOperation.java +++ b/src/main/java/com/fanruan/hihidata/datasource/server/DatasourceOperation.java @@ -1,7 +1,11 @@ package com.fanruan.hihidata.datasource.server; import com.fanruan.hihidata.datasource.server.http.HttpKits; +import org.apache.http.HttpEntity; +import org.apache.http.entity.StringEntity; +import org.json.JSONObject; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -11,15 +15,51 @@ import java.util.Map; */ public class DatasourceOperation { - private Authentication auth; + private final Authentication auth; public DatasourceOperation(Authentication auth) { this.auth = auth; } public void open() throws Exception { + JSONObject body = new JSONObject(); + body.put("status", "1"); + HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8); + HttpKits.post(String.format("%s/api/v1/datasource/status/modify", ProjectConstants.BASE_URI), headers(), entity); + } + + public void createGroup(String groupName) throws Exception { + JSONObject body = new JSONObject(); + body.put("name", groupName); + HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8); + HttpKits.post(String.format("%s/api/v1/datasource/group/create", ProjectConstants.BASE_URI), headers(), entity); + } + + public String createTable(String tableName, String groupName) throws Exception { + JSONObject body = new JSONObject(); + body.put("name", tableName); + body.put("group", groupName); + HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8); + return HttpKits.post(String.format("%s/api/v1/datasource/table/create", ProjectConstants.BASE_URI), headers(), entity); + } + + public String requestUploadUrl(String tableId) throws Exception { + JSONObject body = new JSONObject(); + body.put("tableId", tableId); + HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8); + return HttpKits.post(String.format("%s/api/v1/datasource/table/upload/url", ProjectConstants.BASE_URI), headers(), entity); + } + + public void markAsFinish(String tableId) throws Exception { + JSONObject body = new JSONObject(); + body.put("tableId", tableId); + HttpEntity entity = new StringEntity(body.toString(), StandardCharsets.UTF_8); + HttpKits.post(String.format("%s/api/v1/datasource/table/upload/finish", ProjectConstants.BASE_URI), headers(), entity); + } + + private Map headers() throws Exception { Map headers = new HashMap<>(); headers.put("Authorization", auth.getToken()); - HttpKits.post(String.format("%s/api/v1/datasource/status/modify", ProjectConstants.BASE_URI), headers); + return headers; } } diff --git a/src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java b/src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java index 3539aa1..e647051 100644 --- a/src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java +++ b/src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java @@ -1,13 +1,28 @@ package com.fanruan.hihidata.datasource.server; +import com.fanruan.hihidata.datasource.server.http.HttpKits; + +import java.io.File; + /** * @author richie * Created by richie on 2022/9/16 */ public class ServerApplication { - public static void main(String... args) { + public static void main(String... args) throws Exception { Authentication authentication = new Authentication(args[0], args[1]); DatasourceOperation operation = new DatasourceOperation(authentication); + operation.open(); + // 创建一个分组,如果已经存在,则直接跳过 + operation.createGroup("TestGroup"); + // 在分组下创建一个表,返回创建的表的ID,如果表已经存在,则直接返回表ID + String tableId = operation.createTable("HelloTable", "TestGroup"); + // 获取上传文件的url + String fileUploadUrl = operation.requestUploadUrl(tableId); + // 将数据文件上传 + HttpKits.upload(fileUploadUrl, new File(System.getProperty("user.dir") + "/data/地区数据分析.csv")); + // 标记该表的数据已经上传完成 + operation.markAsFinish(tableId); } } diff --git a/src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java b/src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java index deb0a9c..194cb21 100644 --- a/src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java +++ b/src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java @@ -190,6 +190,22 @@ public class HttpKits { .build()); } + /** + * 发起POST请求并获取返回的文本 + * + * @param url 响应请求的的服务器地址 + * @param headers POST请求的参数 + * @return 服务器返回的文本内容 + */ + public static String post(String url, Map headers, HttpEntity body) throws IOException { + return executeAndParse(HttpRequest + .custom() + .url(url) + .headers(headers) + .post(body) + .build()); + } + /** * 上传文件到指定的服务器 *