Updated on 2024-03-08 GMT+08:00

Examples of Exporting Data Using GDS

Exporting Data in Remote Mode

The data server and the cluster reside on the same intranet, the IP address of the data server is 192.168.0.90, and data source files are in CSV format. In this scenario, data is exported in parallel in Remote mode.

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

  1. Log in to the GDS data server as user root, create the /output_data directory for storing data files, and create user gds_user and its user group.
    1
    mkdir -p /output_data
    
  2. (Optional) Create a user and the user group it belongs to. The user is used to start GDS. If the user and user group exist, skip this step.
    1
    2
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
    
  3. Change the owner of the /output_data directory on the data server to gds_user.
    1
    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/dws/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 is 5000. GDS runs in daemon mode.
    1
    /opt/bin/dws/gds/bin/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 /output_data/ and the GDS listening port is 5000 when GDS is started. The directory created 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'\x08'.
    • quote is set to E'\x1b'.
    • null is set to an empty string without quotation marks.
    • escape defaults to the value of quote.
    • 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
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    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'\x08', 
    QUOTE E'\x1b', 
    NULL ''
    ) 
    WRITE ONLY;
    
  6. In the database, export data to data files through the foreign table foreign_tpcds_reasons.
    1
    INSERT INTO foreign_tpcds_reasons SELECT * FROM tpcds.reason;
    
  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.
    1
    2
    3
    4
    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
    

Exporting Data Using Multiple Threads

The data server and the cluster reside on the same intranet, the IP address of the data server is 192.168.0.90, and data source files are in CSV format. In this scenario, data is concurrently exported to two target 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 as user root, create the /output_data directory for storing data files, and create the database user and its user group.
    1
    2
    3
    mkdir -p /output_data
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
    
  2. Change the owner of the /output_data directory on the data server to gds_user.
    1
    chown -R gds_user:gdsgrp /output_data 
    
  3. Log in to the data server as user gds_user and start GDS.
    The GDS installation path is /opt/bin/dws/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 is 5000. GDS runs in daemon mode. The degree of parallelism is 2.
    1
    /opt/bin/dws/gds/bin/gds -d /output_data -p 192.168.0.90:5000 -H 10.10.0.1/24 -D -t 2 
    
  4. In GaussDB(DWS), 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 /output_data/ and the GDS listening port is 5000 when GDS is started. The directory created 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'\x08'.
      • quote is set to E'\x1b'.
      • null is set to an empty string without quotation marks.
      • escape defaults to the value of quote.
      • header is set to false, indicating that the first row is identified as a data row in an exported file.

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

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    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'\x08', 
    QUOTE E'\x1b', 
    NULL ''
    ) 
    WRITE ONLY;
    

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

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    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'\x08', 
    QUOTE E'\x1b', 
    NULL ''
    ) 
    WRITE ONLY;
    
  5. 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
    INSERT INTO foreign_tpcds_reasons1 SELECT * FROM tpcds.reason;
    
    1
    INSERT INTO foreign_tpcds_reasons2 SELECT * FROM tpcds.reason;
    
  6. After data export is complete, log in to the data server as user gds_user and stop GDS.
    The GDS process ID is 128954.
    1
    2
    3
    4
    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
    

