我正在尝试将带有管道分隔符的csv加载到配置单元外部表。出现在数据字段中的管道用引号括起来。数据中出现的双引号用\转义。当我配置外部表时,我看到带双引号的数据没有正确解释。
测试.csv
id|name
105|"Test | pipe delim in field"
107|\" Test Escaped single double quote in HIVE
108|\" Test Escaped enclosed double quote in HIVE \"
109|\\" Test Escaped enclosed double quote in HIVE \"
110|\\" Test Escaped enclosed double quote in HIVE \\"
外部表create语句
drop table test_schema.hive_test;
CREATE EXTERNAL TABLE test_schema.hive_test (id string, name string)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES
(
"separatorChar" = "|",
"quoteChar" = "\"",
"escapeChar" = "\\"
)
LOCATION '/staging/test/hive'
tblproperties ("skip.header.line.count"="1");
输出
+---------------+-------------------------------------------------+
| hive_test.id | hive_test.name |
+---------------+-------------------------------------------------+
| 105 | Test | pipe delim in field |
| 107 | NULL |
| 108 | NULL |
| 109 | NULL |
| 110 | " Test Escaped enclosed double quote in HIVE \ |
+---------------+-------------------------------------------------+
预期产量
+---------------+-------------------------------------------------+
| hive_test.id | hive_test.name |
+---------------+-------------------------------------------------+
| 105 | Test | pipe delim in field |
| 107 | " Test Escaped single double quote in HIVE |
| 108 | " Test Escaped enclosed double quote in HIVE " |
| 109 | NULL |
| 110 | NULL |
+---------------+-------------------------------------------------+
打开csv版本2.3
1条答案
按热度按时间x7yiwoj41#
遗憾的是,这是不可能实现的,因为opencsv使用单个字符作为转义符,而实际上您正试图使用双反斜杠作为转义符(这将是一个
string
). 在opencsvserde类中,您可以发现无论您传递什么作为转义符,opencsvserde都会获得字符串值的第一个字符https://github.com/apache/hive/blob/master/serde/src/java/org/apache/hadoop/hive/serde2/opencsvserde.java#l98这里是当前的代码作为参考
}
我认为缺少一个警告,即在创建表时让用户知道只支持单个字符。