更新时间:2023-04-19 GMT+08:00

GDS导出示例

Remote模式导出

规划数据服务器与集群处于同一内网,数据服务器IP为192.168.0.90,导出数据文件格式为CSV,所以规划的并行导出模式为Remote模式。

Remote模式并行导出数据操作示例如下所示:

  1. 以root用户登录GDS数据服务器,创建数据文件存放目录“/output_data”,启动gds_user用户及所属的用户组。
    mkdir -p /output_data
  2. (可选)创建用户及其所属的用户组。此用户用于启动GDS。若该类用户及所属用户组已存在,可跳过此步骤。
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
  3. 修改数据服务器上数据文件目录“/output_data”的属主为gds_user。
    chown -R gds_user:gdsgrp /output_data 
  4. 以gds_user用户登录数据服务器上分别启动GDS。
    其中GDS安装路径为“/opt/bin/dws/gds”,导出数据文件存放在“/output_data/”目录下,数据服务器所在IP为192.168.0.90,GDS监听端口为5000,以后台方式运行。
    /opt/bin/dws/gds/bin/gds -d /output_data -p 192.168.0.90:5000 -H 10.10.0.1/24 -D
  5. 在数据库中创建外表foreign_tpcds_reasons用于接收数据服务器上的数据。

    其中设置的导出模式信息如下所示:

    • 由于启动GDS时,设置的导出数据文件存放目录为“/output_data/”,GDS监听端口为5000。创建的导出数据文件存放目录为“/output_data/”。所以设置参数“location”为“gsfs://192.168.0.90:5000/”。

    设置导出的数据文件格式信息如下所示:

    • 数据文件格式(format)为CSV。
    • 编码格式(encoding)为UTF-8。
    • 字段分隔符(delimiter)为E'\x08'。
    • 引号字符(quote)为E'\x1b'。
    • 数据文件中空值(null)为没有引号的空字符串。
    • 逃逸字符(escape)默认和quote相同。
    • 数据文件是否包含标题行(header)为默认值false,即导出时数据文件第一行被识别为数据。

    根据以上信息,创建的外表如下所示:

    1
    2
    3
    4
    5
    6
    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. 在数据库上,通过外表foreign_tpcds_reasons,将数据导出到数据文件中。
    1
    INSERT INTO foreign_tpcds_reasons SELECT * FROM tpcds.reason;
    
  7. 待数据导出完成后,以gds_user用户登录数据服务器,停止GDS。
    其中GDS进程号为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

多线程导出

规划数据服务器与集群处于同一内网,数据服务器IP为192.168.0.90,导出的数据文件格式为CSV,同时导出2个目标表,所以规划使用Remote模式进行多线程导出。

Remote模式多线程导出数据操作示例如下所示:

  1. 以root用户登录GDS数据服务器,创建导出数据文件存放目录“/output_data”,数据库用户及所属的用户组。
    mkdir -p /output_data
    groupadd gdsgrp
    useradd -g gdsgrp gds_user
  2. 修改数据服务器上数据文件目录“/output_data”的属主为gds_user。
    chown -R gds_user:gdsgrp /output_data 
  3. 以gds_user用户登录数据服务器上启动GDS。
    其中GDS安装路径为“/opt/bin/dws/gds”,导出数据文件存放在“/output_data/”目录下,数据服务器所在IP为192.168.0.90,GDS监听端口为5000,以后台方式运行,设定并发度为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. GaussDB(DWS)上,创建外表foreign_tpcds_reasons1和foreign_tpcds_reasons2用于接收数据服务器上的数据。
    • 其中设置的导出模式信息如下所示:
      • 由于启动GDS时,设置的导出数据文件存放目录为“/output_data/”,GDS监听端口为5000。创建的导出数据文件存放目录为“/output_data/”。所以设置参数“location”为“gsfs://192.168.0.90:5000/”。
    • 设置导出的数据文件格式信息如下所示:
      • 数据文件格式(format)为CSV。
      • 编码格式(encoding)为UTF-8。
      • 字段分隔符(delimiter)为E'\x08'。
      • 引号字符(quote)为E'\x1b'。
      • 数据文件中空值(null)为没有引号的空字符串。
      • 逃逸字符(escape)默认和quote相同。
      • 数据文件是否包含标题行(header)为默认值false,即导出时数据文件第一行被识别为数据。

    根据以上信息,创建的外表foreign_tpcds_reasons1如下所示:

    1
    2
    3
    4
    5
    6
    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;
    

    参考以上设置,创建的外表foreign_tpcds_reasons2如下所示:

    1
    2
    3
    4
    5
    6
    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. 在数据库中通过外表foreign_tpcds_reasons1和foreign_tpcds_reasons2,将表reasons1和reasons2中的数据导出到目录“/output_data”中。
    1
    INSERT INTO foreign_tpcds_reasons1 SELECT * FROM tpcds.reason;
    
    1
    INSERT INTO foreign_tpcds_reasons2 SELECT * FROM tpcds.reason;
    
  6. 待数据导出完成后,以gds_user用户登录数据服务器,停止GDS。
    其中GDS进程号为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