Exporting Data Through a Pipe

  1. Start GDS.

    1
    gds -d /***/gds_data/ -D -p 192.168.0.1:7789 -l /***/gds_log/aa.log -H 0/0 -t 10 -D
    

    If you need to set the timeout interval of a pipe, use the --pipe-timeout parameter.

  2. Export data.

    1. Log in to the database, create an internal table, and write data to the table.
      1
      2
      3
      4
      5
      6
      7
      8
      CREATE TABLE test_pipe( id integer not null, sex text not null, name text ) ;
      
      
      INSERT INTO test_pipe values(1,2,'11111111111111');
      INSERT INTO test_pipe values(2,2,'11111111111111');
      INSERT INTO test_pipe values(3,2,'11111111111111');
      INSERT INTO test_pipe values(4,2,'11111111111111');
      INSERT 0 1
      
    2. Create a write-only foreign table.
      CREATE FOREIGN TABLE foreign_test_pipe_tw( id integer not null, age text not null, name  text ) SERVER gsmpp_server OPTIONS (LOCATION 'gsfs://192.168.0.1:7789/', FORMAT 'text', DELIMITER ',',  NULL '', EOL '0x0a' ,file_type 'pipe', auto_create_pipe 'false') WRITE ONLY;
    3. Execute the export statement. In this case, the statements are blocked.
      1
      INSERT INTO foreign_test_pipe_tw select * from test_pipe; 
      

  3. Export data through the GDS pipes.

    1. Log in to GDS and go to the GDS data directory.
      1
      cd /***/gds_data/  
      
    2. Create a pipe. If auto_create_pipe is set to true, skip this step.
      1
      mkfifo postgres_public_foreign_test_pipe_tw.pipe 
      

      A pipe will be automatically cleared after an operation is complete. To perform another operation, create a pipe file again.

    3. Read data from the pipe and write it to a new file.
      1
      cat postgres_public_foreign_test_pipe_tw.pipe > postgres_public_foreign_test_pipe_tw.txt
      
    4. To compress the exported files, run the following command:
      1
      gzip -9 -c < postgres_public_foreign_test_pipe_tw.pipe  > out.gz 
      
    5. To export the content from the pipe to the HDFS server, run the following command:
      1
      cat postgres_public_foreign_test_pipe_tw.pipe  | hdfs dfs -put -  /user/hive/***/test_pipe.txt
      

  4. Verify the exported data.

    1. Check whether the exported file is correct.
      1
      2
      3
      4
      5
      cat postgres_public_foreign_test_pipe_tw.txt
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
      
    2. View the compressed file.
      1
      2
      3
      4
      5
      vim out.gz
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
      
    3. View the data exported to the HDFS server.
      1
      2
      3
      4
      5
      hdfs dfs -cat /user/hive/***/test_pipe.txt
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
      

Exporting Data Through Multi-Process Pipes

GDS also supports importing and exporting data through multi-process pipes. That is, one foreign table corresponds to multiple GDSs.

The following takes exporting a local file as an example.

  1. Start multiple GDSs.

    1
    2
    gds -d /***/gds_data/ -D -p 192.168.0.1:7789 -l /***/gds_log/aa.log -H 0/0 -t 10 -D
    gds -d /***/gds_data_1/ -D -p 192.168.0.1:7790 -l /***/gds_log/aa.log -H 0/0 -t 10 -D
    

    If you need to set the timeout interval of a pipe, use the --pipe-timeout parameter.

  2. Export data.

    1. Log in to the database and create an internal table.
      1
      CREATE TABLE test_pipe (id integer not null, sex text not null, name  text);
      
    2. Write data.
      1
      2
      3
      4
      INSERT INTO test_pipe values(1,2,'11111111111111');
      INSERT INTO test_pipe values(2,2,'11111111111111');
      INSERT INTO test_pipe values(3,2,'11111111111111');
      INSERT INTO test_pipe values(4,2,'11111111111111');
      
    3. Create a write-only foreign table.
      1
      CREATE FOREIGN TABLE foreign_test_pipe_tw( id integer not null, age text not null, name  text ) SERVER gsmpp_server OPTIONS (LOCATION 'gsfs://192.168.0.1:7789/|gsfs://192.168.0.1:7790/', FORMAT 'text', DELIMITER ',',  NULL '', EOL '0x0a' ,file_type 'pipe', auto_create_pipe 'false') WRITE ONLY;
      
    4. Execute the export statement. In this case, the statements are blocked.
      1
      INSERT INTO foreign_test_pipe_tw select * from test_pipe; 
      

  3. Export data through the GDS pipes.

    1. Log in to GDS and go to each GDS data directory.
      1
      2
      cd /***/gds_data/ 
      cd /***/gds_data_1/ 
      
    2. Create a pipe. If auto_create_pipe is set to true, skip this step.
      1
      mkfifo postgres_public_foreign_test_pipe_tw.pipe 
      
    3. Read each pipe and write the new file to the pipes.
      cat postgres_public_foreign_test_pipe_tw.pipe > postgres_public_foreign_test_pipe_tw.txt

  4. Verify the exported data.

    1
    2
    cat /***/gds_data/postgres_public_foreign_test_pipe_tw.txt
    3,2,11111111111111
    
    1
    2
    3
    4
    cat /***/gds_data_1/postgres_public_foreign_test_pipe_tw.txt
    1,2,11111111111111
    2,2,11111111111111
    4,2,11111111111111