Updated on 2023-10-23 GMT+08:00

Example 3: Importing Data in Private Mode

A cluster has four hosts and eight primary DNs and every two primary DNs reside on the same host. There are eight data files to import and the size of each file is 50 MB. The files are in CSV format.
  1. Log in as the OS user omm to the host where the CN is located.
  2. Run the following command to connect to the database:
    gsql -d postgres -p 8000

    postgres is the name of the database, and 8000 is the port number of the CN.

    If information similar to the following is displayed, the connection succeeds:

    gsql((GaussDB Kernel VxxxRxxxCxx build f521c606) compiled at 2021-09-16 14:55:22 commit 2935 last mr 6385 release)
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    Type "help" for help.
    
    openGauss=# 
  3. Query DN names on each node.
    1
    openGauss=# SELECT node_name,node_host FROM pgxc_node WHERE node_type='D';
    

    Example:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    openGauss=# SELECT node_name,node_host FROM pgxc_node WHERE node_type='D';
      node_name   |   node_host    
    --------------+----------------
     dn_6001_6002 | 192.168.0.11
     dn_6003_6004 | 192.168.0.11
     dn_6005_6006 | 192.168.0.12
     dn_6007_6008 | 192.168.0.12
     dn_6009_6010 | 192.168.0.13
     dn_6011_6012 | 192.168.0.13
     dn_6013_6014 | 192.168.0.14
     dn_6015_6016 | 192.168.0.14
    (8 rows)
    
  4. Upload the source data files to the nodes where DNs are located.
    1. Log in to each cluster node as a common user. Create the /input_data directory to store data files and create sub-directories named after DNs on each node.

      The following uses the node with the IP address 192.168.0.11 which is queried in 3 as an example. Two DNs, dn_6001_6002 and dn_6003_6004, are on the node.

      mkdir -p /input_data
      mkdir -p /input_data/dn_6001_6002
      mkdir -p /input_data/dn_6003_6004
    2. Evenly distribute source data files to the sub-directories created on each node.
    3. Change the owner of data source files to be imported and the /input_data directory on each cluster node to omm.
      chown -R omm:dbgrp /input_data
  5. Create the target table reasons.
    1
    2
    3
    4
    5
    6
    openGauss=# CREATE TABLE reasons
    (
      r_reason_sk integer not null,
      r_reason_id char(16) not null,
      r_reason_desc char(100)
    );
    
  6. Create the foreign table foreign_tpcds_reasons for the source data.

    Set import mode parameters as follows:

    • Set the import mode to Private.
    • The source data files are stored in sub-directories named after DNs on the nodes, and can be locally accessed. Therefore, set location to file:///input_data/*.

    Information about the data format is set based on data format parameters specified during data export. The parameter settings are as follows:

    • format is set to CSV.
    • delimiter is set to a comma (,).
    • quote is set to 0x1b.
    • null is set to an empty string without quotation marks.

    Set import error tolerance parameters as follows:

    • Set PER NODE REJECT LIMIT (number of allowed data format errors) to unlimited. In this case, all the data format errors detected during data import will be tolerated.
    • Set LOG INTO to err_tpcds_reasons. The data format errors detected during data import will be recorded in the err_tpcds_reasons table.
    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 'file:///input_data/*', format 'CSV', mode 'private', delimiter ',', quote E'\x1b', null '')LOG INTO err_tpcds_reasonS PER NODE REJECT LIMIT 'unlimited';
    
  7. Import data to the reasons table.
    1
    openGauss=# INSERT INTO reasons SELECT * FROM foreign_tpcds_reasons;
    
  8. Query data import errors in the err_tpcds_reasons table and rectify the errors (if any). For details, see Handling Import Errors.
    1
    openGauss=# SELECT * FROM err_tpcds_reasons;