IMPORT FOREIGN SCHEMA语句用于批量创建外部表。本文为您介绍IMPORT FOREIGN SCHEMA语句的用法和使用限制。
使用限制
- 使用 - IMPORT FOREIGN SCHEMA语句时,建议您添加- LIMIT TO限制,并使用括号将需要添加限制的表名称括起来。如果不添加该限制,系统则将目标MaxCompute工作空间中的所有表批量创建至Hologres中。
- 仅Hologres V1.1.26及以上版本支持对使用 - IMPORT FOREIGN SCHEMA创建的外部表名称增加前缀和后缀,如果您的实例是V1.1.26以下版本,请您使用常见升级准备失败报错或加入Hologres钉钉交流群反馈,详情请参见如何获取更多的在线支持?。
- 仅Hologres V1.3及以上版本支持MaxCompute的三层模型模式(即在原先的Project和Table之间增加了一层Schema的概念),更多描述请参见Schema操作。如果您想在Hologres中使用MaxCompute的三层模型的项目创建外部表,且您的Hologres版本较低,请您使用常见升级准备失败报错或加入Hologres钉钉交流群反馈,详情请参见如何获取更多的在线支持?。 
命令格式
在Hologres中批量创建外部表的命令格式如下。
IMPORT FOREIGN SCHEMA remote_schema
    [ { LIMIT TO | EXCEPT } ( table_name [, ...] ) ]
    FROM SERVER odps_server
    INTO local_schema 
    [ OPTIONS ( option 'value' [, ... ] ) ]参数说明
参数说明如下表所示。
| 参数 | 描述 | 
| remote_schema | 
 | 
| table_name | 需要导入的MaxCompute表名称。 | 
| server_name | MaxCompute表所在的外部服务器名称,默认为odps_server。 您可以直接调用Hologres底层已创建的名为odps_server的外部表服务器。详细原理请参见Postgres FDW。 | 
| local_schema | Hologres外部表所在的Schema名(如public)。 | 
| options | Hologres支持如下四个option: 
 | 
Hologres仅支持创建MaxCompute外部表。新建的外部表名称需要同MaxCompute表的名称一致。
使用示例
- MaxCompute两层模型。 - 示例选取MaxCompute公共数据集public_data中的表,在Hologres中批量创建外部表。您可以参照使用公开数据集描述,登录并查询数据集 。 - 示例1:为public Schema新建一张外部表,若表存在则更新表。 - IMPORT FOREIGN SCHEMA public_data LIMIT TO (customer) FROM server odps_server INTO PUBLIC options(if_table_exist 'update');
- 示例2:为public Schema批量新建外部表。 - IMPORT FOREIGN SCHEMA public_data LIMIT TO( customer, customer_address, customer_demographics, inventory,item, date_dim, warehouse) FROM server odps_server INTO PUBLIC options(if_table_exist 'update');
- 示例3:新建一个testdemo Schema并批量新建外部表。 - CREATE schema testdemo; IMPORT FOREIGN SCHEMA public_data LIMIT TO( customer, customer_address, customer_demographics, inventory,item, date_dim, warehouse) FROM server odps_server INTO testdemo options(if_table_exist 'update'); SET search_path TO testdemo;
- 示例4:在public Schema批量创建外部表,已有外表则报错。 - IMPORT FOREIGN SCHEMA public_data LIMIT to (customer, customer_address) FROM server odps_server INTO PUBLIC options(if_table_exist 'error');
- 示例5:在public Schema中批量创建外部表,已有外表则跳过该外部表。 - IMPORT FOREIGN SCHEMA public_data LIMIT to (customer, customer_address) FROM server odps_server INTO PUBLIC options(if_table_exist 'ignore');
 
- MaxCompute三层模型。 - 基于MaxCompute - odps_hologres项目的- tpch_10g这个Schema中的- odps_region_10g表创建Hologres中的外部表。- IMPORT FOREIGN SCHEMA "odps_hologres#tpch_10g" LIMIT to ( odps_region_10g ) FROM SERVER odps_server INTO public OPTIONS(if_table_exist 'error',if_unsupported_type 'error');