单个管道文件导出

  1. 启动GDS。

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

    如果需要设置管道文件的超时时间,则使用--pipe-timeout参数设置。

  2. 执行数据导出。

    1. 登录数据库创建内表,并写入数据。
      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 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. 导出语句,此时语句会阻塞。
      INSERT INTO foreign_test_pipe_tw select * from test_pipe; 

  3. 通过管道文件将数据从GDS导出。

    1. 登录GDS,进入GDS数据目录。
      cd /***/gds_data/  
    2. 创建管道文件,如果auto_create_pipe设置为true跳过此步骤。
      mkfifo postgres_public_foreign_test_pipe_tw.pipe 

      管道文件创建完成后,每执行完一次操作,业务会被自动清理。如果还需要执行其他业务,请参考该步骤重新创建管道文件。

    3. 读取管道文件并写入新文件。
      cat postgres_public_foreign_test_pipe_tw.pipe > postgres_public_foreign_test_pipe_tw.txt
    4. 若需要对导出的文件进行压缩执行:
      gzip -9 -c < postgres_public_foreign_test_pipe_tw.pipe  > out.gz 
    5. 若需要将管道文件的内容导出到hdfs服务器:
      cat postgres_public_foreign_test_pipe_tw.pipe  | hdfs dfs -put -  /user/hive/***/test_pipe.txt

  4. 验证导出的数据。

    1. 查看文件是否导出正确。
      cat postgres_public_foreign_test_pipe_tw.txt
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
    2. 查看压缩后的文件。
      vim out.gz
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111
    3. 查看导出到hdfs服务器上的数据。
      hdfs dfs -cat /user/hive/***/test_pipe.txt
      3,2,11111111111111
      1,2,11111111111111
      2,2,11111111111111
      4,2,11111111111111

多进程管道文件导出

GDS也支持多进程管道文件导入导出, 即启动一个外表对应多个GDS。

以本地文件的导出为例:

  1. 启动多个GDS。

    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

    如果需要设置管道文件的超时时间,则使用--pipe-timeout参数设置。

  2. 执行数据导出。

    1. 登录数据库创建内表。
      CREATE TABLE test_pipe (id integer not null, sex text not null, name  text);
    2. 写入数据。
      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 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. 导出语句,此时语句会阻塞。
      INSERT INTO foreign_test_pipe_tw select * from test_pipe; 

  3. 通过管道文件将数据从GDS导出。

    1. 登录GDS,分别进入GDS数据目录。
      cd /***/gds_data/ 
      cd /***/gds_data_1/ 
    2. 创建管道文件,如果auto_create_pipe设置为true跳过此步骤。
      mkfifo postgres_public_foreign_test_pipe_tw.pipe 
    3. 分别读取管道文件并写入新文件。
      cat postgres_public_foreign_test_pipe_tw.pipe > postgres_public_foreign_test_pipe_tw.txt

  4. 验证导出的数据。

    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