Search code examples
jsonhivehadoop2hive-serde

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?


Solution

  • 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';