Upgrade to Pro — share decks privately, control downloads, hide ads and more …

サーバレスなAmazon Athenaで始めるTrinoとTransactional Data...

Sotaro Hikita
October 06, 2023
900

サーバレスなAmazon Athenaで始めるTrinoとTransactional Data Lake

Sotaro Hikita

October 06, 2023
Tweet

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. サーバレスなAmazon Athena で始める TrinoとTransactional Data Lake Sotaro Hikita Amazon Web Services Japan, G.K. Solution Architect 2023/10/5
  2. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. ⽦⽥ 宗太郎 (ひきた そうたろう) ソリューションアーキテクト ⾦融業界の総合的なクラウド活⽤⽀援 + 多様な業界のデータ分析系サービス活⽤⽀援 • 好きな AWS サービス • Amazon Athena • AWS Glue • Amazon DataZone 自己紹介
  3. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. 3 Amazon Athena 概要
  4. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. データ ユーザー 前処理 (ETL) 収集 蓄積 分析 活用 データ分析のパイプライン データレイク (Amazon S3) Amazon Athena は ここを担当 ! Amazon Athena は、アドホックな「分析」を主⽬的とした サーバーレスのサービス 4
  5. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 様々なデータソースへのSQLクエリをサーバレスに実現する AWS Glue データカタログ データソース定義 (AwsDataCatalog) Amazon Athena for SQL … データコネクタ対応ソース データソース定義 (MyDataSource-1) ユーザー データソース定義 (MyDataSource-N) DynamoDB DocumentDB CloudWatch Redis Timestream Kafka … 他SaaS 等 … OpenSearch データコネクタを介してアクセス (実体はLambda関数) Amazon S3 データソース定義 (MyDataSource-2) Athena Federated Query 標準SQL Amazon Athenaとは アプリ (連携AWS サービス含む) 5
  6. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. AWS Glue データカタログ データソース定義 (AwsDataCatalog) Amazon Athena for SQL … データコネクタ対応ソース データソース定義 (MyDataSource-1) ユーザー アプリ (連携AWS サービス含む) データソース定義 (MyDataSource-N) DynamoDB DocumentDB CloudWatch Redis Timestream Kafka … 他SaaS 等 … OpenSearch データコネクタを介してアクセス (実体はLambda関数) Amazon S3 データソース定義 (MyDataSource-2) Athena Federated Query 標準SQL Amazon Athenaとは 様々なデータソースへのSQLクエリをサーバレスに実現する 6
  7. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. AWS Glue データカタログ データソース定義 (AwsDataCatalog) Amazon Athena for SQL … データコネクタ対応ソース データソース定義 (MyDataSource-1) ユーザー アプリ (連携AWS サービス含む) データソース定義 (MyDataSource-N) DynamoDB DocumentDB CloudWatch Redis Timestream Kafka … 他SaaS 等 … OpenSearch データコネクタを介してアクセス (実体はLambda関数) Amazon S3 データソース定義 (MyDataSource-2) Athena Federated Query 標準SQL Amazon Athenaとは 様々なデータソースへのSQLクエリをサーバレスに実現する 7
  8. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. クエリエディタから 即座にクエリを発行可能 データソースは Hive Metastore 互換の サーバレスなデータカタログ (Glue DataCatalog)で管理 JDBC / ODBC / API 経由で BI ツールやシステムとの 連携も可能 Amazon Athena 利用イメージ データソース/DB/ テーブルスキーマの表⽰ クエリ結果 (CSVダウン ロード可能) クエリを⼊⼒ 8
  9. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. "サーバーレス"とは... サーバーがない? サーバーの存在を意識しない サーバーがない 9
  10. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. サーバーの存在を意識しない • サーバーの起動、シャットダウン、健全性のモニタリング • セキュリティパッチの適用 • キャパシティ管理、オートスケーリングの設計・設定 例えば...以下の項目についてはお客様側での実施は不要 リクエストやイベントが来ると AWS 側で自動的に起動し、不要になると終了される メンテナンス、セキュリティパッチの適用は AWS 側で実施される リクエストやイベント数に応じて自動的にスケールする 10
  11. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Presto/Trinoベースであり、バージョン管理されている 使⽤バージョンはユーザーごとに切り替え可能 # SQLエンジン Ver ベースとなるエ ンジン リリース 新機能 1 v3 Trino 2022/10/13 エンジンがTrinoベースへ、Open Table Format サポート拡大、クエリ結果キャッシュ/再利用 対応など 2 v2 Presto 2020/11/13 EXPLAIN対応、Federated Query対応など 3 v1 Presto 2016/11/30 GA時点リリース ※現在は利用不可 各ユーザー環境 エンジンバージョン ※ 同一バージョン内でも不定期にサービスアップデートが行われる https://docs.aws.amazon.com/athena/latest/ug/release-notes.html Athena エンジンバージョン 11
  12. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. https://docs.aws.amazon.com/athena/latest/ug/engine-versions-reference-0003.html#engine- versions-reference-0003-improvements-and-new-features Athena エンジンバージョン 3 • 機能面 • Trinoプロジェクトの50以上の関数と30以上の新機能を新たにサポート • 地理空間関数、T-digest 関数、MATCH_RECOGNIZE演算子など • クエリ結果のキャッシュ / 再利用サポート • Open Table Format のサポート拡大 • 非機能面 • パフォーマンス向上 • スキャン量削減によるコスト効率の向上 • メモリ効率の改善による安定性向上 12
  13. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. T P C - D S ベ ン チ マ ー ク の ク エ リ ( 縦 軸 ) に お け る 、 バ ー ジ ョ ン 2 に 対 す る 改 善 の 割 合 ( 横 軸 ) エンジンバージョン 3での性能向上 https://aws.amazon.com/jp/blogs/big-data/upgrade-to-athena-engine-version-3-to-increase- query-performance-and-access-more-analytics-features/ クエリ実行時間 クエリプランニングに要する時間 • データカタログとの統合の改善な どにより、クエリプランニングと 実行時間の両面で高速化 • パフォーマンスが最大10倍向上 • スキャンデータ量が最適化され、 コストが最大70%削減 13
  14. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Amazon Athena の特徴 サーバレスでインフラ管理不要 ⼤規模データに対しても⾼速なクエリ 事前のデータロードなしで様々なデータソースにアクセス スキャンしたデータに対しての従量課⾦ JDBC / ODBC / API 経由で BI ツールやシステムと連携 14
  15. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Athena ACID サポートと Transactional Data Lake
  16. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. AWS で⼀般的な Data Lake の構成 Storage (Amazon S3) table/year=2020/file1.parquet /year=2020/file2.parquet /year=2020/file3.parquet /year=2021 /year=2022 … Hive Metastore (Glue DataCatalog) table Data Ingestion Query Engine Analysis Data Source 16
  17. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 従来の Data Lake の限界 Storage (Amazon S3) table/year=2020/file1.parquet /year=2020/file2.parquet /year=2020/file3.parquet … Hive Metastore (Glue DataCatalog) table Data Ingestion Query Engine Analysis Data Source • レコードレベルの Read / Write が非効率 • テーブルが大きくなるに従ってテーブル操作が高コスト化 • 同時アクセス時の整合性を担保できない • データが追加、更新されると過去の状態が復元できない • スキーマ変更への追従の負荷が大きい • クエリを実行するユーザがパーティション構造を把握する必要がある 17
  18. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 「Transactional な Data Lake」のニーズ • ストリームパイプライン • 書込みと読込みのパフォーマンスの両立(スモールファイルへの対応) • 重複レコードによる分析の複雑化の回避 • ソースデータのスキーマ変更への追従 • CDC(Change Data Capture) • ターゲットテーブルへのUPSERT • 継続的に更新されるデータへの分析時のトランザクション担保 • プライバシーポリシーへの対応 • 決まった時間内に特定の個人情報(主にレコードベース)を 削除する必要がある • データ削除中のトランザクションの担保 • データサイエンス • データサイエンスでは、実験を再現できることが重要 • ある特定の時点のデータを利用した分析を再現できる必要がある 18
  19. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Distributed engine layer OTFs’ layer Data layer Read consistent/old data Write data with rollback support change/delete single or multiple records Read/Write through open table format https://pages.awscloud.com/rs/112-TZM-766/images/AWS-Black-Belt_2023_Datalake-Format- On-AWS_0516_v1.pdf 19 Open Table Format (OTF) レイヤーを挟んで 高度なニーズに対応する
  20. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 対応フォーマット 説明 読取り 書込み Apache Iceberg • Iceberg Format v2 をサポート • Apache Iceberg の幅広い機能をサポート • MERGE INTO 句はエンジンv3のみ対応 ◯ ◯ Apache Hudi • Hudi v0.12.2(エンジンv3), v0.8.0(エンジンv2) • 書き込みは末サポート • 増分クエリは末サポート ◯ × Delta Lake • Glueデータカタログのみ対応 • タイムトラベル機能は未サポート • エンジンv3のみ対応 & 対応DDLは限定的 ◯ × Athena の Open Table Format 対応状況 ※ Athena エンジンバージョン、Format ごとにサポートされる機能の範囲、制約が異なる点に注意 https://docs.aws.amazon.com/ja_jp/athena/latest/ug/acid-transactions.html ※ AWSにおける Hudi/Iceberg/Delta Lake の 使いどころと違いについて https://pages.awscloud.com/rs/112-TZM-766/images/AWS-Black-Belt_2023_Datalake-Format-On-AWS_0516_v1.pdf 20
  21. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 対応フォーマット 説明 読取り 書込み Apache Iceberg • Iceberg Format v2 をサポート • Apache Iceberg の幅広い機能をサポート • MERGE INTO 句はエンジンv3のみ対応 ◯ ◯ Apache Hudi • Hudi v0.12.2(エンジンv3), v0.8.0(エンジンv2) • 書き込みは末サポート • 増分クエリは末サポート ◯ × Delta Lake • Glueデータカタログのみ対応 • タイムトラベル機能は未サポート • エンジンv3のみ対応 & 対応DDLは限定的 ◯ × Athena の Open Table Format 対応状況 ※ Athena エンジンバージョン、Format ごとにサポートされる機能の範囲、制約が異なる点に注意 https://docs.aws.amazon.com/ja_jp/athena/latest/ug/acid-transactions.html ※ AWSにおける Hudi/Iceberg/Delta Lake の 使いどころと違いについて https://pages.awscloud.com/rs/112-TZM-766/images/AWS-Black-Belt_2023_Datalake-Format-On-AWS_0516_v1.pdf 21
  22. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Athena の Iceberg サポートが Data Lake の既存課題を解決 • レコードレベルの Read / Write が非効率 • テーブルを構成するファイル/パーティションの増加に従ってテーブル操作が高コスト化 → メタデータの工夫や統計情報の活用による高速なクエリ計画 & スキャン • 同時アクセス時の整合性が確保できない → Reader, Writer は同時アクセス時に独立したデータ操作が可能 • データが追加、更新されると過去の状態が復元できない → テーブルの変更断面(Snapshot)が保存され、過去のある時点や断面の参照、巻き戻しが可能 • スキーマ変更への追従の負荷が大きい → カラムの追加、削除、更新、名前の変更(Schema Evolution)をサポート • パーティション構造を活かすには、 クエリを実行するユーザがテーブルの物理構造を把握する必要がある → hidden partitioning によって、ユーザがテーブルの物理構造を意識せず効率的にクエリ可能 22 https://iceberg.apache.org/spec/
  23. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. TBL_PROPERTIES句でtable_typeプロパティを‘ICEBERG’に設定 ※ Iceberg テーブル作成時は EXTERNAL キーワードを使用しない点に注意 CREATE TABLE [db_name.]table_name (col_name data_type [COMMENT col_comment] [, ...] ) [PARTITIONED BY (col_name | transform, ... )] LOCATION 's3://DOC-EXAMPLE-BUCKET/[your-folder]/’ TBLPROPERTIES ( 'table_type‘ = 'ICEBERG‘ [, property_name=property_value]); https://docs.aws.amazon.com/ja_jp/athena/latest/ug/querying-iceberg-creating- tables.html 23 Amazon AthenaでIcebergテーブルを定義する⽅法
  24. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. INSERT INTO tablename VALUES (v1, v2,...)… DELETE FROM tablename [WHERE predicate] UPDATE tablename SET xx=yy[,...] [WHERE predicate] MERGE INTO target_table USING { source_table | query } ON search_condition when_clause [...] 24 使い慣れた SQL の延⻑で Iceberg テーブルを操作
  25. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Source Data AWS Glue Data Catalog Iceberg base Data Lake Amazon Athena Data Ingestion Data Analytics Iceberg/Spark Amazon EMR AWS Glue ETL Amazon Managed Service for Apache Flink Iceberg/Flink Streaming Batch Amazon Managed Streaming for Apache Kafka 全体アーキテクチャ例 25
  26. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 27
  27. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 運⽤上の検討ポイント • OPTIMIZE : table_name REWRITE DATA は、データファイルをデータサイズと関 連する削除ファイルの数に基づいてコンパクションして最適化する • WHERE 句でコンパクションの影響を受けるファイルを制御可能 OPTIMIZE table_name REWRITE DATA USING BIN_PACK [WHERE predicate]; • VACUUM : 有効期限切れのスナップショットと、孤立ファイル(メタデータに紐 づかないファイル)を削除することでデータサイズが小さくなる (実データが削除されるので注意) VACUUM table_name; 28 • Iceberg はテーブルの変更履歴を保持するため、差分データが蓄積される → 徐々にクエリが遅くなる → テーブル/メタデータが肥⼤化する
  28. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. Athena で指定可能なテーブルプロパティ CREATE TABLE / ALTER TABLE SET PROPERTIES DDL で指定する プロパティ 説明 デフォルト値 format ファイルデータ形式 エンジンバージョン2は parque のみ、バージョン3はAvro, ORC, parqueに対応 parquet write_compression ファイル圧縮コーデック ※サポートされるファイル形式と圧縮の組み合わせは、Athena エンジンのバー ジョンによって異なる バージョン2:GZIP バージョ3:ZSTD optimize_rewrite_d ata_file_threshold テーブルを構成するデータファイル数の閾値で、この数を超えると統合される ファイルサイズを目標サイズに近づけることで、計算コストを削減できる 5 optimize_rewrite_d elete_file_threshold データファイルごとに蓄積する削除ファイルの数 削除ファイル数を適切に設定することで、計算コストを削減できる 2 vacuum_min_snaps hots_to_keep テーブルのメインブランチに保持するスナップショットの最小数 (vacuum_max_snapshot_age_seconds プロパティよりも優先される) 1 vacuum_max_snaps hot_age_seconds メインブランチに保持するスナップショットの最大保存期間 432,000 秒 (5 日間) https://docs.aws.amazon.com/ja_jp/athena/latest/ug/querying-iceberg-creating-tables.html
  29. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 30 Athena + Open Table Format:検討事項 • 適したユースケース • データセットが大きく、部分的な更新・削除が発生 (小さい場合は全体書き換えでも問題ない) • 小さいデータの継続的な更新 • タイムトラベルクエリ等の単体のファイルでは実現困難な機能が必要 • 注意点 • RDBMSのような更新速度が出るものではありません • OTFによる追加のオーバーヘッド • 更新が繰り返された後のパフォーマンス調整、メンテナンス運用 • 利用可能なサービスが限定される(サポートされるOTFバージョンも含めて要検討)
  30. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 31 • Athenaはアドホックな分析を高速に実行するサーバーレスのサービスで、 Presto/TrinoベースのSQLをサポート • S3上のデータだけでなく、 Federated Query経由で多様なデータを一元的にクエリする環境を提供 • データレイクへの高度な要求に対応するため、Apache Iceberg をはじめとす る各種 OTF をサポートしている まとめ
  31. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 32 • ACID transactions with Athena & Iceberg Workshop • Accelerate data science feature engineering on transactional data lakes using Amazon Athena with Apache Iceberg • Amazon Athena、Amazon EMR、および AWS Glue を使用した Apache Iceberg データレイクの構築 • Perform upserts in a data lake using Amazon Athena and Apache Iceberg • Build a transactional data lake using Apache Iceberg, AWS Glue, and cross-account data shares using AWS Lake Formation and Amazon Athena • Use Apache Iceberg in a data lake to support incremental data processing • Build a real-time GDPR-aligned Apache Iceberg data lake • Automate replication of relational sources into a transactional data lake with Apache Iceberg and AWS Glue • Interact with Apache Iceberg tables using Amazon Athena and cross account fine-grained permissions using AWS Lake Formation • Build a serverless transactional data lake with Apache Iceberg, Amazon EMR Serverless, and Amazon Athena 参考リソース
  32. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Thank you!