read a csv file with comma as delimiter and escaping quotes in psql

时间:2017-12-18 05:48:52

标签: sql csv greenplum

I want to read a csv file which is separated by comma (,) but want to ignore comma within the double quotes (""). I want to store the result into a table.

Example:

abc,00.000.00.00,00:00:00:00:00:00,Sun Nov 01 00:00:00 EST 0000,Sun Nov 01 00:00:00 EST 0000,"Apple, Inc.",abcd-0000abc-a,abcd-abcd-a0000-00

Here I don't want to split on Apple, .

I know there exists csv reader in python and I can use it in plpython but that's slow considering millions of such strings! I would like a pure psql method!

1 个答案:

答案 0 :(得分:1)

以下是使用CSV格式读取带外部表的CSV文件的示例。

CREATE EXTERNAL TABLE ext_expenses ( name text, 
date date,  amount float4, category text, desc1 text ) 
LOCATION ('gpfdist://etlhost-1:8081/*.txt', 
          'gpfdist://etlhost-2:8082/*.txt')
FORMAT 'CSV' ( DELIMITER ',' )
LOG ERRORS SEGMENT REJECT LIMIT 5;

这也取自Greenplum文档。

http://gpdb.docs.pivotal.io/530/admin_guide/external/g-example-4-single-gpfdist-instance-with-error-logging.html