😊 이전 시간에 Spark로 다룬 데이터를 Hive로 다루기
경로는 hdfs위에 이전에 다뤘던 Json 파일들을 적재
CREATE EXTERNAL TABLE http_raw_tbl (
`host` string,
`timestamp` string,
`request` string,
`http_reply` int,
`bytes` int
)
PARTITIONED BY(
`type` string,
`yyyymmdd` string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 'hdfs://namenode:9000/user/hive/warehouse/http_raw_tbl';
타입은 일단 나중에 추가될 타입이 있다고 가정하고 http 로 지정하였고 매일 디렉토리에 로그 파일들을 적재한다고 가정하여 yyyymmdd 로 설정
Location은 데이터를 올려놓은 위치를 지정
ALTER TABLE http_raw_tbl ADD partition (type='http',yyyymmdd='20230408') LOCATION 'hdfs://namenode:9000/logs/http/20230408';
CREATE TABLE `http.log_tbl`(
`host` string,
`info_detail` string
)
PARTITIONED BY (
`yyyymmddhh` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
'hdfs://namenode:9000/user/hive/warehouse/http.db/log_tbl'
TBLPROPERTIES (
'bucketing_version'='2',
'transient_lastDdlTime'='1677478293');
WITH base_tbl as(
SELECT host
,`timestamp` as tmp
,request
,http_reply
,bytes
from http_raw_tbl
WHERE yyyymmdd='20230408'
)
SELECT
base_tbl.host
,collect_list(
named_struct('cnt',cnt,'timestamp',tmp,'request',request,'http_reply',http_reply,'bytes',bytes)
) as json_result
FROM
(
SELECT host
,count(*) as cnt
FROM base_tbl
GROUP BY host
)as cnt_tbl
INNER JOIN base_tbl
ON base_tbl.host = cnt_tbl.host
GROUP by base_tbl.host;