文档首页> 数据仓库服务 GaussDB(DWS)> 常见问题> 数据导入导出> 如何实现不同编码库之间数据容错导入
更新时间:2023-11-14 GMT+08:00
分享

如何实现不同编码库之间数据容错导入

要实现从数据库A(UTF8编码)至数据库B(GBK编码)的数据导入,常规方法导入数据时会出现字符集编码不匹配的错误,导致数据无法导入。

针对小批量数据导入的场景,可以通过\COPY命令来完成,具体方法如下:

  1. 创建数据库A和B,其中数据库A的编码格式为UTF8,数据库B的编码格式为GBK。

    1
    2
    postgres=> CREATE DATABASE A ENCODING 'UTF8' template = template0;
    postgres=> CREATE DATABASE B ENCODING 'GBK' template = template0;
    

  2. 查看数据库列表,可以看到已经创建的数据库A和B。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    postgres=> \l
                               List of databases
       Name    |  Owner  | Encoding  | Collate | Ctype | Access privileges
    -----------+---------+-----------+---------+-------+-------------------
     a         | dbadmin | UTF8      | C       | C     |
     b         | dbadmin | GBK       | C       | C     |
     gaussdb   | Ruby    | SQL_ASCII | C       | C     |
     postgres  | Ruby    | SQL_ASCII | C       | C     |
     template0 | Ruby    | SQL_ASCII | C       | C     | =c/Ruby          +
               |         |           |         |       | Ruby=CTc/Ruby
     template1 | Ruby    | SQL_ASCII | C       | C     | =c/Ruby          +
               |         |           |         |       | Ruby=CTc/Ruby
     xiaodi    | dbadmin | UTF8      | C       | C     |
    (7 rows)
    

  3. 切换到数据库A,输入用户密码。创建表test01,并向表中插入数据。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    postgres=> \c a
    Password for user dbadmin:
    SSL connection (protocol: TLSv1.3, cipher: TLS_AES_128_GCM_SHA256, bits: 128)
    You are now connected to database "a" as user "dbadmin".
    
    a=> CREATE TABLE test01
     (
         c_customer_sk             integer,
         c_customer_id             char(5),
         c_first_name              char(6),
         c_last_name               char(8)
     )
     with (orientation = column,compression=middle)
     distribute by hash (c_last_name);
    CREATE TABLE
    a=> INSERT INTO test01(c_customer_sk, c_customer_id, c_first_name) VALUES (3769, 'hello', 'Grace');
    INSERT 0 1
    a=> INSERT INTO test01 VALUES (456, 'good');
    INSERT 0 1
    

  4. 使用\COPY命令从utf8库中将数据以Unicode编码格式导出成文件test01.dat。

    1
    \copy test01 to '/opt/test01.dat' with (ENCODING 'Unicode');
    

  5. 切换到数据库B,创建同名表test01。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    a=> \c b
    Password for user dbadmin:
    SSL connection (protocol: TLSv1.3, cipher: TLS_AES_128_GCM_SHA256, bits: 128)
    You are now connected to database "b" as user "dbadmin".
    
    b=> CREATE TABLE test01
     (
         c_customer_sk             integer,
         c_customer_id             char(5),
         c_first_name              char(6),
         c_last_name               char(8)
     )
     with (orientation = column,compression=middle)
     distribute by hash (c_last_name);
    

  6. 使用\COPY命令将文件test01.dat导入到数据库B。

    1
    \copy test01 from '/opt/test01.dat' with (ENCODING 'Unicode' ,COMPATIBLE_ILLEGAL_CHARS 'true');
    
    • 容错性参数COMPATIBLE_ILLEGAL_CHARS 指定导入时对非法字符进行容错处理,非法字符转换后入库。不报错,不中断导入。
    • 此参数不支持BINARY格式,会报“cannot specify bulkload compatibility options in BINARY mode”错误信息。
    • 此参数仅对COPY FROM导入有效。

  7. 在数据库B里查看表test01中的数据;

    1
    2
    3
    4
    5
    6
    b=> select * from test01;
     c_customer_sk | c_customer_id | c_first_name | c_last_name
    ---------------+---------------+--------------+-------------
              3769 | hello         | Grace        |
               456 | good          |              |
    (2 rows)
    

  8. 通过以上操作就可完成将数据从数据库A(UTF8)至数据库B(GBK)的导入。
分享:

数据导入导出 所有常见问题

more