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

Examples

Example: Exporting Data in Remote Mode

A data server resides on the same intranet as the cluster, the IP address of the data server is 192.168.0.90, and data files are in CSV format. In this scenario, data is concurrently exported in Remote mode.

To concurrently export data in Remote mode, perform the following operations:

  1. (Optional) Create a user and its user group. The user is used to start GDS. If the user and user group already exist, skip this step.
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
  2. Switch to user gds_user, create the /output_data directory for storing data files, and start user gds_user and its user group.
    su - gds_user
    mkdir -p /output_data
  3. Change the owner of the /output_data directory on the data server to gds_user.
    chown -R gds_user:gdsgrp /output_data 
  4. Log in to the data server as user gds_user and start GDS.
    The GDS installation path is /opt/bin/gds. Exported data files are stored in /output_data/. The IP address of the data server is 192.168.0.90. The GDS monitoring port number is 5000. The GDS runs in the background.
    /opt/bin/gds/gds -d /output_data -p 192.168.0.90:5000 -H 10.10.0.1/24 -D
  5. In the database, create the foreign table foreign_tpcds_reasons for receiving data from the data server.

    Data export mode settings are as follows:

    • The directory for storing exported files is set to /output_data/ and the GDS listening port number is 5000 while GDS is started. The created directory for storing exported files is /output_data/. Therefore, set the location parameter to gsfs://192.168.0.90:5000/.

    Data format parameter settings are as follows:

    • format is set to CSV.
    • encoding is set to UTF-8.
    • delimiter is set to E'\x20'.
    • quote is set to 0x1b.
    • null is set to an empty string without quotation marks.
    • escape is set to a double quotation mark.
    • header is set to false, indicating that the first row is identified as a data row in an exported file.

    Based on the above settings, the foreign table is created using the following statement:

    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/', FORMAT 'CSV',ENCODING 'utf8',DELIMITER E'\x20', QUOTE E'\x1b', NULL '') WRITE ONLY;
    
  6. In the database, export data to data files through the foreign table foreign_tpcds_reasons.
    1
    openGauss=# INSERT INTO foreign_tpcds_reasons SELECT * FROM reasons;
    
  7. After data export is complete, log in to the data server as user gds_user and stop GDS.
    The GDS process ID is 128954.
    ps -ef|grep gds
    gds_user 128954      1  0 15:03 ?        00:00:00 gds -d /output_data -p 192.168.0.90:5000 -D
    gds_user 129003 118723  0 15:04 pts/0    00:00:00 grep gds
    kill -9 128954

Example: Exporting Data Using Multiple Threads

A data server resides on the same intranet as the cluster, the IP address of the data server is 192.168.0.90, and source data files are in CSV format. In this scenario, data is concurrently exported from two tables using multiple threads in Remote mode.

