Manish Kumar
Manish Kumar

Reputation: 245

How to parse nested Json structure in Hive?

I have json like the following in hadfs.

{"result": [{"sys_tags": {"display_value": "d1", "value": "v1"}, "user_input": {"display_value": "d2", "value": "v2"}}, {"sys_tags": {"display_value": "d1", "value": "v1"}, "user_input": {"display_value": "d2", "value": "v2"}}]}

I want to create an external table in hive to analyse the data.

I downloaded json-serde-1.3.7-jar-with-dependencies.jar and added in in hive shell. here is the query i ran

CREATE EXTERNAL TABLE t2(result array<STRUCT<sys_tags STRUCT<display_value :STRING, value:STRING>>, STRUCT<user_input STRUCT<display_value :STRING, value:STRING>>>) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'  location 'hdfs://localhost:9000/t2';

But it' not working. Can anybody help to figure out the issue?

Upvotes: 0

Views: 3228

Answers (2)

franklinsijo
franklinsijo

Reputation: 18270

There are a few mismatched < and missing : in the CREATE statement.

Try,

CREATE EXTERNAL TABLE t2(
       result array<STRUCT<sys_tags:STRUCT<display_value:STRING, value:STRING>,user_input:STRUCT<display_value:STRING, value:STRING>>>) 
       ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'  
       location 'hdfs://localhost:9000/t2';

Upvotes: 1

Rahul Sharma
Rahul Sharma

Reputation: 5834

It's failing because the json attributes are not mapped properly, try this out:

CREATE EXTERNAL TABLE t2(result MAP<STRING, ARRAY<MAP<STRING,STRUCT<display_value :STRING, value:STRING>>>>) ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'  location 'hdfs://localhost:9000/t2';

Upvotes: 0

Related Questions