Athena Partition Projection を使用したテーブルのパーティショニング自動化
Athena Partition Projection は2020年6月にサポートされ、 Athena テーブルの自動パーティショニングが可能になります。この機能を使用すると、新しいパーティションを追加するために MSCK REPAIR TABLE
は不要になります。
概要
公式 AWS ドキュメントからの引用です。
In partition projection, partition values and locations are calculated from configuration rather than read from a repository like the AWS Glue Data Catalog. 1
partition projection can reduce the runtime of queries against highly partitioned tables. 2
Partition projection allows Athena to avoid calling
GetPartitions
because the partition projection configuration gives Athena all of the necessary information to build the partitions itself. 3
AWS リソース作成
以下の内容で CloudFormation テンプレートを作成してください。重要なのは、 GlueTable
定義の GlueTable.TableInput.Parameters
(27-35行目)です。
projection.year_month.range
プロパティは UTC として扱われます。 S3 オブジェクトのパスで異なるタイムゾーンを使用する場合は、 NOW+9HOURS
のような形式で時間差を指定する必要があります。詳細については、公式ドキュメントをご参照ください。 AWSTemplateFormatVersion: "2010-09-09"
Description: Stack for Athena partition projection sample
Resources:
S3:
Type: AWS::S3::Bucket
Properties:
BucketName: athena-partition-projection-logs
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
GlueDatabase:
Type: AWS::Glue::Database
Properties:
DatabaseInput:
Name: sample
CatalogId: !Ref AWS::AccountId
GlueTable:
Type: AWS::Glue::Table
Properties:
DatabaseName: !Ref GlueDatabase
CatalogId: !Ref AWS::AccountId
TableInput:
TableType: EXTERNAL_TABLE
Parameters:
classification: json
"projection.enabled": true
"projection.year_month.format": yyyy/MM
"projection.year_month.interval": 1
"projection.year_month.interval.unit": MONTHS
"projection.year_month.range": 2021/09,NOW
"projection.year_month.type": date
"storage.location.template": s3://athena-partition-projection-logs/${year_month}
StorageDescriptor:
Columns:
- Name: id
Type: int
- Name: message
Type: string
Location: !Sub s3://${S3}/
InputFormat: org.apache.hadoop.mapred.TextInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed: false
NumberOfBuckets: 0
SerdeInfo:
SerializationLibrary: org.openx.data.jsonserde.JsonSerDe
Parameters:
paths: id,message
StoredAsSubDirectories: false
PartitionKeys:
- Name: year_month
Type: string
Retention: 0
Name: sample_logs
以下のコマンドで CloudFormation スタックをデプロイしてください。
aws cloudformation deploy --template-file stack.yml --stack-name athena-partition-projection-sample
テスト
以下のコマンドを実行して、次の例の JSON を含むオブジェクトを配置してください。
$ echo '{"id": 1, "message": "hello"}' > 2021-09.json
$ echo '{"id": 2, "message": "world"}' > 2021-10.json
$ aws s3 cp 2021-09.json s3://athena-partition-projection-logs/2021/09/
$ aws s3 cp 2021-10.json s3://athena-partition-projection-logs/2021/10/
$ aws s3 ls s3://athena-partition-projection-logs/2021/
PRE 09/
PRE 10/
2021/09
パーティションのデータをクエリしてみましょう。
SELECT * FROM "sample"."sample_logs"
WHERE year_month = '2021/09'
LIMIT 10;
結果
1 hello 2021/09
次に、 2021/10
パーティションのデータをクエリしてみましょう。
SELECT * FROM "sample"."sample_logs"
WHERE year_month = '2021/10'
LIMIT 10;
結果
2 world 2021/10
クリーンアップ
以下のコマンドを使用して、プロビジョニングされた AWS リソースを削除してください。
aws s3 rm --recursive s3://athena-partition-projection-logs
aws cloudformation delete-stack --stack-name athena-partition-projection-sample