2017-03-01 60 views
1

我在Hive中使用內置的JSON服務器創建外部表,即org.apache.hive.hcatalog.data.JsonSerDe。我的輸入JSON包含一個名爲last的字段,我想將其映射到表中的不同列名,因爲last是保留關鍵字。使用JSON映射映射Hive中的列名稱

這可能與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' 

回答

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  | !    | 
+------------+------------+---------------+ 

對於保留關鍵字s,'(Grave accent)的資格解決了這個問題。

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   | 
+----------------+---------------+----------------+ 
+0

我站在更正 - 謝謝你的全面解答。實際上,在使用AWS Athena時遇到了一個問題 - 'SELECT * FROM my_table'起作用,但是'SELECT last FROM my_table'不起作用。聽起來像這個問題可能是特定於雅典娜。 – tobycoleman

0

使用「爲關鍵詞的單引號。示例代碼

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' 
+1

\'最後\'串 –

+0

錯誤的符號。不是'''而是'\'' –

+0

是最後一個保留了嗎? – franklinsijo