Help Center > > Developer Guide> SQL Reference> SQL Syntax> CREATE FOREIGN TABLE (for GDS Import and Export)

CREATE FOREIGN TABLE (for GDS Import and Export)

Updated at: Jul 15, 2020 GMT+08:00

CREATE FOREIGN TABLE creates a GDS foreign table.

Function

CREATE FOREIGN TABLE creates a GDS foreign table in the current database for concurrent data import and export. The GDS foreign table can be read-only or write-only, used for concurrent data import and export, respectively. The OBS foreign table is read-only by default.

Precautions

  • The foreign table is owned by the user who runs the command.
  • The distribution mode of a GDS foreign table does not need to be explicitly specified. The default is ROUNDROBIN.
  • All constraints (including column and row constraints) are invalid to the GDS foreign table.

Syntax

1
2
3
4
5
6
7
8
9
CREATE FOREIGN TABLE [ IF NOT EXISTS  ] table_name 
    ( [  { column_name type_name POSITION(offset,length) | LIKE source_table } [, ...]  ] ) 
    SERVER gsmpp_server 
    OPTIONS (  { option_name ' value '  }  [, ...] ) 
    [  { WRITE ONLY  |  READ ONLY  }] 
    [ WITH error_table_name | LOG INTO error_table_name] 
    [REMOTE LOG 'name'] 
    [PER NODE REJECT LIMIT 'value']
    [ TO { GROUP groupname | NODE ( nodename [, ... ] ) } ];

Parameter Overview

CREATE FOREIGN TABLE provides multiple parameters, which are classified as follows:

