假设您没有任何“^”字符,您可以将其用作转义字符。
copy tpcds.call_center to stdout with delimiter '|' escape '^';
更多关于副本的信息可以在这里找到:https://www.postgresql.org/docs/8.2/static/sql-copy.html
这种技术会比较慢,给法师带来负担。如果您改用 gpfdist,则可以利用集群中的并行性并绕过主节点。此解决方案非常适合卸载大量数据。
首先,启动gpfidst进程:
[gpadmin@gpdbsne ~]$ gpfdist -p 8888 > gpfdist_8888.log 2>&1 < gpfdist_8888.log &
[1] 2255
现在,您可以创建外部表了。
[gpadmin@gpdbsne ~]$ psql
SET
Timing is on.
psql (8.2.15)
Type "help" for help.
gpadmin=# create writable external table tpcds.et_call_center
(like tpcds.call_center)
location ('gpfdist://gpdbsne:8888/call_center.txt')
format 'text' (delimiter '|' escape '^');
NOTICE: Table doesn't have 'distributed by' clause, defaulting to distribution columns from LIKE table
CREATE EXTERNAL TABLE
Time: 18.681 ms
现在,您插入数据:
gpadmin=# insert into tpcds.et_call_center select * from tpcds.call_center;
INSERT 0 6
Time: 72.653 ms
gpadmin=# \q
验证:
[gpadmin@gpdbsne ~]$ wc -l call_center.txt
6 call_center.txt
在我的示例中,我使用了主机名“gpdbsne”,该集群中的所有网段都可以访问它。通常,Greenplum 使用专用网络在分段之间进行通信,因此该主机名需要连接到专用网络。
由于可写外部表是使用 SQL 写入的,因此您可以在 SQL 中使用任何您想要的转换逻辑,这样您就可以根据需要将制表符更改为空格。这消除了对文件进行后处理的 awk 或 sed 的需要。复制也可以使用 SQL,但就像我说的,它比使用可写外部表要慢。