Creating a Foreign Table
After operations in Creating a Foreign Server are complete, create an HDFS write-only foreign table in the DWS database to access data stored in HDFS. The foreign table is write-only and can be used only for data export.
1 2 3 4 5 6 7 8 9 10 11 | 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 ' } [, ...] ) [ {WRITE ONLY }] DISTRIBUTE BY {ROUNDROBIN | REPLICATION} [ PARTITION BY ( column_name ) [ AUTOMAPPED ] ] ; |
For example, when creating a foreign table named product_info_ext_obs, configure the parameters in the syntax as described in Table 1.
| Parameter | Description | Example |
|---|---|---|
| table_name | Specifies the name of the foreign table. | product_info_ext_obs |
| column_name | Specifies the column name in the foreign table. Multiple columns are separated by commas (,). The number of columns and column types in the foreign table must be the same as those in the data stored on OBS. | product_price |
| type_name | Specifies the data type of the column. | integer |
| SERVER dfs_server | Specifies the foreign server name of the foreign table. This server must exist. The foreign table connects to OBS/HDFS to read data through the foreign server. Enter the name of the foreign server created in Creating a Foreign Server. | hdfs_server |
| OPTIONS | Specifies various parameters of the foreign table data. The key parameters are as follows:
| format 'orc' |
| Other parameters in the syntax | Other parameters are optional and can be configured as required. In this example, they do not need to be configured. For details, see "CREATE FOREIGN TABLE (SQL on Hadoop or OBS)" in Data Warehouse Service (DWS) SQL Syntax Reference. | - |
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 an HDFS foreign table that does not contain partition columns. The foreign server associated with the table is hdfs_server, the format of the file on HDFS corresponding to the table is ORC, and the data storage path on HDFS is /user/hive/warehouse/product_info_orc/. CREATE FOREIGN TABLE product_info_ext_obs ( product_price integer , product_id char(30) , 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 hdfs_server OPTIONS ( format 'orc', foldername '/user/hive/warehouse/product_info_orc/', compression 'snappy', version '0.12' ) Write Only; |