如何处理列值中的分隔符?

ewm0tg9j  于 2021-07-15  发布在  Hadoop
关注(0)|答案(1)|浏览(557)

我试图将csv文件数据加载到配置单元表中,但它在一列的值中有分隔符(,),因此配置单元将它作为分隔符并将其加载到新列中。我尝试使用转义序列,但我也尝试了转义序列,它不起作用,总是在新的列中加载数据。
我的csv文件:

  1. id,name,desc,per1,roll,age
  2. 226,a1,"\"double bars","item1 and item2\"",0.0,10,25
  3. 227,a2,"\"doubles","item2 & item3 item4\"",0.1,20,35
  4. 228,a3,"\"double","item3 & item4 item5\"",0.2,30,45
  5. 229,a4,"\"double","item5 & item6 item7\"",0.3,40,55

我已经更新了我的table

  1. create table testing(id int, name string, desc string, uqc double, roll int, age int)
  2. ROW FORMAT SERDE
  3. 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
  4. WITH SERDEPROPERTIES (
  5. "separatorChar" = ",",
  6. "quoteChar" = '"',
  7. "escapeChar" = "\\" ) STORED AS textfile;

但我还是从另一列得到了数据,。
我在路径命令中使用加载数据。

rjzwgtxy

rjzwgtxy1#

这是如何基于regexserde创建表。
每列应有相应的捕获组 () 在正则表达式中。您可以轻松地调试regex,而无需使用 regex_replace :

  1. select regexp_replace('226,a1,"\"double bars","item1 and item2\"",0.0,10,25',
  2. '^(\\d+?),(.*?),"(.*)",([0-9.]*),([0-9]*),([0-9]*).*', --6 groups
  3. '$1 $2 $3 $4 $5 $6'); --space delimited fields

结果:

  1. 226 a1 "double bars","item1 and item2" 0.0 10 25

如果看起来不错,请创建表:

  1. create external table testing(id int,
  2. name string,
  3. desc string,
  4. uqc double,
  5. roll int,
  6. age int
  7. )
  8. ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
  9. WITH SERDEPROPERTIES ('input.regex'='^(\\d+?),(.*?),"(.*)",([0-9.]*),([0-9]*),([0-9]*).*')
  10. location ....
  11. TBLPROPERTIES("skip.header.line.count"="1")
  12. ;

阅读本文了解更多细节。

展开查看全部

相关问题