Parameter Description

  • IF NOT EXISTS

    Does not throw an error if a table with the same name already exists. A notice is issued in this case.

  • table_name

    Specifies the name of the foreign table to be created.

    Value range: a string. It must comply with the naming convention rule.

  • column_name

    Specifies the name of a column in the foreign table.

    Value range: a string. It must comply with the naming convention rule.

  • type_name

    Specifies the data type of the column.

  • POSITION(offset,length)

    Defining the location of each column in the data file in fixed length mode.

    offset is the start of the column in the source file, and length is the length of the column.

    Value range: offset must be greater than 0 bytes, and its unit is byte.

    The length of each record must be less than 1 GB. By default, columns not in the file are replaced with null.

  • SERVER gsmpp_server

    Specifies the server name of the foreign table. For the GDS foreign table, its server is created by initial database, which is gsmpp_server.

  • OPTIONS ( { option_name ' value ' } [, ...] )

    Specifies all types of parameters of foreign table data.

    • location

      Specifies the data source location of the foreign table, which can be expressed through URLs. Separate URLs with vertical bars (|).

      Currently, GDS can automatically create a directory defined by a foreign table during data export. For example, when the foreign table location defines that gsfs:// 192.168.0.91:5000/2019/09 executes an export task, if the 2019/09 subdirectory in the GDS data directory does not exist, the subdirectory is automatically created. You do not need to manually create the directory specified in the foreign table.

      • For a read-only foreign table imported by GDS from a remote server in parallel, its URL must end with its corresponding schema or file name. (Read-only is the default file attribute.)

        For example: gsfs://192.168.0.90:5000/* or file:///data/data.txt or gsfs:// 192.168.0.90:5000/* | gsfs:// 192.168.0.91:5000/*.

      • For a writable foreign table exported by GDS to a remote server in parallel, its URL does not need to contain a file name. If the data source location is a remote URL, for example, gsfs:// 192.168.0.90:5000/, multiple data sources can be specified. If the number of exported data file locations is less than or equal to the number of DNs, when you use the foreign table for export, data is evenly distributed to each data source location. If the number of exported data file locations is greater than the number of DNs, when you export data, the data is evenly distributed to data source locations corresponding to the DNs. Blank data files are created on the excess data source locations.
      • For a read-only foreign table imported by GDS from a remote server in parallel, the number of URLs must be less than the number of DNs, and URLs in the same location cannot be used.
      • If the URL begins with gsfss://, data is imported and exported in encryption mode, and DOP cannot exceed 10.
      • During GDS export, the 2019/09 subdirectory in THE gsfs://127.0.0.1:7789/2019/09/ directory specified by the location table is automatically created when the export task is executed.
    • format

      Specifies the format of the data source file in a foreign table.

      Value range: CSV, TEXT, or FIXED. The default value is TEXT. DWS only supports CSV and TEXT formats.

      • In CSV files, escape sequences are processed as common strings. Therefore, linefeeds are processed as data.
      • In TEXT files, escape sequences are processed as they are. Therefore, linefeeds are not processed as data.
      • The FIXED file can process newline characters in data columns efficiently, but cannot process special characters very well.
      • An escape sequence is a string starting with a backslash (\), including \b (backspace), \f (formfeed page break), \n (new line), \r (carriage return), \t (horizontal tab), \v (vertical tab), \number (octal number), and \xnumber (hexadecimal number). In TEXT files, strings are processed as they are. In files of other formats, strings are processed as data.
      • FIXED is defined as follows: (POSITION must be specified for each column when FIXED is used.)
        1. The column length of each record is the same.
        2. Spaces are used for column padding. Left padding is used for the numeric type and right padding is used for the char type.
        3. No delimiters are used between columns.
    • header

      Specifies whether a data file contains a table header. header is available only for CSV and FIXED files.

      When data is imported, if header is on, the first row of the data file will be identified as title row and ignored. If header is off, the first row is identified as data.

      When data is exported, if header is on, ▪fileheader must be specified. fileheader is used to specify the export header file format. If header is off, the exported file does not include a title row.

      Value range: true, on, false, and off. The default value is false or off.

    • fileheader

      Specifies a file that defines the content in the header for exported data. The file contains one row of data description of each column.

      For example, to add a header in a file containing product information, define the file as follows:

      The information of products.\n

      • This parameter is available only when header is on or true. The file must be prepared in advance.
      • In Remote mode, the definition file must be put to the working directory of GDS (the -d directory specified when starting the GDS).
      • The definition file can contain only one row of title information, and end with a newline character. Excess rows will be discarded. (Title information cannot contain newline character).
      • The length of the definition file including the newline character cannot exceed 1 MB.
    • out_filename_prefix

      Specifies the name prefix of the exported data file exported using GDS from a write-only foreign table.

      • The prefix of the specified file name must be valid and compliant with the restrictions of the file system in the physical environment where the GDS is deployed. Otherwise, the file will fail to be created.
        • The file name prefix can contain only lowercase letters, uppercase letters, digits, and underscores (_).
        • The prefix of the specified export file name cannot contain feature fields reserved for the Windows and Linux OS, including but not limited to:

          "con","aux","nul","prn","com0","com1","com2","com3","com4","com5","com6","com7","com8","com9","lpt0","lpt1","lpt2","lpt3","lpt4","lpt5","lpt6","lpt7","lpt8","lpt9"

        • The total length of the absolute path consisting of the exported file prefix, the path specified by gds –d, and .dat.x should be as required by the file system where GDS is deployed.
        • It is required that the prefix can be correctly parsed and identified by the receiver (including but not limited to the original database where it was exported) of the data file. Identify and modify the option that causes the file name resolution problem (if any).
      • To concurrently perform export jobs, do not use the same file name prefix for them. Otherwise, the exported files may overwrite each other or be lost in the OS or file system.
    • delimiter

      Specifies the column delimiter of data, and uses the default delimiter if it is not set. The default delimiter of TEXT is a tab and that of CSV is a comma (,). No delimiter is used in FIXED format.

      • A delimiter cannot be \r or \n.
      • A delimiter cannot be the same as the null value. The delimiter of CSV cannot be same as the quote value.
      • The delimiter for the TEXT format data cannot contain any of the following characters: \.abcdefghijklmnopqrstuvwxyz0123456789.
      • The data length of a single row should be less than 1 GB. If the delimiters are too long and there are too many rows, the length of valid data will be affected.
      • You are advised to use a multi-character, such as the combination of the dollar sign ($), caret (^), the ampersand (&), or invisible characters, such as 0x07, 0x08, and 0x1b as the delimiter.

      Valid value:

      The value of delimiter can be a multi-character delimiter whose length is less than or equal to 10 bytes.

    • quote

      Specifies which characters in a CSV source data file will be identified as quotation marks. The default value is a double quotation mark (").

      • The quote parameter cannot be the same as the delimiter or null parameter.
      • The quote parameter must be a single-byte character.
      • Invisible characters are recommended as quote values, such as 0x07, 0x08, and 0x1b.
    • escape

      Specifies which characters in a CSV source data file are escape characters. Escape characters can only be single-byte characters.

      The default value is a double quotation mark ("). If it is the same as the value of quote, it will be replaced with \0.

    • null
      Specifies the string that represents a null value.
      • The null value cannot be \r or \n. The maximum length is 100 characters.
      • The null value cannot be the same as the delimiter or quote parameter.

      Valid value:

      • The default value is \n for the TEXT format.
      • The default value for the CSV format is an empty string without quotation marks.
    • noescaping

      Specifies in TEXT format, whether to escape the backslash (\) and its following characters.

      noescaping is available only for the TEXT format.

      Value range: true, on, false, and off. The default value is false or off.

    • encoding

      Specifies the encoding of a data file, that is, the encoding used to parse, check, and generate a data file. Its default value is the default client_encoding value of the current database.

      Before you import foreign tables, it is recommended that you set client_encoding to the file encoding format, or a format matching the character set of the file. Otherwise, unnecessary parsing and check errors may occur, leading to import errors, rollback, or even invalid data import. Before you import foreign tables, you are also advised to specify this parameter, because the export result using the default character set may not be what you expected.

      If this parameter is not specified when you create a foreign table, a warning message will be displayed on the client.

      Currently, GDS cannot parse or write in a file using multiple encoding formats during foreign table import or export.

    • fill_missing_fields

      Specifies whether to generate an error message when the last column in a row in the source file is lost during data import.

      Value range: true, on, false, and off. The default value is false or off.

      • If this parameter is set to true or on and the last column of a data row in a data source file is lost, the column will be replaced with NULL and no error message will be generated.
      • If this parameter is set to false or off and the last column is missing, the following error information will be displayed:
        missing data for column "tt"
    • ignore_extra_data

      Specifies whether to ignore excessive columns when the number of data source files exceeds the number of foreign table columns. This parameter is available during data import.

      Value range: true, on, false, and off. The default value is false or off.

      • If this parameter is set to true or on and the number of data source files exceeds the number of foreign table columns, excessive columns will be ignored.
      • If this parameter is set to false or off and the number of data source files exceeds the number of foreign table columns, the following error information will be displayed:
        extra data after last expected column

      If the newline character at the end of the row is lost, setting the parameter to true will ignore data in the next row.

    • reject_limit

      Specifies the maximum number of data format errors allowed during a data import task. If the number of errors does not reach the maximum number, the data import task can still be executed.

      You are advised to replace this syntax with PER NODE REJECT LIMIT 'value'.

      Examples of data format errors include the following: a column is lost, an extra column exists, a data type is incorrect, and encoding is incorrect. Once a non-data format error occurs, the whole data import process is stopped.

      Value range: a positive integer or unlimited

      The default value is 0, indicating that error information is returned immediately.

      Enclose positive integer values with single quotation marks ('').

    • mode

      Specifies the data import policy during a specific data import process. DWS supports only the Normal mode.

      Valid value:

      • Normal (default): supports all file types (CSV, TEXT, FIXED). Enabling Gauss data service to help data import.
    • eol

      Specifies the newline character style of the imported or exported data file.

      Value range: multi-character newline characters within 10 bytes. Common newline characters include \r (0x0D), \n (0x0A), and \r\n (0x0D0A). Special newline characters include $ and #.

      • The eol parameter supports only the TEXT format for data import and export and does not support the CSV or FIXED format for data import. For forward compatibility, the eol parameter can be set to 0x0D or 0x0D0A for data export in the CSV and FIXED formats.
      • The value of the eol parameter cannot be the same as that of DELIMITER or NULL.
      • The eol parameter value cannot contain lowercase letters, digits, or dot (.).
    • conflict_delimiter

      This parameter is generally used with the compatible_illegal_chars parameter. If a data file contains a truncated Chinese character, the truncated character and a delimiter will be encoded into another Chinese character due to inconsistent encoding between the foreign table and the database. As a result, the delimiter is masked and an error will be reported, indicating that there are missing fields.

      This parameter is used to avoid encoding a truncated character and a delimiter into another character.

      Value range: true, on, false, and off. The default value is false or off.

      • If the parameter is set to true or on, encoding a truncated character and a delimiter into another character is allowed.
      • If the parameter is set to false or off, encoding a truncated character and a delimiter into another character is not allowed.

      This parameter is disabled by default. It is recommended that you disable this parameter, because encoding a truncated character and a delimiter into another character is rarely required. If the parameter is enabled, the scenario may be incorrectly identified and thereby causing incorrect information imported to the table.

      For example, a user wants to import a row of GBK data 3|+|+|20191212 to the UTF8 database, and the user-defined delimiter is |+|.

      The GBK data maps to the hexadecimal system as follows:

      33 7C 2B 7C C4 7C 2B 7C 323031393132313

      ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

      3 | + | ** | + | 20191212

      When the GBK data is imported to UTF8 database, the database performs the following conversions:

      GBK -> UTF8

      33->33 (3)

      7C->7C (|)

      2B->2B (+)

      7C->7C(|)

      C47C -> E886A2(膢)

      2B->2B(+)

      7C->7C(|)

      323031393132313-> 20191212

      The user-defined delimiter is |+|. Therefore, the data obtained is 3 and +|20191212.

      Instead, the user actually wants to obtain 3, ?, and 20191212.

      is actually a truncated character that is not expected by users. It conflicts with | in the user-defined delimiter |+|. As a result, the data is garbled or the import fails.

      If you want to avoid this issue, use conflict_delimiter together with compatible_illegal_chars to replace C4 with a question mark (?), and then import to the UTF8 database.

    • fix

      Specifies the length of fixed format data. The unit is byte. This syntax is available only for READ ONLY foreign tables.

      Value range: Less than 1 GB, and greater than or equal to the total length specified by POSITION (The total length is the sum of offset and length in the last column of the table definition.)

    • out_fix_alignment

      Specifies how the columns of the types BYTEAOID, CHAROID, NAMEOID, TEXTOID, BPCHAROID, VARCHAROID, NVARCHAR2OID, and CSTRINGOID are aligned during fixed-length export.

      Value range: align_left, align_right

      Default value: align_right

      The bytea data type must be in hexadecimal format (for example, \XXXX) or octal format (for example, \XXX\XXX\XXX). The data to be imported must be left-aligned (that is, the column data starts with either of the two formats instead of spaces). Therefore, if the exported file needs to be imported using a GDS foreign table and the file data length is less than that specified by the foreign table formatter, the exported file must be left aligned. Otherwise, an error is reported during the import.

    • date_format

      Imports data of the DATE type. This syntax is available only for READ ONLY foreign tables.

      Value range: any valid DATE value. For details, see Date and Time Processing Functions and Operators.

      If ORACLE is specified as the compatible database, the DATE format is TIMESTAMP. For details, see timestamp_format below.

    • time_format

      Imports data of the TIME type. This syntax is available only for READ ONLY foreign tables.

      Value range: any valid TIME value. Time zones cannot be used. For details, see Date and Time Processing Functions and Operators.

    • timestamp_format

      Imports data of the TIMESTAMP type. This syntax is available only for READ ONLY foreign tables.

      Value range: any valid TIMESTAMP value. Time zones are not supported. For details, see Date and Time Processing Functions and Operators.

    • smalldatetime_format

      Imports data of the SMALLDATETIME type. This syntax is available only for READ ONLY foreign tables.

      Value range: any valid SMALLDATETIME value. For details, see Date and Time Processing Functions and Operators.

    • compatible_illegal_chars

      Enables or disables fault tolerance on invalid characters during data import. This syntax is available only for READ ONLY foreign tables.

      Value range: true, on, false, and off. The default value is false or off.

      • If this parameter is set to true or on, invalid characters are tolerated and imported to the database after conversion.
      • If this parameter is set to false or off and an error occurs when there are invalid characters, the import will be interrupted.

      The rule of error tolerance when you import invalid characters is as follows:

      • \0 is converted to a space.
      • Other invalid characters are converted to question marks.
      • If compatible_illegal_chars is set to true or on, invalid characters are tolerated. If NULL, DELIMITER, QUOTE, and ESCAPE are set to spaces or question marks, errors like "illegal chars conversion may confuse COPY escape 0x20" will be displayed to prompt users to modify parameter values that cause confusion, preventing import errors.
  • READ ONLY

    Specifies whether a foreign table is read-only. This parameter is available only for data import.

  • WRITE ONLY

    Specifies whether a foreign table is write-only. This parameter is available only for data import.

  • WITH error_table_name

    Specifies the table where data format errors generated during parallel data import are recorded. You can query the error information table after data is imported to obtain error details. This parameter is available only after reject_limit is set.

    To be compatible with PostgreSQL open source interfaces, you are advised to replace this syntax with LOG INTO.

    Value range: a string. It must comply with the naming convention rule.

  • LOG INTO error_table_name

    Specifies the table where data format errors generated during parallel data import are recorded. You can query the error information table after data is imported to obtain error details.

    This parameter is available only after PER NODE REJECT LIMIT is set.

    Value range: a string. It must comply with the naming convention rule.

  • REMOTE LOG 'name'

    The data format error information is saved as files in GDS. name is the prefix of the error data file.

  • PER NODE REJECT LIMIT 'value'

    This parameter specifies the allowed number of data format errors on each DN during data import. If the number of errors exceeds the specified value on any DN, data import fails, an error is reported, and the system exits data import.

    This syntax specifies the error tolerance of a single node.

    Examples of data format errors include the following: a column is lost, an extra column exists, a data type is incorrect, and encoding is incorrect. When a non-data format error occurs, the whole data import process stops.

    Value range: integer, unlimited. The default value is 0, indicating that error information is returned immediately.

  • TO { GROUP groupname | NODE ( nodename [, ... ] ) }

    Currently, TO GROUP cannot be used. TO NODE is used for internal scale-out tools.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
-- Create a foreign table to import data from GDS servers 192.168.0.90 and 192.168.0.91 in TEXT format. Record errors that occur during data import in err_HR_staffS.
CREATE FOREIGN TABLE foreign_HR_staffS
(
  staff_ID       NUMBER(6) ,
  FIRST_NAME     VARCHAR2(20),
  LAST_NAME      VARCHAR2(25),
  EMAIL          VARCHAR2(25),
  PHONE_NUMBER   VARCHAR2(20),
  HIRE_DATE      DATE,
  employment_ID  VARCHAR2(10),
  SALARY         NUMBER(8,2),
  COMMISSION_PCT NUMBER(2,2),
  MANAGER_ID     NUMBER(6),
  section_ID  NUMBER(4)
) SERVER gsmpp_server OPTIONS (location 'gsfs://192.168.0.90:5000/* | gsfs://192.168.0.91:5000/*', format 'TEXT', delimiter E'\x08',  null '') WITH err_HR_staffS;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
-- Create a foreign table to import data from GDS servers 192.168.0.90 and 192.168.0.91 in TEXT format and record error messages in the import process to the err_HR_staffS table. A maximum of two data format errors are allowed during the import.
CREATE FOREIGN TABLE foreign_HR_staffS_ft3
(
  staff_ID       NUMBER(6) ,
  FIRST_NAME     VARCHAR2(20),
  LAST_NAME      VARCHAR2(25),
  EMAIL          VARCHAR2(25),
  PHONE_NUMBER   VARCHAR2(20),
  HIRE_DATE      DATE,
  employment_ID  VARCHAR2(10),
  SALARY         NUMBER(8,2),
  COMMISSION_PCT NUMBER(2,2),
  MANAGER_ID     NUMBER(6),
  section_ID  NUMBER(4)
) SERVER gsmpp_server OPTIONS (location 'gsfs://192.168.0.90:5000/* | gsfs://192.168.0.91:5000/*', format 'TEXT', delimiter E'\x08',  null '',reject_limit '2') WITH err_HR_staffS_ft3;

1
2
3
-- Delete the foreign table:
DROP FOREIGN TABLE foreign_HR_staffS;
DROP FOREIGN TABLE foreign_HR_staffS_ft3;

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel