Naveen Srinivas
Naveen Srinivas

Reputation: 71

HIVE Create table for JSON - STRUCT Error

I am trying to load JSON data to HIVE. I am using the default SERDE present in HIVE. I am encountering an error during create table. Help required!

JSON data:

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    }
}}

The Create Statement :

CREATE TABLE complex_json(
widget struct<window:struct< title:string,name:string,width:int,height:int>,
debug:string,
image:struct< src:string,name:string,hOffset:int,vOffset:int,alignment:string > >
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe';

And I am getting the error :

ParseException line 2:27 cannot recognize input near 'window' ':' 'struct' in column specification

Upvotes: 2

Views: 1096

Answers (1)

Mukesh S
Mukesh S

Reputation: 2876

WINDOW is a reserved key word in Hive and directly you cannot use reserved as part of your column,reserved key words

You have two options here:

1.) Do not use any reserved key work as part of columns. Try renaming your object name from window to say window1 and it will work.

2.) If you want to use keywords then use like this `window`, back quote and the column(key word) So your create table will look like :

CREATE TABLE complex_json(
widget struct< `window` :struct< title:string,name:string,width:int,height:int>,
debug:string,
image:struct< src:string,name:string,hOffset:int,vOffset:int,alignment:string > >
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe';

Also make sure your JSON is in single line. All these SerDes don't recognize proper formatted JSON.

Hope it helps...!!!

Upvotes: 2

Related Questions