Updated at: 2022-08-03 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.
    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.
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
  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/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.
    /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'\x0a'.
    • 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:

    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.
    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.
    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.
    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.
    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.
    /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:

    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:

    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.
    INSERT INTO foreign_tpcds_reasons1 SELECT * FROM tpcds.reason;
    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.
    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.

    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.
      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');
    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. The statement will be blocked.
      INSERT INTO foreign_test_pipe_tw select * from test_pipe; 

  3. Export data through the GDS pipe.

    1. Log in to GDS and go to the GDS data directory.
      cd /***/gds_data/  
    2. Create a pipe. If auto_create_pipe is set to true, skip this step.
      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 again.

    3. Read data from the pipe and write it to a new file.
      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:
      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:
      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.
      cat postgres_public_foreign_test_pipe_tw.txt
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
    2. Check the compressed file.
      vim out.gz
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
    3. Check the data exported to the HDFS server.
      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.

    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.
      CREATE TABLE test_pipe (id integer not null, sex text not null, name  text);
    2. Write data.
      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.
      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. The statement will be blocked.
      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.
      cd /***/gds_data/ 
      cd /***/gds_data_1/ 
    2. Create a pipe. If auto_create_pipe is set to true, skip this step.
      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.

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

close