将 CSV 加载到 table 时出错
Error loading CSV to table
我的 CSV 文件包含有关公司员工的详细信息。一栏包含员工的薪水(例如 - 4,000 美元)。
因此,当我编写脚本将数据加载到 table 中时,分隔符将我的薪水列分别作为 4 和 000。如何处理?
CSV 文件示例 -
澳大利亚,35-39 岁,咨询,创业 (1-25),Web 应用程序开发人员,"$10,001 - $25,000",Enterprise
Table代码-
create table survey
(
country string,
age string,
industryType string,
companyType string,
occupation string,
salary string,
project string)
row format delimited
fields terminated by ',' ;
参考文献:
符合 Apache 的 DDL 语法 Create/Drop/Truncate Table, and, more specifically, to Row Formats & SerDe,为了更改 FIELDS/LINES/etc
属性,您可以使用:
- 原生 SerDe(如果未指定
ROW FORMAT
,或设置了 ROW FORMAT DELIMITED
),或
- a custom SerDe(通过应用
SERDE
子句)。
选项 1:使用本机 Serde
CREATE TABLE survey(country string, age string, industryType string, companyType string, occupation string, salary string, project string)
ROW FORMAT DELIMITED
FIELDS
TERMINATED BY ","
ESCAPED BY "\"
LINES TERMINATED BY "\n"
STORED AS TEXTFILE;
请注意缺少可以指定字符引用字段值的子句。
选项 2:使用自定义 Serde
在 Row Formats & SerDe 的 table 中查看名为 "CSV/TSV" 的记录。
CREATE TABLE survey(country string, age string, industryType string, companyType string, occupation string, salary string, project string)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
"separatorChar" = ",",
"quoteChar" = "\"",
"escapeChar" = "\"
)
STORED AS TEXTFILE;
我建议您使用包含一行值的 CSV 文件进行测试。在行内将所有列值括在双引号中,看看会发生什么。被包围,“,”字符(如 salary
字段中的字符)will/should 会自动转义,因此会保存为相应列值的一部分。
祝你好运。
我的 CSV 文件包含有关公司员工的详细信息。一栏包含员工的薪水(例如 - 4,000 美元)。 因此,当我编写脚本将数据加载到 table 中时,分隔符将我的薪水列分别作为 4 和 000。如何处理?
CSV 文件示例 - 澳大利亚,35-39 岁,咨询,创业 (1-25),Web 应用程序开发人员,"$10,001 - $25,000",Enterprise
Table代码-
create table survey ( country string, age string, industryType string, companyType string, occupation string, salary string, project string) row format delimited fields terminated by ',' ;
参考文献:
符合 Apache 的 DDL 语法 Create/Drop/Truncate Table, and, more specifically, to Row Formats & SerDe,为了更改 FIELDS/LINES/etc
属性,您可以使用:
- 原生 SerDe(如果未指定
ROW FORMAT
,或设置了ROW FORMAT DELIMITED
),或 - a custom SerDe(通过应用
SERDE
子句)。
选项 1:使用本机 Serde
CREATE TABLE survey(country string, age string, industryType string, companyType string, occupation string, salary string, project string)
ROW FORMAT DELIMITED
FIELDS
TERMINATED BY ","
ESCAPED BY "\"
LINES TERMINATED BY "\n"
STORED AS TEXTFILE;
请注意缺少可以指定字符引用字段值的子句。
选项 2:使用自定义 Serde
在 Row Formats & SerDe 的 table 中查看名为 "CSV/TSV" 的记录。
CREATE TABLE survey(country string, age string, industryType string, companyType string, occupation string, salary string, project string)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
"separatorChar" = ",",
"quoteChar" = "\"",
"escapeChar" = "\"
)
STORED AS TEXTFILE;
我建议您使用包含一行值的 CSV 文件进行测试。在行内将所有列值括在双引号中,看看会发生什么。被包围,“,”字符(如 salary
字段中的字符)will/should 会自动转义,因此会保存为相应列值的一部分。
祝你好运。