【问题标题】:Load data into hive table from CSV containing dictionary从包含字典的 CSV 将数据加载到配置单元表中
【发布时间】:2021-09-21 08:28:17
【问题描述】:

我有一个 CSV 文件,我试图将它放在一个配置单元表中。 CSV 包含一些列的字典。

文件如下所示:

a,b,{'c':'d','e':'f'},g

所以表格应该是这样的:

| col1     | col2           | col3              | col4|
| -------- | -------------- | ----              | --- |
| a        | c              | {'c':'d','e':'f'} |   g |

但它会拾取括号内的逗号。

如何忽略括号内的逗号。

我正在使用它来编写配置单元表。

create external table mytable(
  col1 string,
  col2 string,
  col3 string,
  col4 string
) 

row format delimited fields terminated by ',' stored as textfile location '/user/myuser/mydir/';

【问题讨论】:

    标签: csv hadoop hive hdfs hiveql


    【解决方案1】:

    如果您可以用双引号将列括起来,则可以使用 OpenCSVSerde 正确加载数据。大多数 csv 生成工具应该能够做到这一点。

    您的文件应如下所示-

    "a","b","{'c':'d','e':'f'}","g"
    

    你的脚本应该是这样的 -

       col1    STRING, 
       col2    STRING, 
       col3    STRING, 
       col4    STRING
    )
    ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
    WITH SERDEPROPERTIES (
       "separatorChar" = "\t",
       "quoteChar"     = "\""
    )  
    LOCATION '/your/folder/location/';
    

    您可以查看此链接https://cwiki.apache.org/confluence/display/Hive/CSV+Serde。现在,这个过程对于字符串列来说是完美的,对于其他数据类型,你需要在下一步将它们转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多