hive-escape双引号问题

lbsnaicq  于 2021-05-31  发布在  Hadoop
关注(0)|答案(1)|浏览(627)

我正在尝试将带有管道分隔符的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

x7yiwoj4

x7yiwoj41#

遗憾的是,这是不可能实现的,因为opencsv使用单个字符作为转义符,而实际上您正试图使用双反斜杠作为转义符(这将是一个 string ). 在opencsvserde类中,您可以发现无论您传递什么作为转义符,opencsvserde都会获得字符串值的第一个字符https://github.com/apache/hive/blob/master/serde/src/java/org/apache/hadoop/hive/serde2/opencsvserde.java#l98
这里是当前的代码作为参考

private char getProperty(final Properties tbl, final String property, final char def) {
final String val = tbl.getProperty(property);

if (val != null) {
  return val.charAt(0);
}

return def;

}
我认为缺少一个警告,即在创建表时让用户知道只支持单个字符。

相关问题