- A processar o JSON.

estou a tentar obter alguns valores de JSON aninhado para milhões de linhas (5 TB+ table). Qual é a maneira mais eficiente de fazer isso?

aqui está um exemplo:

{"country":"US","page":227,"data":{"ad":{"impressions":{"s":10,"o":10}}}}

preciso destes valores do JSON acima:

Country        Page      impressions_s       impressions_o
---------      -----     -------------       --------------
US              2        10                  10

Esta é a função json_tuple De Hive, Não tenho certeza se esta é a melhor função. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-getjsonobject

Author: Don P, 2012-09-28

4 answers

Pode usar o get_ json_object:

 select get_json_object(fieldname, '$.country'), 
        get_json_object(fieldname, '$.data.ad.s') from ... 
Vais ter melhor desempenho com o json_tuple, mas encontrei um "como" para obter os valores no json dentro do json.; Para formar a sua mesa pode usar algo assim:

from table t lateral view explode( split(regexp_replace(get_json_object(ln, ''$.data.ad.s'), '\\[|\\]', ''), ',' ) ) tb1 as s este código acima irá transformar você "Array" em uma coluna.

Forma mais: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

Espero que isto ajude ...
 17
Author: Wemerson Cesar, 2015-03-18 16:58:35
Aqui está o que você pode tentar rapidamente, eu sugiro usarJson-Ser-de .

Nano / tmp / hive-parsing-json.json

{"country":"US","page":227,"data":{"ad":{"impressions":{"s":10,"o":10}}}}

Criar a tabela base:

hive > CREATE TABLE hive_parsing_json_table ( json string );

Carregar o ficheiro json para a Tabela:

hive > LOAD DATA LOCAL INPATH  '/tmp/hive-parsing-json.json' INTO TABLE hive_parsing_json_table;

Consultar a tabela:

hive >  select v1.Country, v1.Page, v4.impressions_s, v4.impressions_o 
from hive_parsing_json_table hpjp
     LATERAL VIEW json_tuple(hpjp.json, 'country', 'page', 'data') v1
     as Country, Page, data
     LATERAL VIEW json_tuple(v1.data, 'ad') v2
     as Ad
     LATERAL VIEW json_tuple(v2.Ad, 'impressions') v3
     as Impressions
     LATERAL VIEW json_tuple(v3.Impressions, 's' , 'o') v4
     as impressions_s,impressions_o;  

Resultado:

v1.country  v1.page     v4.impressions_s    v4.impressions_o
US      227     10          10
 8
Author: Sanjiv, 2015-07-27 06:18:37
Se usares a colmeia nativa, podes fazer isto.. Aqui estão os passos

Adicionar JAR / path/to / hive-hcatalog-core.jar;

create a table as below 
 CREATE TABLE json_serde_nestedjson (
  country string,
  page int,
  data struct < ad: struct < impressions: struct < s:int, o:int  > > >
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe';

Depois carregar os dados (armazenados no ficheiro)

LOAD DATA LOCAL INPATH '/tmp/nested.json' INTO TABLE json_serde_nestedjson;

Obter os dados necessários utilizando

SELECT country, page, data.ad.impressions.s, data.ad.impressions.o 
FROM json_serde_nestedjson;  
 3
Author: Hemantha Kumara M S, 2017-08-31 09:15:05

Implementar um SerDe para analisar os seus dados em JSON é uma maneira melhor para o seu caso.

Um tutorial sobre como implementar SerDe para processar JSON pode ser encontrado aqui

Http://blog.cloudera.com/blog/2012/12/how-to-use-a-serde-in-apache-hive/

Também pode utilizar a seguinte implementação do serde de amostra

Https://github.com/rcongiu/Hive-JSON-Serde

 1
Author: HuntingCheetah, 2013-02-24 18:48:13