使用数据创建数据表
使用该接口可以创建数据表并导入
请求
curl -vvv -X POST "$API_HOST/open/api/v1/tablenodes/$table_id/rows/append/" -H "Authorization: $access-token" -d "skipFirstRow=false&projectID=$project_id&name=$name" -F "columns=@$file_path" -F "rows=@$csv_file_path"
请求使用表单的方式提交,表单中包含两个文件字段: coulumns 和 rows,分别代表的「数据表的列定义」和「数据行」。详情参考下面表格
请求体form-data示例
| Key | Value | 必需 | 备注 | 
|---|---|---|---|
| projectID | 264 | 是 | 项目 ID | 
| name | 数据表名称 | 是 | |
| skipFirstRow | false | 否 | 是否跳过CSV首行 | 
| columns | Json 文件 create_table_columns.json | 是 | 列配置json文件:内容参考示例文件。一个更复杂的示例文件: create-table-columns-complex.json | 
| rows | Csv 文件 create-table-data.csv | 是 | 行数据csv文件,内容参考示例文件。 | 
columns 参数 json格式说明
| 名称 | 类型 | 描述 | 必需 | 
|---|---|---|---|
| array | 所有列配置数组 | 是 | |
| ∟ name | string | 列名称 | 是 | 
| ∟ type | string | 列类型:multiLineText number datetime singleChoice coordinate | 是 | 
| ∟ typeOptions | object | 列选项:number datetime singleChoice 时需要 | 否 | 
| ∟ format | string | 数值格式:number percentage commaNumber | 是:列类型为number时 | 
| ∟ precision | string | 数值精度: 0-5 | 是:列类型为number时 | 
| ∟ dateFormat | string | 日期格式: year/month/day month/day/year detail | 是:列类型为datatime时 | 
| ∟ timeFormat | string | 时间格式: hidden 24-hour-clock 12-hour-clock | 是:列类型为datatime时 | 
| ∟ choices | array | 选项列表: 为空时自动创建 | 否:列类型为singleChoice时 | 
| ∟ name | string | 选项名: 当选项名和单元格值相等时使用该选项 | 是:列类型为singleChoice时 | 
| ∟ color | string | 选项颜色: 为空时随机设置颜色 | 否:列类型为singleChoice时 | 
| ∟ isPrimary | boolean | 是否为主列:只能设置一个,且列类型为:multiLineText number datetime | 否 | 
Python 示例
def create_table(access_token, data, columns_file, data_file):
    url = '%s/open/api/v1/tablenodes/import/' % API_HOST
    files = {'rows': open(data_file, 'rb'),
             'columns': open(columns_file, 'rb')}
    headers = {'Authorization': access_token}
    req = requests.post(url, data=data, headers=headers, files=files)
    return req.json()
# create project
resp = ma.create_project(access_token, 'proj-name', 'proj-desc')
time.sleep(3) # wait for backend jobs finish
ws_resp = ma.get_workspace_detail(access_token, workspace)
project = ws_resp['detail']['detail']['projects'][0]
# create table using csv
data = {
    'projectID': project['id'],
    'name': 'test-csv-upload',
    'skipFirstRow': True
}
resp = ma.create_table(access_token, data,
                       'data/create_table_columns.json',
                       'data/gd.csv')
响应
响应体
部分主要信息说明
| 名称 | 类型 | 描述 | 
|---|---|---|
| detail | object | 新建数据表 | 
| ∟ id | int64 | 数据表ID | 
| ∟ columns | array | 列配置数据:可保存为json文件,追加数据时需要和更新时需要 | 
| ∟ id | string | 列ID | 
| ∟ type | string | 列类型 | 
| ∟typeOptions | object | 列选项 | 
| ∟ isPrimary | boolean | 是否为主列 | 
| ∟ rowIDs | map | CSV行号对应的数据表行ID:更新行数据时需要 | 
响应体示例
{
    "code": 0,
    "detail": {
        "id": 981,
        "name": "test",
        "type": "table",
        "order": 442,
        "parentID":null,
        "projectID": 264,
        "subscribed":false,
        "published":false,
        "roleInherited":true,
        "createTime": "2022-04-15T07:51:38.699027Z",
        "updateTime": "2022-04-15T07:51:38.699027Z",
        "columns": [
            {
                "id": "6259240a04b5540935aef03d",
                "name": "Title",
                "type": "multiLineText",
                "typeOptions":null,
                "isPrimary":true            },
            {
                "id": "6259240a04b5540935aef03e",
                "name": "Region",
                "type": "singleChoice",
                "typeOptions": {
                    "choices": [
                        {
                            "color": "redLight",
                            "id": "61320ba43bcee9f811eef77d",
                            "name": "Gongye District"
                        },
                        {
                            "color": "salmonLight",
                            "id": "61320ba43bcee9f811eef77e",
                            "name": "Xiacheng District"
                        },
                        {
                            "color": "greyLight",
                            "id": "61320ba43bcee9f811eef786",
                            "name": "Linan District"
                        }
                    ]
                },
                "isPrimary":false            },
            {
                "id": "6259240a04b5540935aef03f",
                "name": "Location",
                "type": "multiLineText",
                "typeOptions":null,
                "isPrimary":false            },
            {
                "id": "6259240a04b5540935aef040",
                "name": "FAR",
                "type": "number",
                "typeOptions": {
                    "format": "number",
                    "precision": 1
                },
                "isPrimary":false            },
            {
                "id": "6259240a04b5540935aef041",
                "name": "Geo",
                "type": "coordinate",
                "typeOptions":null,
                "isPrimary":false            }
        ],
        "rowIDs": {
            "0": "6259240a04b5540935aef04c",
            "1": "6259240a04b5540935aef04d"
        },
        "importErr":null    },
    "extra":null,
    "message": "OK",
    "requestID":null
}