json을 정보를 저장하는 테이블에 view를 생성해서 json없는 테이블처럼 사용할 수 있다. 

(더 정확히 말하면 general하게 로그 수집을 위한 모델링할 수 있는 로그에 json를 사용할 수 있으면서 

쉽게 정보를 파악할 수 있는 방법이다)




아래와 같은 row를 가진 hive 테이블이 있다. 


1787821114291 service 15 birthday {"result":true,"birth":"0224","id":"15","action":"add"}  2011-02-23


이런 hive테이블을 조회하려면 아래와 같이 실행해야 한다.


select * from googleplus_reqlog where date_id='2011-02-23' and label='birthday' and get_json_object(props, '$.id')='15';





hive 질의 시 get_json_object를 사용하는 것이 불편하기 때문에 새로운 view를 만든다.


CREATE VIEW `stat`.`birthday` COMMENT 'google plus birthday' AS SELECT

`birthday_temp`.`ts` AS `ts`,

`birthday_temp`.`date_id` AS `date_id`,

`birthday_temp`.`result` AS `result`,

`birthday_temp`.`birth` AS `birth`,

`birthday_temp`.`id` AS `id`,

`birthday_temp`.`action` AS `action`,

`birthday_temp`.`from` AS `from`

FROM (

  select

    ts,

    date_id,

    get_json_object(`googleplus_reqlog`.`props`, '$.result') as result,

    get_json_object(`googleplus_reqlog`.`props`, '$.birth') as birth,

    get_json_object(`googleplus_reqlog`.`props`, '$.id') as id,

    get_json_object(`googleplus_reqlog`.`props`, '$.action') as action,

  from `src`.`googleplus_reqlog`

  where label='birthday') `birthday_temp`;

  

  

  

view를 확인한다. 


hive> desc birthday;

OK

ts                   bigint

date_id             string

result               string

birth               string

id           string

action               string




이제 get_json_object 없이 간단하게 hive 질의를 할 수 있다. 

  

 select * from birthday where date_id='2011-02-23' and account_id='15';

  


처음에 소개한 hive 질의와 동일한 결과를 내지만, 훨씬 편하다.


select * from googleplus_reqlog where date_id='2011-02-23' and label='birthday' and get_json_object(props, '$.id')='15';



Posted by '김용환'
,