如何在greenplum的报价字段中使用逗号从csv文件创建外部表?

Posted

技术标签:

【中文标题】如何在greenplum的报价字段中使用逗号从csv文件创建外部表?【英文标题】:how to create external table from csv file with commas in quote field in greenplum? 【发布时间】:2020-05-05 12:09:36 【问题描述】:

我正在尝试像这样从 csv 创建外部表:

CREATE EXTERNAL TABLE hctest.ex_nkp
(
a text,
b text,
c text,
d text,
e text,
f text,
g text,
h text
)
LOCATION ('gpfdist://192.168.56.111:10000/performnkp.csv')
FORMAT 'CSV' (DELIMITER ',' HEADER);

csv 由逗号 (,) 分隔,如下所示:

"Subject Username","Form Title","Form Start Date","Form End Date","Competency Name","Competency Description","Core Competency","Competency Official Rating"
"90008765","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","1. Uncompromising Integrity","<p>High ethical standards, low tolerance of unethical conduct.</p>","Yes","3"
"90008766","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","2. Team Synergy","<p>Passionately work together, ensuring completeness, to achieve common goals.</p>","Yes","3"
"90008767","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","3. Simplicity","<p>We do our utmost to deliver the easy to use solutions, exceeding customers&#39","","
"90008768","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","4. Exceptional Performance","<p>Highest level of performance, with a heart for people.</p>","Yes","3"

我发现了错误:

ERROR:  extra data after last expected column  (seg0 slice1 192.168.56.111:6000 pid=14121)
DETAIL:  External table ex_nkp, line 5 of file gpfdist://192.168.56.111:10000/performnkp.csv

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

您的 CSV 在第 4 行看起来格式不正确。请注意,在第 4 行的末尾有一个单引号,Greenplum 将其解释为带有换行符的 CSV 字段。通过在第 4 行添加缺少的引号,我可以在 Greenplum 中读取文件。

"Subject Username","Form Title","Form Start Date","Form End Date","Competency Name","Competency Description","Core Competency","Competency Official Rating"
"90008765","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","1. Uncompromising Integrity","<p>High ethical standards, low tolerance of unethical conduct.</p>","Yes","3"
"90008766","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","2. Team Synergy","<p>Passionately work together, ensuring completeness, to achieve common goals.</p>","Yes","3"
"90008767","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","3. Simplicity","<p>We do our utmost to deliver the easy to use solutions, exceeding customers&#39","","
"90008768","Performance Review - 2nd Semester 2019 for Ely Eisley","01/01/2019","31/12/2019","4. Exceptional Performance","<p>Highest level of performance, with a heart for people.</p>","Yes","3"

查询结果:

fguerrero=# select * from ex_nkp ;
NOTICE:  HEADER means that each one of the data files has a header row
    a     |                           b                           |     c      |     d      |              e              |                                         f                                          |  g  | h
----------+-------------------------------------------------------+------------+------------+-----------------------------+------------------------------------------------------------------------------------+-----+---
 90008765 | Performance Review - 2nd Semester 2019 for Ely Eisley | 01/01/2019 | 31/12/2019 | 1. Uncompromising Integrity | <p>High ethical standards, low tolerance of unethical conduct.</p>                 | Yes | 3
 90008766 | Performance Review - 2nd Semester 2019 for Ely Eisley | 01/01/2019 | 31/12/2019 | 2. Team Synergy             | <p>Passionately work together, ensuring completeness, to achieve common goals.</p> | Yes | 3
 90008767 | Performance Review - 2nd Semester 2019 for Ely Eisley | 01/01/2019 | 31/12/2019 | 3. Simplicity               | <p>We do our utmost to deliver the easy to use solutions, exceeding customers&#39  |     |
 90008768 | Performance Review - 2nd Semester 2019 for Ely Eisley | 01/01/2019 | 31/12/2019 | 4. Exceptional Performance  | <p>Highest level of performance, with a heart for people.</p>                      | Yes | 3
(4 rows)

如果这有帮助,请告诉我

【讨论】:

【参考方案2】:

您可以在外部表定义中指定“LOG ERRORS SEGMENT REJECT LIMIT 10”。这样,段将跳过有错误的行。 然后您可以使用“select * from gp_read_error_log('external_table_name');”返回跟踪详细信息 从示例中,您可以在该字段中添加额外的逗号。尝试在 HEADER 之后指定 QUOTE '"'。

【讨论】:

以上是关于如何在greenplum的报价字段中使用逗号从csv文件创建外部表?的主要内容,如果未能解决你的问题,请参考以下文章

sql字段里有逗号隔开的数据,怎么取值

使用连接(codeigniter)从单个字段中检索和打印逗号分隔符后的值

从Greenplum上的表中读取数据时,如何在Spark-jdbc应用程序的选项“dbtable”中指定子查询? [复制]

如何从浮点输入中去除逗号?

php 如何从mysql输出数组用逗号分隔?

如何使用 Python 将数据从 Teradata 传输到 Greenplum?