
# 创建外表
当完成[创建外部服务器](https://support.huaweicloud.com/migration-dws/dws_15_0015.html#ZH-CN_TOPIC_0000002254983677)后，在DWS数据库中创建一个OBS外表，用来访问存储在OBS上的数据。OBS外表是只读的，只能用于查询操作，可直接使用SELECT查询其数据。根据集群版本不同，操作有所区别。
- 8.2.0及以上版本：参见[管理OBS数据源](https://support.huaweicloud.com/mgtg-dws/dws_01_1602.html)完成。
- 8.2.0以前版本，参见以下步骤完成。
![](https://support.huaweicloud.com/migration-dws/public_sys-resources/caution_3.0-zh-cn.png)
用户创建外表时如果报错"permission denied for foreign server xxx"，说明当前用户没有外部服务器权限，需要执行以下命令进行授权（obs_server和u1，请替换为实际的外部服务器名称和当前用户名称）：
```
GRANT usage ON foreign server obs_server TO u1;
```
#### 创建外表
创建外表的语法格式如下。
```
CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name 
( [ { column_name type_name 
    [ { [CONSTRAINT constraint_name] NULL |
    [CONSTRAINT constraint_name] NOT NULL |
      column_constraint [...]} ] |
      table_constraint [, ...]} [, ...] ] ) 
    SERVER dfs_server 
    OPTIONS ( { option_name ' value ' } [, ...] ) 
    DISTRIBUTE BY {ROUNDROBIN | REPLICATION}
    [ PARTITION BY ( column_name ) [ AUTOMAPPED ] ] ;
```
例如，创建一个名为"*product_info_ext_obs* "的外表，对语法中的参数按如[表1]描述进行设置：
 表1创建外表参数 
| 参数名               | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | 示例                     |
|:---|:---|:---|
| table_name        | 外表的表名。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | *product_info_ext_obs* |
| column_name       | 外表中的字段名。多个字段用","隔开。外表的字段个数和字段类型，需要与OBS上保存的数据完全一致。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | product_price          |
| type_name         | 字段的数据类型。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | integer                |
| SERVER dfs_server | 外表的外部服务器名称，这个server必须存在。外表通过设置外部服务器连接OBS读取数据。此处应填写为参照[创建外部服务器](https://support.huaweicloud.com/migration-dws/dws_15_0015.html#ZH-CN_TOPIC_0000002254983677)创建的外部服务器名称。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | *obs_server*           |
| OPTIONS           | 用于指定外表数据的各类参数，关键参数如下所示。 - "format"：表示对应的OBS服务上的文件格式，支持"orc"、"carbondata"和"parquet"格式。  - "foldername"：必选参数。数据源文件的OBS路径，此处仅需要填写"/*桶名* /*文件夹目录层级* /"。 可以先通过[OBS上的数据准备](https://support.huaweicloud.com/migration-dws/dws_15_0014.html#ZH-CN_TOPIC_0000002219983976)中的[2](https://support.huaweicloud.com/migration-dws/dws_15_0014.html#ZH-CN_TOPIC_0000002219983976__zh-cn_topic_0000001811609589_zh-cn_topic_0000001188482188_zh-cn_topic_0000001145410931_zh-cn_topic_0102810712_li123314509351)获取数据源文件的完整的OBS路径，该路径为OBS服务的终端节点（Endpoint）。   - "totalrows"：可选参数。该参数不是导入的总行数。由于OBS上文件可能很多，执行analyze可能会很慢，通过"totalrows"参数，让用户来设置一个预估的值，使优化器能通过这个值做大小表的估计。一般预估值与实际值的数量级差不多时，查询效率较高。  - **"encoding"**：外表中数据源文件的编码格式名称，缺省为utf8。对于OBS外表此参数为必选项。   | format 'orc'           |
| DISTRIBUTE BY     | 这个子句是必须的，当前支持**ROUNDROBIN和** **REPLICATION** 分布方式。缺省为**ROUNDROBIN**分布方式。 **ROUNDROBIN**分布方式表示外表在从数据源读取数据时，DWS集群每一个节点随机读取一部分数据，并组成完整数据。 **REPLICATION**分布方式表示外表在从数据源读取数据时，DWS集群选取一个节点读取全部数据。因为每个数据节点都有完整的表数据。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | ROUNDROBIN             |
| 语法中的其他参数          | 其他参数均为可选参数，用户可以根据自己的需求进行设置，在本例中不需要设置。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | -                      |
   
根据以上信息，创建外表命令如下所示：
建立不包含分区列的OBS外表，表关联的外部服务器为obs_server，表对应的OBS服务上的文件格式为'orc'，OBS上的数据存储路径为'/mybucket/demo.db/product_info_orc/'。
```
DROP FOREIGN TABLE IF EXISTS product_info_ext_obs;
CREATE FOREIGN TABLE product_info_ext_obs
(
    product_price                integer        not null,
    product_id                   char(30)       not null,
    product_time                 date           ,
    product_level                char(10)       ,
    product_name                 varchar(200)   ,
    product_type1                varchar(20)    ,
    product_type2                char(10)       ,
    product_monthly_sales_cnt    integer        ,
    product_comment_time         date           ,
    product_comment_num          integer        ,
    product_comment_content      varchar(200)                      
) SERVER obs_server 
OPTIONS (
format 'orc', 
foldername '/mybucket/demo.db/product_info_orc/',
encoding 'utf8',
totalrows '10'
) 
DISTRIBUTE BY ROUNDROBIN;
```
建立包含分区列的OBS外表，product_info_ext_obs外表使用product_manufacturer字段作为分区键，obs/mybucket/demo.db/product_info_orc/路径下有如下分区目录：
分区目录1：product_manufacturer=10001
分区目录2：product_manufacturer=10010
分区目录3：product_manufacturer=10086
...
```
DROP FOREIGN TABLE IF EXISTS product_info_ext_obs;
CREATE FOREIGN TABLE product_info_ext_obs
(
    product_price                integer        not null,
    product_id                   char(30)       not null,
    product_time                 date           ,
    product_level                char(10)       ,
    product_name                 varchar(200)   ,
    product_type1                varchar(20)    ,
    product_type2                char(10)       ,
    product_monthly_sales_cnt    integer        ,
    product_comment_time         date           ,
    product_comment_num          integer        ,
    product_comment_content      varchar(200)   ,
    product_manufacturer integer
) SERVER obs_server 
OPTIONS (
format 'orc', 
foldername '/mybucket/demo.db/product_info_orc/',
encoding 'utf8',
totalrows '10'
) 
DISTRIBUTE BY ROUNDROBIN
PARTITION BY (product_manufacturer) AUTOMAPPED;
```
