LoginSignup
7
7

More than 5 years have passed since last update.

Amazon Athenaのパーティション追加めんどくさい

Posted at

S3のバケットに

s3://バケット名/HOGE/20170824/00_0.gz
s3://バケット名/HOGE/20170824/00_1.gz

みたいな感じでログを置いています。

Athenaを利用して軽く集計してみようかなーと思ったところ、パーティションを分ける必要がありました。
(分けないでも実行できるけど、よりお金かかるから分けたほうがいい)

Hiveフォーマットに沿って格納されていれば簡単だけど、上記のように 20170824 だとそれぞれでAlter Tableを叩かないといけないのでめんどくさい・・・

ので、開始日と終了日を指定してわーっとAlter Table叩いてくれるShell Scriptを書きました。

#!/bin/sh

STARTDATE=2016-11-01
ENDDATE=2020-10-31

CURRENTDATE=$STARTDATE
while [ 1 ] ; do
        CURRENTYEAR=`date -d "$CURRENTDATE" "+%Y"`
        CURRENTMONTH=`date -d "$CURRENTDATE" "+%m"`
        CURRENTDAY=`date -d "$CURRENTDATE" "+%d"`

        aws --region ap-northeast-1 athena start-query-execution --query-string "ALTER TABLE データベース名.テーブル名 ADD PARTITION (year=$CURRENTYEAR,month=$CURRENTMONTH,day=$CURRENTDAY) location 's3://バケット名/HOGE/$CURRENTYEAR$CURRENTMONTH$CURRENTDAY/'" --result-configuration OutputLocation=s3://結果出力先

        if [ $CURRENTDATE = $ENDDATE ] ; then
                break
        fi

        CURRENTDATE=`date -d "$CURRENTDATE 1day" "+%Y-%m-%d"`
done

おしまい。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7