Updated on 2024-10-14 GMT+08:00

Creating a GDS Foreign Table

Source data information and GDS access information are configured in a foreign table. Then, GaussDB can import data from a data server to a database table based on the configuration in the foreign table.

Procedure

  1. Collect source data information and GDS access information.

    You need to collect the following source data information:

    • format: CSV, TEXT, and FIXED are supported. Check the format of data to import, for example, CSV format.
    • header: whether a source data file has a header. This parameter is set only for files in CSV or FIXED format.
    • delimiter: delimiter in the source data file, for example, comma (,).
    • encoding: encoding format of the source data file, for example, UTF-8.
    • eol: line break character in the data file. It can be a default character, such as 0x0D0A or 0X0A, or a customized line break character such as a string !@#. This parameter can be set only for TEXT import.
    • For details on more source data information configured in a foreign table, see data format parameters.

    You need to collect the following GDS access information:

    location: GDS URL. GDS information in Installing, Configuring, and Starting GDS is used as an example. In non-SSL mode, location is set to gsfs://192.168.0.90:5000//input_data/. In SSL mode, location is set to gsfss://192.168.0.90:5000//input_data/. 192.168.0.90:5000 is the IP address and port number of GDS. input_data is the directory for storing source data files on the data server. Replace the values as required.

  2. Design an error tolerance mechanism for data import.

    GaussDB supports the following error tolerance in data import:
    • fill_missing_fields: This parameter specifies whether to report an error when the last column in a row of the source data file is empty, or to fill the column with null.
    • ignore_extra_data: When the number of columns in the source data file is greater than that specified in the foreign table, this parameter specifies whether to report an error or ignore the extra columns.
    • per node reject_limit: This parameter specifies the number of data format errors allowed on each DN. If the number of errors recorded in the error table on a DN exceeds the specified value, the import fails and an error message is reported. You can also set it to unlimited.
    • compatible_illegal_chars: When an illegal character is encountered, this parameter specifies whether to import an error, or convert it and proceed with the import.

      The following describes the rules for converting an illegal character:

      • \0 is converted to a space.
      • Other illegal characters are converted to question marks.
      • If NULL, DELIMITER, QUOTE, or ESCAPE is also set to a space or question mark, GaussDB displays an error message, such as "illegal chars conversion may confuse COPY escape 0x20", to prompt you to modify parameter settings that may cause import errors.
    • error_table_name: This parameter specifies the name of the table that records data format errors. After the parallel import, you can query the table for error details.
    • remote log 'name': This parameter specifies whether to store data format errors in files on the GDS server. name is the prefix of the error data file.
    • For details on more error tolerance parameters, see error tolerance parameters.

  3. After connecting to the database using gsql or Data Studio, create a GDS foreign table based on the collected and design information.

    The command is as follows:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons
    (
      r_reason_sk integer not null,
      r_reason_id char(16) not null,
      r_reason_desc char(100)
    )
     SERVER gsmpp_server
     OPTIONS 
    (
    LOCATION 'gsfs://192.168.0.90:5000/input_data | gsfs://192.168.0.91:5000/input_data', 
    FORMAT 'CSV' ,
    DELIMITER ',',
    ENCODING 'utf8',
    HEADER 'false',
    FILL_MISSING_FIELDS 'true',
    IGNORE_EXTRA_DATA 'true'
    )
    LOG INTO product_info_err 
    PER NODE REJECT LIMIT 'unlimited';
    

    The following describes information in this example:

    • The columns specified in the foreign table must be the same as those in the target table.
    • Retain the value gsmpp_server for SERVER.
    • Set location based on the GDS access information collected in 1. If SSL is used, replace gsfs with gsfss.
    • Set FORMAT, DELIMITER, ENCODING, and HEADER based on the source data information collected in 1.
    • Set FILL_MISSING_FIELDS, IGNORE_EXTRA_DATA, LOG INTO, and PER NODE REJECT LIMIT based on the error tolerance mechanism designed in 2. LOG INTO specifies the name of the error table.

    For details on the CREATE FOREIGN TABLE syntax, see CREATE FOREIGN TABLE (for Import and Export).

Examples

For more examples, see Examples.

  • Example 1: Create a GDS foreign table named foreign_tpcds_reasons. The data format is CSV.
    1
    2
    3
    4
    5
    6
    7
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons
    (
      r_reason_sk integer not null,
      r_reason_id char(16) not null,
      r_reason_desc char(100)
    )
     SERVER gsmpp_server OPTIONS (location 'gsfs://192.168.0.90:5000/* | gsfs://192.168.0.91:5000/*', FORMAT 'CSV',MODE 'Normal', ENCODING 'utf8', DELIMITER E'\x20', QUOTE E'\x1b', NULL '');
    
  • Example 2: Create a GDS foreign table named foreign_tpcds_reasons_SSL. SSL is used and the data format is CSV.
    1
    2
    3
    4
    5
    6
    7
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons_SSL
    (
      r_reason_sk integer not null,
      r_reason_id char(16) not null,
      r_reason_desc char(100)
    )
     SERVER gsmpp_server OPTIONS (location 'gsfss://192.168.0.90:5000/* | gsfss://192.168.0.91:5000/*', FORMAT 'CSV',MODE 'Normal', ENCODING 'utf8', DELIMITER E'\x20', QUOTE E'\x1b', NULL '');
    
  • Example 3: Create a GDS foreign table named foreign_tpcds_reasons. The data format is TEXT.
    1
    2
    3
    4
    5
    6
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons
    (
      r_reason_sk integer not null,
      r_reason_id char(16) not null,
      r_reason_desc char(100)
    ) SERVER gsmpp_server OPTIONS (location 'gsfs://192.168.0.90:5000/* | gsfs://192.168.0.91:5000/*', FORMAT 'TEXT', delimiter E'\x20',  null '',reject_limit '2',EOL '0x0D') WITH err_foreign_tpcds_reasons;
    
  • Example 4: Create a GDS foreign table named foreign_tpcds_reasons. The data format is FIXED.
    1
    2
    3
    4
    5
    6
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons
    (
      r_reason_sk       integer      position(1,2),
      r_reason_id       char(16)     position(3,16),
      r_reason_desc     char(100)    position(19,100)
    ) SERVER gsmpp_server OPTIONS (location 'gsfs://192.168.0.90:5000/*', FORMAT 'FIXED', ENCODING 'utf8',FIX '119');