## <strong>创建数据模型</strong>

- <strong>接口调用</strong>

http://host:port/smartbi/smartbix/api/dataModel/{pid}

- <strong>接口请求类型</strong>

POST

- <strong>返回值</strong>

CheckResult

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                    |
| --------------------- | --------------------- | ---------------------------------------- |
| id                    | Stirng                | 模型 id                                  |
| errorCode             | String                | 错误码,成功时错误码、错误信息同时为空   |
| errorMessage          | String                | 错误信息,成功时错误码、错误信息同时为空 |

### <strong>简单示例:单物理表</strong>

![](https://wiki.smartbi.com.cn/download/attachments/115002507/DIdcbwCfgon6Kux843ecvX6Wnsh.png)

```json
{  
        "name": "SimpleModelDemo",
        "alias": "模型简单示例",
        "views": [{
                "name": "orders",
                "alias": "订单表",
                "type": "BASIC_TABLE",
                "define": {
                        "tableId": "TAB.northwind.null.orders"
                }
        }],
        "dimensions":[{
                "name": "ShipRegion",
                "alias": "区域",
                "viewName": "orders",
                "fieldName": "ShipRegion",
                "valueType": "STRING"
        }],
        "measures": [{
                "name": "Freight",
                "alias": "运费",
                "viewName": "orders",
                "aggregator": "sum",
                "fieldName": "Freight"
        }]
}
```

### 简单示例 2:SQL 私有查询

```json
{  
        "name": "SimpleModelDemo2",
        "alias": "模型简单示例2",
        "views": [{
                "name": "orders",
                "alias": "订单表",
                "type": "SQL_VIEW",
                "define": {
                        "dataSourceId": "DS.notthwind",
                        "sql": " select * from orders"
                }
        }]
}
```

### <strong>详细示例:2 表关联、定义层次、日期层次、计算度量、计算成员</strong>

```json
注意:需要把注释去掉

{  
        "name": "ModelDemo",
        "alias": "模型示例",
        // 私有查询列表
        "views": [{
                "name": "orderdetails",
                "alias": "订单明细表",
                "type": "BASIC_TABLE",
                "define": {
                        "tableId": "TAB.northwind.null.orderdetails"
                }
                /*,
                "storeType": "EXTRACT",
                "extractSetting": {
                    "type": "FULL"
                }*/
        },{
                "name": "orders",
                "alias": "订单表",
                "type": "BASIC_TABLE",
                "define": {
                        "tableId": "TAB.northwind.null.orders"
                }
                /*,
                "storeType": "EXTRACT",
                "extractSetting": {
                    "type": "FULL"
                }*/
        }],
        // 层次结构列表
        "hierarchies": [{
            "name":"hierArea",
            "alias": "地区",
            "levels": [
                {
                    "viewName": "orders",
                    "name": "ShipRegionLevel",
                    "alias": "区域",
                    "fieldName": "ShipRegion",
                    "levelType": "LEVEL"
                },
                {
                    "viewName": "orders",
                    "name": "ShipProvinceLevel",
                    "alias": "省份",
                    "fieldName": "ShipProvince",
                    "levelType": "LEVEL"
                }
            ]
        }],
        // 日期层次结构列表
        "dateHierarchies": [{
            "name":"hierDate",
            "alias": "时间维度",
            "levels": [
                {
                    "viewName": "orders",
                    "name": "orderYear",
                    "alias": "年",
                    "fieldName": "OrderDate",
                    "levelType": "LEVEL_TIME_YEAR"
                },
                {
                    "viewName": "orders",
                    "name": "orderQuarter",
                    "alias": "季",
                    "fieldName": "OrderDate",
                    "levelType": "LEVEL_TIME_QUARTER"
                },
                {
                    "viewName": "orders",
                    "name": "orderMonth",
                    "alias": "月",
                    "fieldName": "OrderDate",
                    "levelType": "LEVEL_TIME_MONTH"
                },
                {
                    "viewName": "orders",
                    "name": "orderDay",
                    "alias": "日",
                    "fieldName": "OrderDate",
                    "levelType": "LEVEL_TIME_DAY"
                }
            ]
        }],
        // 维度结构列表
        "dimensions":[{
                "name": "ShipRegion",
                "alias": "区域",
                "viewName": "orders",
                "fieldName": "ShipRegion",
                "valueType": "STRING"
        }],
        // 表关系图
        "relationGraph": {
                "relations": [{
                        "srcViewName": "orderdetails",
                        "destViewName": "orders",
                        "linkType": "LEFTJOIN",
                        "fieldRelations": [{
                                "srcFieldName": "OrderID",
                                "destFieldName": "OrderID"
                        }]
                }]
        },
        // 度量列表
        "measures": [{
                "name": "Quantity",
                "alias": "Quantity",
                "viewName": "orderdetails",
                "aggregator": "sum",
                "fieldName": "Quantity"
        },{
                "name": "UnitPrice",
                "alias": "UnitPrice",
                "viewName": "orderdetails",
                "aggregator": "sum",
                "fieldName": "UnitPrice"
        }],
        // 计算度量列表
        "calcMeasures": [
            {
                "name":"calc1",
                "alias":"数量加1",
                "expression": "[Measures].[Quantity] + 1"
            }
        ],
        // 计算成员列表
        "calcMembers": [
            {
                "name":"north",
                "alias":"北方",
                "expression": "[hierArea].[东北] + [hierArea].[华北]",
                "hierName": "[ShipRegion]"
            }
        ]
}
```

- <strong>接口参数及其说明</strong>

pid:父节点 id;

post 内容的结构如下,详细说明如下:

![](/download/attachments/115002507/1.svg 50%x50%)

##### <strong>DataModelVO 模型</strong>

| <strong>属性</strong> | <strong>类型</strong>          | <strong>说明</strong>        |
| --------------------- | ------------------------------ | ---------------------------- |
| id                    | String                         | 模型 id,更新时才需要填      |
| name                  | String                         | 模型名称                     |
| alias                 | String                         | 模型别名                     |
| views                 | LIst<DataModelViewVO>          | 私有查询列表                 |
| measures              | LIst<DataModelMeasureVO>       | 度量列表                     |
| relationGraph         | DataModelRelationGraphVO       | 表关系图                     |
| hierarchies           | List<DataModelHierarchyVO>     | 层次结构列表                 |
| dateHierarchies       | List<DataModelDateHierarchyVO> | 基于日期字段生成时间层次列表 |
| calcMeasures          | LIst<DataModelCalcMeasureVO>   | 计算度量列表                 |
| calcMembers           | LIst<DataModelCalcMemberVO>    | 计算成员列表                 |
| namedSets             | LIst<DataModelNamedSetVO>      | 命名集列表                   |

##### <strong>DataModelViewVO 私有查询</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                                                                                                                       |
| --------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| name                  | String                | 名称                                                                                                                                        |
| alias                 | String                | 别名                                                                                                                                        |
| type                  | ViewType              | 类型:BASIC_TABLE,代表基础表 。以下暂不支持:SQL_VIEW、PROC_VIEW、JS_VIEW、JAVA_VIEW、ETL_VIEW、EXCEL_VIEW、COMBINEDQUERY_VIEW、TIME_TABLE |
| define                | ObjectNode            | 定义,有对应具体的 VO,如基础表为<strong>BasicTableViewDefineVO</strong>                                                                    |
| storeType             | StoreType             | 存储方式:DIRECT、EXTRACT,分别代表直连、抽取                                                                                               |
| extractSetting        | ExtractSettingVO      | 抽取设置                                                                                                                                    |

##### <strong>ExtractSettingVO 抽取设置定义</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                            |
| --------------------- | --------------------- | ------------------------------------------------ |
| type                  | ExtractType           | 抽取方式:FULL、REALTIME,分别代表全量、按次抽取 |

##### <strong>BasicTableViewDefineVO 基础表定义</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong> |
| --------------------- | --------------------- | --------------------- |
| tableId               | String                | 基础表 Id             |

##### <strong>SQLViewDefineVO  </strong><strong>SQL</strong><strong>查询定义</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong> |
| --------------------- | --------------------- | --------------------- |
| dataSourceId          | String                | 数据源 Id             |
| sql                   | String                | sql 语句              |

##### <strong>DataModelMeasureVO 度量</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                               |
| --------------------- | --------------------- | --------------------------------------------------- |
| name                  | String                | 名称                                                |
| alias                 | String                | 别名                                                |
| aggregator            | String                | 聚合方式:SUM、COUNT、 AVG、DISTINCT_COUNT、MAX、MIN |
| fieldName             | String                | 私有查询字段名                                      |
| viewName              | String                | 私有查询名                                          |

##### <strong>DataModelRelationGraphVO 表关系图</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong> |
| --------------------- | --------------------- | --------------------- |
| relations:            | List<RelationVO>      | 表关系列表            |

##### <strong>RelationVO 表关系</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                                                                                           |
| --------------------- | --------------------- | --------------------------------------------------------------------------------------------------------------- |
| srcViewName           | String                | 名称                                                                                                            |
| destViewName          | String                | 别名                                                                                                            |
| linkType              | TableLinkType         | 连接方式:LEFTJOIN、RIGHTJOIN、INNERJOIN、FULLJOIN,分别代表 1 对多、多对 1、1 对 1(内连接)、1 对 1(外连接) |
| fieldRelations        | List<FieldRelationVO> | 私有查询字段名                                                                                                  |

##### <strong>FieldRelationVO  字段关系</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong> |
| --------------------- | --------------------- | --------------------- |
| srcFieldName          | String                | 源字段名              |
| destFieldName         | String                | 目标字段名            |

##### <strong>DataModelHierarchyVO 层次结构</strong>

| <strong>属性</strong> | <strong>类型</strong>  | <strong>说明</strong> |
| --------------------- | ---------------------- | --------------------- |
| name                  | String                 | 名称                  |
| alias                 | String                 | 别名                  |
| levels                | List<DataModelLevelVO> | 层次列表              |

##### <strong>DataModelLevelVO 层次</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                                                                                                                                          |
| --------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| name                  | String                | 名称                                                                                                                                                           |
| alias                 | String                | 别名                                                                                                                                                           |
| fieldName             | String                | 字段名                                                                                                                                                         |
| viewName              | String                | 私有查询名                                                                                                                                                     |
| levelType             | FieldTreeNodeType     | 层次类型:LEVEL、LEVEL_TIME_YEAR、LEVEL_TIME_QUARTER、LEVEL_TIME_MONTH、LEVEL_TIME_WEEK、LEVEL_TIME_DAY、LEVEL_GEO,分别代表普通、年、季、月、周、日、地理层次 |

##### <strong>DataModelDateHierarchyVO 基于日期字段生成层次结构</strong>

| <strong>属性</strong> | <strong>类型</strong>  | <strong>说明</strong> |
| --------------------- | ---------------------- | --------------------- |
| name                  | String                 | 名称                  |
| alias                 | String                 | 别名                  |
| levels                | List<DataModelLevelVO> | 层次列表              |

##### <strong>DataModelCalcMeasureVO 计算度量</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                                              |
| --------------------- | --------------------- | ------------------------------------------------------------------ |
| name                  | String                | 名称                                                               |
| alias                 | String                | 别名                                                               |
| expression            | String                | 表达式                                                             |
| dataFormat            | String                | 数据格式名,可以在<strong>公共设置-数据格式</strong>中查询需要的名 |

##### <strong>DataModelCalcMemberVO 计算成员</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                    |
| --------------------- | --------------------- | ---------------------------------------- |
| name                  | String                | 名称                                     |
| alias                 | String                | 别名                                     |
| expression            | String                | 表达式                                   |
| hierName              | String                | 层次结构名,如:[ShipRegion]             |
| parentName            | String                | 父成员名:[ShipRegion].[All ShipRegions] |

##### <strong>DataModelNamedSetVO 命名集</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>        |
| --------------------- | --------------------- | ---------------------------- |
| name                  | String                | 名称                         |
| alias                 | String                | 别名                         |
| expression            | String                | 表达式                       |
| hierName              | String                | 层次结构名,如:[ShipRegion] |

##### <strong>DataModelDimensionVO 字段属性</strong>

| <strong>属性</strong> | <strong>类型</strong> | <strong>说明</strong>                                              |
| --------------------- | --------------------- | ------------------------------------------------------------------ |
| viewName              | String                | 私有查询名                                                         |
| fieldName             | String                | 字段名                                                             |
| visible               | String                | 可见性                                                             |
| dataFormat            | String                | 数据格式名,可以在<strong>公共设置-数据格式</strong>中查询需要的名 |
| maskingRule           | String                | 脱敏规则                                                           |
| valueType             | ValueType             | 数据类型:INTEGER、DOUBLE、LONG、STRING 等                         |