【问题标题】:Mapping column names in Hive with JSON serde使用 JSON serde 映射 Hive 中的列名
【发布时间】:2017-03-01 14:19:26
【问题描述】:

我正在使用 Hive 中的内置 JSON serde 创建一个外部表,即org.apache.hive.hcatalog.data.JsonSerDe。我的输入 JSON 包含一个名为 last 的字段,我想将其映射到表中的不同列名,因为 last 是保留关键字。

这可能与SERDEPROPERTIES 有关吗?我可以找到如何使用OpenX Json serde 执行此操作的示例,但不是蜂巢。

目前我正在像这样创建我的表

CREATE EXTERNAL TABLE my_table (
    a string,
    b string,
    last string)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
LOCATION 's3://my-bucket/my-folder/data'

【问题讨论】:

    标签: json hadoop hive


    【解决方案1】:

    last 是一个非保留关键字。
    这里没有问题。

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Keywords,Non-reservedKeywordsandReservedKeywords

    hive> dfs -cat /user/hive/warehouse/my_table/*;
    {"a":"hello","b":"world","last":"!"}
    

    create external table my_table 
    (
        a       string
       ,b       string
       ,last    string
    )
        row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
        location '/user/hive/warehouse/my_table'
    ;
    

    select * from my_table
    ;
    

    +------------+------------+---------------+
    | my_table.a | my_table.b | my_table.last |
    +------------+------------+---------------+
    | hello      | world      | !             |
    +------------+------------+---------------+
    

    对于保留关键字,使用`(重音)限定可以解决问题。

    hive> dfs -cat /user/hive/warehouse/my_table_2/*;
    {"and":"X","or":"Mix","not":"Drix"}
    

    create external table my_table_2
    (
        `and`   string
       ,`or`    string
       ,`not`   string
    )
        row format serde 'org.apache.hive.hcatalog.data.JsonSerDe'
        location '/user/hive/warehouse/my_table_2'
    ;
    

    select * from my_table_2
    ;
    

    +----------------+---------------+----------------+
    | my_table_2.and | my_table_2.or | my_table_2.not |
    +----------------+---------------+----------------+
    | X              | Mix           | Drix           |
    +----------------+---------------+----------------+
    

    【讨论】:

    • 我的立场已得到纠正 - 感谢您的全面回答。事实上,我在使用 AWS Athena 时遇到了问题 - SELECT * FROM my_table 有效,但 SELECT last FROM my_table 无效。听起来这个问题可能是 Athena 特有的。
    【解决方案2】:

    使用 ' 单引号作为关键字。示例代码

    CREATE EXTERNAL TABLE my_table (
        a string,
        b string,
        'last' string)
    ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
    LOCATION 's3://my-bucket/my-folder/data'
    

    【讨论】:

    • `last` 字符串
    • 符号错误。不是',而是`
    • 最后保留了吗?
    • 应该是(')单引号怎么看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2016-02-28
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多