Browse Source

修改示例

master
richie 2 years ago
parent
commit
2470e9b063
  1. 49
      README.md
  2. BIN
      screenshots/api_datasource.png
  3. BIN
      screenshots/api_datasource_effect.png
  4. 19
      src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java
  5. 21
      src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java

49
README.md

@ -2,23 +2,37 @@
## 调用示例代码
### 开启API数据源
```java
Authentication authentication = new Authentication("#key", "#secret");
DatasourceOperation operation = new DatasourceOperation(authentication);
operation.open();
// 创建一个分组,如果已经存在,则直接跳过
operation.createGroup("TestGroup");
// 在分组下创建一个表,返回创建的表的ID,如果表已经存在,则直接返回表ID
String tableId = operation.createTable("HelloTable", "TestGroup");
// 获取上传文件的url
```
在开启后,可以在企业数据的"数据导入"菜单看到如下图所示的API数据源入口
![api_datasource](/screenshots/api_datasource.png)
###
```java
Authentication authentication = new Authentication("#key", "#secret");
DatasourceOperation operation = new DatasourceOperation(authentication);
// // 创建表并返回表id
String tableId = operation.createTable("HelloTable");
// 获取给该表提供数据的文件的上传地址
String fileUploadUrl = operation.requestUploadUrl(tableId);
// 将数据文件上传
HttpKits.upload(fileUploadUrl, new File(System.getProperty("user.dir") + "/data/地区数据分析.csv"));
// 标记该表的数据已经上传完成
operation.markAsFinish(tableId);
```
在调用了上述代码后,就可以在API数据源页面看到新增的表和其数据了,这时就可以和其他数据源表一样,添加到项目中使用了:
![api_datasource_effect](/screenshots/api_datasource_effect.png)
## API文档
### 开启API数据源
@ -52,14 +66,14 @@ operation.markAsFinish(tableId);
```
### 分组管理
#### 创建分组
#### 创建分组(暂未启用)
请求地址:```/api/v1/datasource/group/create```
请求类型:POST
请求参数:```{"name":"#分组名"}```
#### 读取所有分组
#### 读取所有分组(暂未启用)
请求地址:```/api/v1/datasource/group/list```
请求类型:POST
@ -70,8 +84,16 @@ operation.markAsFinish(tableId);
请求类型:POST
请求参数: ``` {"group":"#分组名", "name":"#表名"} ```,分组名可以不传,默认值为“默认”
请求参数: ``` {"tableName":"#表名"} ```
返回值:
```json
{
"success": true,
"code": "200",
"data":"2469965eae4d45339448fb1e74d2b733"
}
```
#### 获取表数据上传地址
请求地址:```/api/v1/datasource/table/upload/url```
@ -80,6 +102,15 @@ operation.markAsFinish(tableId);
请求参数:``` {"tableId":"#表Id"} ```
返回值:
```json
{
"success": true,
"code": "200",
"data":"upload_url"
}
```
#### 标记表数据已上传完成
请求地址:```/api/v1/datasource/table/upload/finish```

BIN
screenshots/api_datasource.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

BIN
screenshots/api_datasource_effect.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

19
src/main/java/com/fanruan/hihidata/datasource/server/ServerApplication.java

@ -11,14 +11,19 @@ import java.io.File;
public class ServerApplication {
public static void main(String... args) throws Exception {
Authentication authentication = new Authentication("ZDdjOWJhZGI5OTBjNDI3ZWE2MzAzYWYwY2UyMTI1Zjg=", "103d4522039be2d17588882b7ae5ba12");
Authentication authentication;
if (args.length > 1) {
authentication = new Authentication(args[0], args[1]);
} else {
authentication = new Authentication("#key", "#secret");
}
DatasourceOperation operation = new DatasourceOperation(authentication);
//operation.open();
// 创建一个分组,如果已经存在,则直接跳过
//operation.createGroup("TestGroup");
// 在分组下创建一个表,返回创建的表的ID,如果表已经存在,则直接返回表ID
String tableId = operation.createTable("HelloTable");
// 获取上传文件的url
// 创建表并返回表id
String tableId = operation.createTable("地区数据");
//String tableId = "dfb18e8b6ee7442cbc00a70c181168ac";
//a9d3a8474df3421ab8a064435f9f6d65
System.out.println("table:" + tableId);
// 获取给该表提供数据的文件的上传地址
String fileUploadUrl = operation.requestUploadUrl(tableId);
// 将数据文件上传
HttpKits.upload(fileUploadUrl, new File(System.getProperty("user.dir") + "/data/地区数据分析.csv"));

21
src/main/java/com/fanruan/hihidata/datasource/server/http/HttpKits.java

@ -230,7 +230,18 @@ public class HttpKits {
* @throws IOException 上传中出现错误则抛出此异常
*/
public static void upload(String url, File file) throws IOException {
upload(url, file, StandardCharsets.UTF_8);
upload(url, file, StandardCharsets.UTF_8, HttpRequestType.PUT);
}
/**
* 上传文件到指定的服务器
*
* @param url 接收文件的服务器地址
* @param file 要上传的文件默认的文件编码为utf-8
* @throws IOException 上传中出现错误则抛出此异常
*/
public static void upload(String url, File file, HttpRequestType requestType) throws IOException {
upload(url, file, StandardCharsets.UTF_8, requestType);
}
/**
@ -241,8 +252,8 @@ public class HttpKits {
* @param charset 文件的编码
* @throws IOException 上传中出现错误则抛出此异常
*/
public static void upload(String url, File file, Charset charset) throws IOException {
upload(url, new FileEntity(file), charset);
public static void upload(String url, File file, Charset charset, HttpRequestType requestType) throws IOException {
upload(url, new FileEntity(file), charset, requestType);
}
/**
@ -253,8 +264,8 @@ public class HttpKits {
* @param charset 文件的编码
* @throws IOException 上传中出现错误则抛出此异常
*/
public static void upload(String url, FileEntity fileEntity, Charset charset) throws IOException {
upload(url, fileEntity, charset, Collections.emptyMap(), HttpRequestType.POST);
public static void upload(String url, FileEntity fileEntity, Charset charset, HttpRequestType requestType) throws IOException {
upload(url, fileEntity, charset, Collections.emptyMap(), requestType);
}
/**

Loading…
Cancel
Save