To concurrently export data using multiple threads in Remote mode, perform the following operations:

  1. Log in to the GDS data server and create a database user and its user group.
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
  2. Switch to user gds_user and create the /output_data directory for storing exported files.
    su - gds_user
    mkdir -p /output_data
  3. Change the owner of the /output_data directory on the data server to gds_user.
    chown -R gds_user:gdsgrp /output_data 
  4. Log in to the data server as user gds_user and start GDS.
    The GDS installation path is /opt/bin/gds. Exported data files are stored in /output_data/. The IP address of the data server is 192.168.0.90. The GDS listening port number is 5000. The GDS runs in the background. The concurrency level is 2.
    /opt/bin/gds/gds -d /output_data -p 192.168.0.90:5000 -H 10.10.0.1/24 -D -t 2 
  5. In GaussDB, create the foreign tables foreign_tpcds_reasons1 and foreign_tpcds_reasons2 for receiving data from the data server.
    • Data export mode settings are as follows:
      • The directory for storing exported files is set to /output_data/ and the GDS listening port number is 5000 while GDS is started. The created directory for storing exported files is /output_data/. Therefore, the location parameter is set to gsfs://192.168.0.90:5000/.
    • Data format parameter settings are as follows:
      • format is set to CSV.
      • encoding is set to UTF-8.
      • delimiter is set to E'\x20'.
      • quote is set to 0x1b.
      • null is set to an empty string without quotation marks.
      • escape is set to a double quotation mark.
      • header is set to false, indicating that the first row is identified as a data row in an exported file.

    Based on the above settings, the foreign table foreign_tpcds_reasons1 is created using the following statement:

    1
    2
    3
    4
    5
    6
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons1
    (  
      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/', FORMAT 'CSV',ENCODING 'utf8', DELIMITER E'\x20', QUOTE E'\x1b', NULL '') WRITE ONLY;
    

    Based on the above settings, the foreign table foreign_tpcds_reasons2 is created using the following statement:

    1
    2
    3
    4
    5
    6
    openGauss=# CREATE FOREIGN TABLE foreign_tpcds_reasons2
    (  
      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/', FORMAT 'CSV', DELIMITER E'\x20', QUOTE E'\x1b', NULL '') WRITE ONLY;
    
  6. In the database, export data from table reasons1 through the foreign table foreign_tpcds_reasons1 and from table reasons2 through the foreign table foreign_tpcds_reasons2 to /output_data.
    1
    openGauss=# INSERT INTO foreign_tpcds_reasons1 SELECT * FROM reasons1;
    
    1
    openGauss=# INSERT INTO foreign_tpcds_reasons2 SELECT * FROM reasons2;
    
  7. After data export is complete, log in to the data server as user gds_user and stop GDS.
    The GDS process ID is 128954.
    ps -ef|grep gds
    gds_user 128954      1  0 15:03 ?        00:00:00 gds -d /output_data -p 192.168.0.90:5000 -D -t 2 
    gds_user 129003 118723  0 15:04 pts/0    00:00:00 grep gds
    kill -9 128954

Example: Exporting Data in Local Mode

A cluster contains eight DNs and four physical nodes. Every two DNs reside on the same physical node. The cluster has sufficient disk space and the cluster node I/O performance is satisfactory. In this scenario, data is concurrently exported in Local mode.

To concurrently export data in Local mode, perform the following operations:

  1. Log in to a physical node, create the /output_data directory for storing data files, and change the owner of the directory to omm.
    All the following operations use a node with the IP address 192.168.0.11 as an example.
    mkdir -p /output_data
    chown -R omm:dbgrp /output_data 
  2. In the database, create the foreign table foreign_tpcds_reasons.
    • Data export mode settings are as follows:
      • The directory created in the previous step for storing data files is /output_data/. Therefore, the location parameter is set to file:///output_data/.
    • Data format parameter settings are as follows:
      • format is set to CSV.
      • encoding is set to UTF-8.
      • delimiter is set to E'\x20'.
      • quote is set to 0x1b.
      • null is set to an empty string without quotation marks.
      • escape is set to a double quotation mark.
      • header is set to false, indicating that the first row is identified as a data row in an exported file.

    Based on the above settings, the foreign table foreign_tpcds_reasons is created using the following statement:

    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 'file:///output_data/', FORMAT 'CSV',ENCODING 'utf8', DELIMITER E'\x20', QUOTE E'\x1b', NULL '') WRITE ONLY;
    
  3. Export data in the database.
    1
    openGauss=# INSERT INTO foreign_tpcds_reasons SELECT * FROM reasons;
    
  4. View data files.

    The data files are generated in /output_data/YYYYMMDD/DN name on each cluster node. YYYYMMDD indicates the current system date. DN name can be found by running the following statement:

     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)
    

    On the node whose IP address is 192.168.0.11, the names of DNs are dn_6001_6002 and dn_6003_6004. In this case, data files are generated in /output_data/20160101/dn_6001_6002 and /output_data/20160101/dn_6003_6004 on the node.