从postgre在功能上执行复制命令,并创建一个文件

问题描述 投票:0回答:1

我的要求是一样创建一个函数从查询的数据复制到文件,并捕捉它的计数。例

  Create function xoy(query,path) returns integer as
   Copy (query) to stdout >path of file
    Get row_count of the query above
    Close

以上是需要一个碱逻辑,请帮帮Postgre SQL 8.3,Greenplum的

function unix copy greenplum
1个回答
1
投票

下面是使用gpfdist和外部表的一个例子。这将提供给写一个外部文件最快的性能。你也应该知道,你可以写信给S3,HDFS,和其他人PXF。

这是Greenplum的一个示例表:

create table foo
(id int,
 fname text,
 lname text,
 city text,
 state text,
 zip text)
distributed by (id);

插入这个例子一些虚拟数据:

insert into foo 
(id, fname, lname, city, state, zip)
select i, 'foo_' || i, 'bar_' || i, 'city_' || i, 'state_' || i, 'zip_' || i
from generate_series(1, 10000) as i;

这是一个使用gpfdist的写入外部表。

create writable external table ext_foo
(like foo)
location ('gpfdist://mdw:8999/foo.txt')
format 'text' (delimiter '|' null as '')
distributed by (id);

这里是你想用你的函数:

create or replace function fn_export_foo() returns void as
$$
declare

begin
    insert into ext_foo 
    select * from foo;
end

$$
language plpgsql;

而现在,MDW主机(如写入外部表定义中指定)上,从庆典开始gpfdist。

gpfdist -p 8999 > gpfdist_8999.log 2>&1 < gpfdist_8999.log &

现在执行的功能:

select fn_export_foo();

下面是结果:

[gpadmin@mdw ~]$ head foo.txt 
42|foo_42|bar_42|city_42|state_42|zip_42
74|foo_74|bar_74|city_74|state_74|zip_74
90|foo_90|bar_90|city_90|state_90|zip_90
122|foo_122|bar_122|city_122|state_122|zip_122
234|foo_234|bar_234|city_234|state_234|zip_234
250|foo_250|bar_250|city_250|state_250|zip_250
293|foo_293|bar_293|city_293|state_293|zip_293
325|foo_325|bar_325|city_325|state_325|zip_325
341|foo_341|bar_341|city_341|state_341|zip_341
373|foo_373|bar_373|city_373|state_373|zip_373

在我的测试集群,拥有10000个记录文件写于143毫秒。

© www.soinside.com 2019 - 2024. All rights reserved.