Browse Source

提供完整的示例

master
richie 2 years ago
parent
commit
523ec5fc45
  1. 44
      src/main/java/com/fanruan/hihidata/datasource/server/DatasourceOperation.java
  2. 17
      src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java
  3. 16
      src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java

44
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<String, String> headers() throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", auth.getToken());
HttpKits.post(String.format("%s/api/v1/datasource/status/modify", ProjectConstants.BASE_URI), headers);
return headers;
}
}

17
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);
}
}

16
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<String, String> headers, HttpEntity body) throws IOException {
return executeAndParse(HttpRequest
.custom()
.url(url)
.headers(headers)
.post(body)
.build());
}
/**
* 上传文件到指定的服务器
*

Loading…
Cancel
Save