본문 바로가기
💻Tech/🐝HIVE

[Hive] 테이블 JSON 포맷 사용 방법

by _viper_ 2020. 3. 27.
반응형

1. JSON 데이터 확인

2. Hive 데이터베이스/테이블 생성

  •  json 중첩 객체가 존재할 경우 string으로 처리
  •  json 컬럼 type 주의 (arrary)
    - 컬럼 List type을 String으로 설정해서 아래 에러가 발생함.  org.apache.hadoop.hive.serde2.SerDeException: java.io.IOException: Field name expected
CREATE DATABASE IF NOT EXISTS db_nm;

CREATE EXTERNAL TABLE IF NOT EXISTS db_nm.tb_nm ( 
  repotype string, 
  repo string, 
  requser string, 
  tags array<string>
) 
PARTITIONED BY (dt string) 
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
STORED AS TEXTFILE 
LOCATION 'hdfs://name/tmp/log' 
;

3. Hive 테이블 파티션 등록

  • hdfs경로의 파티션이(dt=20200326)와 같은 경우가 아니므로 직접 파티션과 hdfs 경로 매핑필요
ALTER TABLE db_nm.tb_nm ADD PARTITION (dt='20200326') LOCATION '/tmp/log/20200326';

 

 

🔎 참고

  • STORED AS JSONFILE 구문으로도 사용 가능
CREATE EXTERNAL TABLE IF NOT EXISTS db_nm.tb_nm ( 
  repotype string, 
  repo string, 
  requser string, 
  tags array<string>
) 
PARTITIONED BY (dt string) 
STORED AS JSONFILE
LOCATION 'hdfs://name/tmp/log' 
;