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 ACID トランザクション + Iceberg

0
Posted at

AWS エバンジェリストシリーズ AWSの基礎を学ぼう

https://awsbasics.connpass.com/event/240345/

プレビュー提供発表記事

Apache Iceberg による Amazon Athena ACID トランザクション (プレビュー) を発表 投稿日: Nov 29, 2021

一般公開発表記事

Apache Iceberg を利用した Amazon Athena ACID トランザクションの一般提供の開始を発表 投稿日: Apr 5, 2022

この新機能は、Athena の SQL のデータ操作言語 (DML) に、挿入、更新、削除、タイムトラベルのオペレーションを追加します。

Amazon Athena

{{< figure src="[https://drive.google.com/uc?export=view&id=1jVVDjfFtxP1_QmaevXju9ZK3kii7743x](https://drive.google.com/uc?export=view&id=1jVVDjfFtxP1_QmaevXju9ZK3kii7743x)" title="Amazon Athena" link=https://aws.amazon.com/jp/athena/ >}}

Time Travel and Version Travel Queries

タイムトラベル:特定データの○時間前、○日前などを参照する際に利用

詳しくはこちら▶ Time Travel and Version Travel Queries

Apache Iceberg

{{< figure src="[https://drive.google.com/uc?export=view&id=1jWK9fTplxjlBkV8FpKM1pUwXvAowVoxV](https://drive.google.com/uc?export=view&id=1jWK9fTplxjlBkV8FpKM1pUwXvAowVoxV)" title="Apache Iceberg" link=https://iceberg.apache.org/ >}}

参考

CPU使用率90%を超える高負荷がLNEのHive Metastoreで発生 Hive table formatの課題はApache Icebergで解消

Handson Part

教材

実際にやってみた

  • Update

image

  • DELETE

image

  • S3のファイル

image

avroは、データがバイナリエンコードされるデータフォーマット

{{< figure src="[https://avro.apache.org/images/avro-logo.png](https://avro.apache.org/images/avro-logo.png)" title="Apache Avro" link=https://avro.apache.org/ >}}

トラブル?

  • Amazon Athenaで、データベース「Default」が表示されない
    • 初期起動時の裏で非同期で実行されるようで初回アクセス直後は表示されない場合はしばらく待ってください
  • identifiers must not start with a digit; surround the identifier with double quotes
    • A column name in an SQL statement can contain only letters, digits, and underscores (_). テーブル名が数字、_で含まれている場合はダブルクォート"で囲んでください

以降は個人で実施した内容です。

Amazon Athena Workshop

事前準備

CloudFormation Template
にある Launch Stack からリソース作成します。

{{< message_box color="danger" >}}
N.Virginia(バージニア北部)リージョンでのみ実施が推奨されています
{{< /message_box >}}

  • [次へ]をおして進めていくだけです

image

  • 以下の表示であれば作成完了です

image

  • 保存したクエリから名前をクリックして実行します
    • Athena_create_amazon_reviews_parquet

image

  • 表示されるqueryをそのまま全体を実行するとエラーとなります

image

  • セミコロン;で区切られているセクションごとにハイライト(選択)して実行してください

image

  • ここまで実行したらクエリエディタで新しくテーブルを作成します
CREATE TABLE amazon_reviews_iceberg(
marketplace string,
customer_id string,
review_id string,
product_id string,
product_parent string,
product_title string,
star_rating int,
helpful_votes int,
total_votes int,
vine string,
verified_purchase string,
review_headline string,
review_body string,
review_date bigint,
year int,
product_category string)
PARTITIONED BY (product_category)
LOCATION 's3://athena-workshop-${account-id}/amazon_reviews_iceberg/'
TBLPROPERTIES (
'table_type'='ICEBERG',
'format'='parquet',
'compaction_bin_pack_target_file_size_bytes'='536870912'
)

image

{{< message_box color="warning" >}}
athena-workshop-${account-id}は、CloudFormationで作成されるS3バケットを利用しました
{{< /message_box >}}

{{< message_box color="danger" >}}
compaction_bin_pack_target_file_size_bytes のままではエラーとなるため、write_target_data_file_size_bytes へ変更する必要がありました
{{< /message_box >}}

  • 作成済みのamazon_reviews_parquetからデータを移送します
insert into amazon_reviews_iceberg select * from "default"."amazon_reviews_parquet"

{{< message_box color="warning" >}}
データベース default 以外で進行している場合は変更が必要です
{{< /message_box >}}

タイムトラベルクエリ

  • Snapshotの履歴(History)を確認します
SELECT * FROM "$DATABASE_NAME"."TABLE_NAME"$iceberg_history

image

  • FOR SYSTEM_TIME AS OF timestamp の形式で抽出

image

  • FOR SYSTEM_VERSION AS OF ${snapshot-id} の形式で抽出

image

  • 他にも時間を過去に遡る検索も可能です
SELECT * FROM "TABLE_NAME"
FOR SYSTEM_TIME AS OF (current_timestamp - interval '1' day)

カラム追加

image

最適化

詳しくはこちら▶Optimizing Iceberg Tables

データが蓄積されていくなかで不要データを削除した場合に、一度肥大化したデータをshrinkしてスキャン効率をよくするものと認識しました。

  • 最適化前の検索
    • 1分以上掛かっています

image

  • 最適化実行

image

  • 最適化直後の検索
    • 1分以上掛かっています

image

  • 最適後の検索(体感で5分程度間隔を空けました)
    • 10秒と少しで検索します

image

まとめ

Athenaはあまり触っていなかったのですが、S3にあるファイルをSQLでここまで制御できるとは驚きました。
Parquet形式は、なにかと使っていたのですが(AWS CURとか...)、分析に使いやすくなるなと個人的に思いました。

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?