Athenaで後からパーティションを追加する


Athenaでパーティションを利用する

Athenaでパーティションを利用しようとする場合、このようなS3の構成が求められる

aws s3 ls s3://elasticmapreduce/samples/hive-ads/tables/impressions/

PRE dt=2009-04-12-13-00/
PRE dt=2009-04-12-13-05/
PRE dt=2009-04-12-13-10/
PRE dt=2009-04-12-13-15/
PRE dt=2009-04-12-13-20/
PRE dt=2009-04-12-14-00/
PRE dt=2009-04-12-14-05/
PRE dt=2009-04-12-14-10/
PRE dt=2009-04-12-14-15/
PRE dt=2009-04-12-14-20/
PRE dt=2009-04-12-15-00/
PRE dt=2009-04-12-15-05/

データのパーティション分割より

dt=という形式にしてパーティションを作成します。ただ何も考えずにディレクトリを分けていた場合これが適用できません。
今回は後からパーティションを追加する方法を考えます。

パーティション名を含んだcreate文

create.sql
CREATE EXTERNAL TABLE impressions (
    requestBeginTime string,
    adId string,
    impressionId string,
    referrer string,
    userAgent string,
    userCookie string,
    ip string,
    number string,
    processId string,
    browserCookie string,
    requestEndTime string,
    timers struct<modelLookup:string, requestTime:string>,
    threadId string,
    hostname string,
    sessionId string)
PARTITIONED BY (dt string)
ROW FORMAT  serde 'org.apache.hive.hcatalog.data.JsonSerDe'
    with serdeproperties ( 'paths'='requestBeginTime, adId, impressionId, referrer, userAgent, userCookie, ip' )
LOCATION 's3://elasticmapreduce/samples/hive-ads/tables/impressions/' ;

上記はAWSのリンクにあったものです。create文にPARTITIONED BYを入れ込みます。
月・日でディレクトリを切っている場合はPARTITIONED BY (year int, month int)のようになります。
そしたらそのテーブルに対してパーティションを加えます。

alter.sql
ALTER TABLE テーブル名 ADD PARTITION (year='2018',month='01') location 's3://バケット名/path/to/2018/12/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='02') location 's3://バケット名/path/to/2019/01/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='02') location 's3://バケット名/path/to/2019/02/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='03') location 's3://バケット名/path/to/2019/03/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='04') location 's3://バケット名/path/to/2019/04/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='05') location 's3://バケット名/path/to/2019/05/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='06') location 's3://バケット名/path/to/2019/06/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='07') location 's3://バケット名/path/to/2019/07/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='08') location 's3://バケット名/path/to/2019/08/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='09') location 's3://バケット名/path/to/2019/09/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='10') location 's3://バケット名/path/to/2019/10/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='11') location 's3://バケット名/path/to/2019/11/';
ALTER TABLE テーブル名 ADD PARTITION (year='2019',month='12') location 's3://バケット名/path/to/2019/12/';
ALTER TABLE テーブル名 ADD PARTITION (year='2020',month='01') location 's3://バケット名/path/to/2020/01/';

このようにパーティションを加えます。
そうするとSQLでmonthとyearの指定が可能となります

select.sql
SELECT count(distinct id)
FROM テーブル名
WHERE year = 2019
    AND month IN (11, 12)

作成したパーティションは

SHOW PARTITIONS table_name

year=2019/month=03
year=2019/month=12
year=2019/month=10
year=2019/month=07
year=2019/month=06
year=2019/month=05
year=2019/month=11
year=2020/month=01
year=2019/month=09
year=2019/month=01
year=2019/month=02
year=2019/month=04
year=2018/month=01
year=2019/month=08

のように確認可能です。