Parse json arrays using HIVE

前端 未结 3 1102
野趣味
野趣味 2020-12-05 08:04

I have many json arrays stored in a table (jt) that looks like this:

[{\"ts\":1403781896,\"id\":14,\"log\":\"show\"},{\"ts\":1403781896,\"id\":14,\"log\":\"         


        
3条回答
  •  庸人自扰
    2020-12-05 08:54

    Use explode() function

     hive (default)> CREATE TABLE logs AS
                      >   SELECT get_json_object(single_json_table.single_json, '$.ts') AS ts,
                      >   get_json_object(single_json_table.single_json, '$.id') AS id,
                      >   get_json_object(single_json_table.single_json, '$.log') AS log
                      >   FROM
                      >     (SELECT explode(json_array_col) as single_json FROM jt) single_json_table ;
    
    Automatically selecting local only mode for query
    Total MapReduce jobs = 3
    Launching Job 1 out of 3
    Number of reduce tasks is set to 0 since there's no reduce operator
    
    hive (default)> select * from logs;
    OK
    ts      id      log
    1403781896      14      show
    1403781896      14      start
    1403781911      14      press
    1403781911      14      press
    Time taken: 0.118 seconds, Fetched: 4 row(s)
    hive (default)>
    

    where json_array_col is column in jt which holds your array of jsons.

    hive (default)> select json_array_col from jt;
    json_array_col
    ["{"ts":1403781896,"id":14,"log":"show"}","{"ts":1403781896,"id":14,"log":"start"}"]
    ["{"ts":1403781911,"id":14,"log":"press"}","{"ts":1403781911,"id":14,"log":"press"}"]
    

提交回复
热议问题