|
|
|
# API数据源使用示例
|
|
|
|
|
|
|
|
**支持版本:** 仅企业版支持API数据源。
|
|
|
|
|
|
|
|
**更新间隔:** 数据源表的数据更新的时候,会根据实际情况启动使用该数据源表的分析表的更新,最小更新间隔为1个小时。*举例:当10点更新了一张数据源表,当表数据写入完成后,会立即更新到九数云中,如果10点10分再次更新这张表,则需要在11点的时候,才会将最新的数据更新到九数云中,13点15的时候再次更新这张表的数据,由于在这之前的1个小时没有任何的更新,因此在数据写入完成后,会立即更新到九数云中。*
|
|
|
|
|
|
|
|
## accessKeyId和accessKeySecret的获取
|
|
|
|
参考帮助文档:[获取accessKeyId和accessKeySecret](https://help.fanruan.com/jiushuyun/doc-view-140.html)
|
|
|
|
|
|
|
|
## 数据流转流程图
|
|
|
|
|
|
|
|
![api_data_flow](/screenshots/api_data_flow.png)
|
|
|
|
|
|
|
|
## 调用示例代码
|
|
|
|
|
|
|
|
```Authentication```表示的是传入accessKeyId和accessKeySecret参数,获取临时令牌的操作,可以自行调用其中的RestApi,不是必须使用该类。
|
|
|
|
|
|
|
|
```DatasourceOperation```表示的是可以对API数据源进行的若干操作的集合,可以自行调用其中的RestApi,不是必须使用该类。
|
|
|
|
|
|
|
|
### 开启API数据源
|
|
|
|
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
operation.open();
|
|
|
|
```
|
|
|
|
|
|
|
|
在开启后,可以在企业数据的"数据导入"菜单看到如下图所示的API数据源入口
|
|
|
|
![api_datasource](/screenshots/api_datasource.png)
|
|
|
|
|
|
|
|
### 创建分组
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
// 创建表并返回分组Id
|
|
|
|
String groupId = operation.createGroup("我的分组");
|
|
|
|
```
|
|
|
|
|
|
|
|
### 创建表
|
|
|
|
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
// 创建表并返回表id
|
|
|
|
String tableId = operation.createTable("HelloTable", "#分组Id(可选)");
|
|
|
|
```
|
|
|
|
|
|
|
|
### 更新表数据
|
|
|
|
|
|
|
|
数据的格式采用csv文件存储,格式如下,假设文件名为data.csv,其内容为(其中第一行是表头,第一行之后是表数据):
|
|
|
|
表头行字段中可以将字段类型(目前支持字符串文本string、日期或时间date、数字类型number(不区分大小写))写在字段后,用#分隔,不带类型则默认为文本
|
|
|
|
|
|
|
|
```
|
|
|
|
"合同签约时间(Year Month Day)#date","城市","国家","客户名称","省份","回款金额","合同金额"
|
|
|
|
"18/1/2016","杭州市","中国","浙江臻善科技有限公司","浙江省","1200000","1200000"
|
|
|
|
"1/8/2016","郑州市","中国","杭州明佑电子有限公司","河南省","100800","100800"
|
|
|
|
"1/4/2016","西安市","中国","西北工业大学","陕西省","430000","430000"
|
|
|
|
```
|
|
|
|
|
|
|
|
**特别注意1:csv文件需要使用utf-8编码,其他编码均无法正确解析**
|
|
|
|
|
|
|
|
**特别注意2:为了避免数据中的逗号和csv的分隔符冲突,我们要求所有的数据均要使用双引号包含起来**
|
|
|
|
|
|
|
|
**注意:** 当前默认为全量覆盖表数据,如需切换到增量更新,参考接口/api/v1/datasource/table/modify。
|
|
|
|
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
// 创建表并返回表id
|
|
|
|
String tableId = "上一步获取到表id";
|
|
|
|
// 获取给该表提供数据的文件的上传地址
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
### 删除表
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
String tableId = "#待删除的表id";
|
|
|
|
operation.deleteTable(tableId);
|
|
|
|
```
|
|
|
|
|
|
|
|
### 删除所有表
|
|
|
|
```java
|
|
|
|
Authentication authentication = new Authentication("#key", "#secret");
|
|
|
|
DatasourceOperation operation = new DatasourceOperation(authentication);
|
|
|
|
operation.reset();
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## API文档
|
|
|
|
|
|
|
|
### 开启API数据源
|
|
|
|
#### 查看状态
|
|
|
|
请求地址:```/api/v1/datasource/status```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success":true,
|
|
|
|
"code":"200",
|
|
|
|
"data":{"status":"0"}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 修改状态
|
|
|
|
请求地址:```/api/v1/datasource/status/modify```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数: ``` {"status":"1"} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success":true,
|
|
|
|
"code":"200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### 分组管理
|
|
|
|
#### 创建分组
|
|
|
|
请求地址:```/api/v1/datasource/group/create```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数:```{"groupName":"#分组名"}```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success":true,
|
|
|
|
"code":"200",
|
|
|
|
"data":"#groupId"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 读取所有分组
|
|
|
|
请求地址:```/api/v1/datasource/group/list```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200",
|
|
|
|
"data": [{"id":"#groupId", "name": "#groupName"}]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 读取分组下的所有表
|
|
|
|
请求地址:```/api/v1/datasource/group/table/list```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数:```{"groupId":"#分组Id"}```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200",
|
|
|
|
"data": [{"id":"#tableId","name":"#tableName"}]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 修改分组名
|
|
|
|
请求地址:```/api/v1/datasource/group/modify```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数: ``` {"groupId":"#分组Id","groupName":"#新的分组名"} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### 表管理
|
|
|
|
#### 创建数据源表
|
|
|
|
请求地址:```/api/v1/datasource/table/create```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数: ``` {"tableName":"#表名","groupId":"#分组Id(可选)","increase":true(可选,true表示后续更新为增量更新)} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200",
|
|
|
|
"data":"#tableId"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 获取表数据上传地址
|
|
|
|
请求地址:```/api/v1/datasource/table/upload/url```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数:``` {"tableId":"#表Id"} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200",
|
|
|
|
"data":"#file_upload_url"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 标记表数据已上传完成
|
|
|
|
请求地址:```/api/v1/datasource/table/upload/finish```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数:``` {"tableId":"#表Id"} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 修改数据源表名、更新方式
|
|
|
|
请求地址:```/api/v1/datasource/table/modify```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数: ``` {"tableId":"#表Id","tableName":"#新的表名","increase": true(可选,同创建表的参数increase)} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 删除数据源表
|
|
|
|
请求地址:```/api/v1/datasource/table/delete```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
请求参数: ``` {"tableId":"#表Id"} ```
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 重置整个数据源
|
|
|
|
请求地址:```/api/v1/datasource/reset```
|
|
|
|
|
|
|
|
请求类型:POST
|
|
|
|
|
|
|
|
返回值:
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"code": "200"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|