Loading JSON file in HIVE table

Koushik Chandra picture Koushik Chandra · Jan 3, 2017 · Viewed 23k times · Source

I have a JSON file like below, which I want to load in a HIVE table with parsed format, what are possible options I can go for.

If it is AVRO then I could have used directly AvroSerDe. But the source file in this case is JSON.

{  
   "subscriberId":"vfd1234-07e1-4054-9b64-83a5a20744db",
   "cartId":"1234edswe-6a9c-493c-bcd0-7fb71995beef",
   "cartStatus":"default",
   "salesChannel":"XYZ",
   "accountId":"12345",
   "channelNumber":"12",
   "timestamp":"Dec 12, 2013 8:30:00 AM",
   "promotions":[  
      {  
         "promotionId":"NEWID1234",
         "promotionContent":{  
            "has_termsandconditions":[  
               "TC_NFLMAXDEFAULT16R103578"
            ],
            "sequenceNumber":"305",
            "quantity":"1",
            "promotionLevel":"basic",
            "promotionDuration":"1",
            "endDate":"1283142400000",
            "description":"Regular Season One Payment",
            "active":"true",
            "disableInOfferPanel":"true",
            "displayInCart":"true",
            "type":"promotion",
            "frequencyOfCharge":"weekly",
            "promotionId":"NEWID1234",
            "promotionIndicator":"No",
            "shoppingCartTitle":"Regular Season One Payment",
            "discountedPrice":"0",
            "preselectedInOfferPanel":"false",
            "price":"9.99",
            "name":"Regular Season One Payment",
            "have":[  
               "CatNFLSundayMax"
            ],
            "ID":"NEWID1234",
            "startDate":"1451365600000",
            "displayInOfferPanel":"true"
         }
      }
   ]
}

I did tried to create a table using org.openx.data.jsonserde.JsonSerDe, but it is not showing me the data.

CREATE EXTERNAL TABLE test1
(
SUBSCRIBER_ID string,
CART_ID string,
CART_STAT_NAME string,
SLS_CHAN_NAME string,
ACCOUNT_ID string,
CHAN_NBR string,
TX_TMSTMP string,
PROMOTION ARRAY<STRING>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION '<HDFS location where the json file is place in single line>';

Answer

Indrajit Swain picture Indrajit Swain · Jan 4, 2017

Not sure about the JsonSerDe you are using . Bu here this JsonSerDe you can use for you.Hive-JSON-Serde

hive> add jar /User/User1/json-serde-1.3.8-SNAPSHOT-jar-with-dependencies.jar;
Added [/User/User1/json-serde-1.3.8-SNAPSHOT-jar-with-dependencies.jar] to class path
Added resources: [/User/User1/json-serde-1.3.8-SNAPSHOT-jar-with-dependencies.jar]
hive> use default;
OK
Time taken: 0.021 seconds
hive> CREATE EXTERNAL TABLE IF NOT EXISTS json_poc (
> alertHistoryId bigint, entityId bigint, deviceId string, alertTypeId int,  AlertStartDate string
> )
> ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
> LOCATION '/User/User1/sandeep_poc/hive_json';
OK
Time taken: 0.077 seconds
hive> select * from json_poc;
OK
123456  123     123     1       jan 04, 2017 2:46:48 PM
Time taken: 0.052 seconds, Fetched: 1 row(s)

How to build jar. Maven should be installed on your PC then run command like this.

C:\Users\User1\Downloads\Hive-JSON-Serde-develop\Hive-JSON-Serde-develop>mvn -Phdp23 clean package.

-Phdp23 is hdp2.3 it should be replaced with your hadoop version.

Or if you want to use inbuilt JsonSerde get_json_object json_tuple if you are looking for an example how to use see this blog Hive-JSON-Serde example .

I will recommend validate your JSON file as well.JSON Validator