0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Athena v3 は DDL でのテーブル定義に制限がある

0
Posted at

概要

Athena コンソールで使うテーブルは Glue Data Catalog 上の定義。エンジン v3(Trino) では Hive 形式の CREATE EXTERNAL TABLE が使いづらいので、AWS::Glue::Database / AWS::Glue::Table で IaC 化する。
DDLで定義する記事がほとんどで、時間がかかってしまったのでまとめておく

要約

  • Athena の「データベース」「テーブル」は AWS::Glue::DatabaseAWS::Glue::TableTableInput で作る。AWS::Athena::* にテーブル作成リソースはない
  • Athena v3 では、クエリエディタで Hive 互換の外部テーブル DDL がそのまま通らなかった。 Glue API または CFn でカタログ登録する方が確実
  • パーティション射影・JsonSerDe・STRUCT 列・storage.location.template${partition_key} は、TableInputに CFn で書く

v2 と v3 の違い(テーブル作成の観点)

観点 v2 v3(Trino)
DDL Hive 互換 CREATE EXTERNAL TABLE が使えることが多い 同じ DDL が失敗しやすい
カタログ Glue を参照 同左(登録先は Glue のまま)
ネスト列 SerDe + 列定義が重要 STRUCT はドット記法で書きやすいが、フィールド名は小文字化されることがある

v3 のクエリエディタで、次のような Hive 形式 DDL(パーティション射影・OpenX JsonSerDe・STRUCTTBLPROPERTIES 付き)をそのまま流すとうまくいかない

CREATE EXTERNAL TABLE example_logs (
  version string,
  id string,
  `detail-type` string,
  detail struct<...>
)
PARTITIONED BY (event_date string)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ('mapping.detail_type' = 'detail-type')
LOCATION 's3://my-bucket/'
TBLPROPERTIES (
  'projection.enabled' = 'true',
  'projection.event_date.type' = 'date',
  'projection.event_date.format' = 'yyyy/MM/dd',
  'storage.location.template' = 's3://my-bucket/${event_date}'
);

対処: Glue に直接定義する。

  • CLI: aws glue create-table --database-name ... --table-input file://...
    • jsonファイルでテーブル定義して読み込ませる形式
  • IaC: CloudFormation の TableInput を定義

↓CFnのサンプル

ExampleGlueTable:
  Type: AWS::Glue::Table
  Properties:
    CatalogId: !Ref AWS::AccountId
    DatabaseName: !Ref ExampleGlueDatabase
    TableInput:
      Name: !Ref GlueTableName
      TableType: EXTERNAL_TABLE
      Parameters:
      PartitionKeys:
        - Name: event_date
          Type: string
      StorageDescriptor:
        Location: !Sub 's3://${S3BucketName}/'
        InputFormat: org.apache.hadoop.mapred.TextInputFormat
        OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
        Compressed: false
        SerdeInfo:
          SerializationLibrary: org.openx.data.jsonserde.JsonSerDe
          Parameters:
            'mapping.detail_type': 'detail-type'
        Columns:
          - Name: version
            Type: string
          # ...

参考リンク

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?