更新时间:2024-03-06 GMT+08:00
分享

创建外表

当完成创建外部服务器后,在GaussDB(DWS)数据库中创建一个OBS外表,用来访问存储在OBS上的数据。OBS外表是只读的,只能用于查询操作,可直接使用SELECT查询其数据。

创建外表

创建外表的语法格式如下,详细的描述请参见CREATE FOREIGN TABLE (SQL on Hadoop or OBS)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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"的外表,对语法中的参数按如下描述进行设置:

  • table_name

    外表的表名。

  • 表字段定义
    • column_name:外表中的字段名。
    • type_name:字段的数据类型。

    多个字段用“,”隔开。

    外表的字段个数和字段类型,需要与OBS上保存的数据完全一致。

  • SERVER dfs_server

    外表的外部服务器名称,这个server必须存在。外表通过设置外部服务器连接OBS读取数据。

    此处应填写为参照创建外部服务器创建的外部服务器名称。

  • OPTIONS参数

    用于指定外表数据的各类参数,关键参数如下所示。

    • “format”:表示对应的OBS服务上的文件格式,支持“orc”、“carbondata”格式。
    • “foldername”:必选参数。数据源文件的OBS路径,此处仅需要填写“/桶名/文件夹目录层级/”。

      可以先通过OBS上的数据准备中的2获取数据源文件的完整的OBS路径,该路径为OBS服务的终端节点(Endpoint)。

    • “totalrows”:可选参数。该参数不是导入的总行数。由于OBS上文件可能很多,执行analyze可能会很慢,通过“totalrows”参数,让用户来设置一个预估的值,使优化器能通过这个值做大小表的估计。一般预估值与实际值的数量级差不多时,查询效率较高。
    • “encoding”:外表中数据源文件的编码格式名称,缺省为utf8。对于OBS外表此参数为必选项。
  • DISTRIBUTE BY

    这个子句是必须的,对于OBS外表,当前只支持ROUNDROBIN分布方式。

    表示外表在从数据源读取数据时,GaussDB(DWS)集群每一个节点随机读取一部分数据,并组成完整数据。

  • 语法中的其他参数

    其他参数均为可选参数,用户可以根据自己的需求进行设置,在本例中不需要设置。

根据以上信息,创建外表命令如下所示:

建立不包含分区列的OBS外表,表关联的外部服务器为obs_server,表对应的OBS服务上的文件格式为‘orc’,OBS上的数据存储路径为'/mybucket/data/'。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
分享:

    相关文档

    相关产品