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

Data engineer を楽にする AWS サービス・機能/aws-services-for-data-engineers

jozono
June 02, 2021

Data engineer を楽にする AWS サービス・機能/aws-services-for-data-engineers

2021.06.01 BigData-JAWS 勉強会#17
講演資料「Data engineer を楽にする AWS サービス・機能」
https://jawsug-bigdata.connpass.com/event/206895/

jozono

June 02, 2021
Tweet

More Decks by jozono

Other Decks in Technology

Transcript

  1. © 2021, Amazon Web Services, Inc. or its Affiliates. Junpei

    Ozono Senior Solutions Architect, Analytics Amazon Web Services Japan K.K. Data engineer を楽にする AWS サービス/機能 BigData-JAWS 勉強会#17
  2. © 2021, Amazon Web Services, Inc. or its Affiliates. 2

    今⽇のお話 Data engineer を楽にする AWS サービス/機能について • ワークフロー on AWS • ML with SQL の 2 つのテーマでご紹介します
  3. © 2021, Amazon Web Services, Inc. or its Affiliates. 4

    ワークフローとは︖ • 「処理の流れ」を図式化したもの • 「だれが何を」「どんな流れで処理するのか」が明確になり、 業務を効率的に進めることが可能になる • ⼀般的にはデータ収集・変換などの複数のジョブを制御 ü 実⾏順序制御(パイプライン) ü 実⾏状況の可視化(モニタリング) ü デバッグのためのエラーログ確認 ü 冪等性をベースとしたリトライ制御 ü etc ETL Pipeline task
  4. © 2021, Amazon Web Services, Inc. or its Affiliates. 5

    ワークフローエンジンの選択肢 AWS Glue Workflows + Blueprints Amazon Managed Workflows for Apache Airflow (MWAA) AWS Step Functions Digdag AWS Non-AWS
  5. © 2021, Amazon Web Services, Inc. or its Affiliates. 6

    AWS Glue Workflows クローラー、トリガー、ジョブをコントロールするワークフロー機能 • Glue Job(Spark/Python) やクローラーを簡単に GUI でワークフローに組み込める • ワークフロー進捗状況を視覚的に確認可能 • boto3 を利⽤した Python Shell を利⽤することで、他 AWS サービスと連携可能 【ワークフロー作成画⾯】 【処理結果確認画⾯】 クローラー、トリガー、ジョブを追加する 処理結果が確認可能 https://docs.aws.amazon.com/ja_jp/glue/latest/dg/orchestrate-using-workflows.html
  6. © 2021, Amazon Web Services, Inc. or its Affiliates. 7

    AWS Step Functions Step Functions • 複数の AWS サービスにまたがる処理を JSON/YAML でフロー(状態遷移図)を 作成し管理 • 実⾏履歴の管理、フローの可視化 • サーバーレス
  7. © 2021, Amazon Web Services, Inc. or its Affiliates. 8

    Amazon Managed Workflows for Apache Airflow (MWAA) Apache Airflow のマネージドオーケストレーションサービス クラウドでエンドツーエンドのデータパイプラインの設定と運⽤を ⼤規模かつ簡単に⾏える Airflow v1.10.12 と v2.0.2 をサポート 2020年11⽉24⽇に⼀般利⽤可能に(東京リージョンで利⽤可能)
  8. © 2021, Amazon Web Services, Inc. or its Affiliates. 10

    Apache Airflowとは AirflowはPythonで記述されており、ワークフ ロー(DAG)はPythonスクリプトを介して作成さ れます。 Airflowは、「コードとしての構成」の原則に 基づいて設計されています。 Pythonを使⽤すると、開発者はライブラリと クラスをインポートしてワークフローの作成に 役⽴てることができます。
  9. © 2021, Amazon Web Services, Inc. or its Affiliates. 11

    Apache Airflowのコンポーネント Scheduler Worker Web Server Meta Database * 同じアカウント内でもリソースは環境ごとに独立
  10. © 2021, Amazon Web Services, Inc. or its Affiliates. 13

    Amazon MWAA はどのように動作するか
  11. © 2021, Amazon Web Services, Inc. or its Affiliates. 14

    DAG DAG: DAGはDirected Acyclic Graph(有向非巡回グラフ) と呼ばれる閉路を持たない 有向グラフです(グラフ理論における閉路のない有向グラフのこと) 。有向グラフは頂点と 有向辺(方向を示す矢印付きの辺)からなり、辺は頂点同士をつなぐが、ある頂点vから 出発し、辺をたどり、頂点vに戻ってこないのが有向非巡回グラフである AirflowでDAGは、実行するタスクの集合であり、Pythonで記述された作業(タスク)、お よび作業を実行する順序(依存関係)を定義します。定義した DAGは定期的に実行する スケジュールを設定したり、Airflow UIから実行状況を確認、必要であれば再実行などが できます。 https://airflow.apache.org/docs/apache-airflow/stable/concepts.html
  12. © 2021, Amazon Web Services, Inc. or its Affiliates. 15

    DAG t1 = AWSAthenaOperator(task_id="athena_drop_output_table",query=athena_drop_output_table_query, database="default", output_location=athena_results) t2 = PythonOperator(task_id="s3_bucket_cleaning", python_callable=s3_bucket_cleaning_job) t3 = AWSAthenaOperator(task_id="athena_create_input_table",query=athena_create_input_table_query, database="default", output_location=athena_results) t4 = AWSAthenaOperator(task_id="athena_ctas_new_table",query=athena_ctas_new_table_query, database="default", output_location=athena_results) t1 >> t3 >> t4 t2 >> t3 DAGコードの中身を少し見てみます。DAGコード(末尾)にこのように書かれていると次のよ うなフローを表します。“athena_create_input_table”タスクは、前段の ”athena_drop_output_table”と”s3_bucket_cleaning”が成功した場合にのみ実行されます
  13. © 2021, Amazon Web Services, Inc. or its Affiliates. 17

    MWAA 料⾦ • 東京リージョン, 2021/6/1時点の価格 • ネットワーク(NATゲートウェイなど)やS3およびCloudWatchの保存料金 は別途かかります。また、GlueやSageMakerなどを使用する場合はそれら の料金がかかります。 サイズ スケジュー ラー vCPU ワーカー vCPU ウェブサーバー vCPU 推奨されるワークフローキャ パシティ 時間あたりの料金 (秒単位) スモール 1 1 0.5 最大 50 DAG 0.49USD ミディアム 2 2 1 最大 250 DAG 0.74USD ラージ 4 4 2 最大 1000 DAG 0.99USD サイズ 時間あたりの料金 (秒単位) スモール 0.055USD ミディアム 0.11USD ラージ 0.22USD 環境インスタンス料⾦ - Small、Medium、Largeの3種類が提供されており、各ノードのvCPUが異なります。秒課⾦。 追加ワーカーインスタンスの料⾦ メタデータベースストレージ 料金 0.10USD/GB 月 https://aws.amazon.com/jp/managed-workflows-for-apache-airflow/pricing/ Workerインスタンスの追加分で、オートスケーリン グでノードが増減するため、2ノード⽬以降の追加さ れたインスタンスがカウントされます。秒課⾦。
  14. © 2021, Amazon Web Services, Inc. or its Affiliates. 18

    Quick Start - Tutorial for MWAA https://docs.aws.amazon.com/ja_jp/mwaa/latest/userguide/quick-start.html • CloudFormation テンプレートで、VPC、S3バケット、 MWAA環境を構築 • DAGをS3へアップロード • AIrflowでDAGを実⾏ • CloudWatchでログを確認 CloudFormation Template
  15. © 2021, Amazon Web Services, Inc. or its Affiliates. 19

    ワークフローサービス⽐較 ⽐較項⽬ Amazon MWAA AWS Step Functions Glue Workflows ユースケース • Apache Airflowとの互換性 • Airflow資産を持っている場合 • 複数 AWS サービスを利⽤ • AWS Glue を中⼼とした ワークフロー(Job/Crawler) プログラミング⾔語 • Pythonで DAG を定義 • JSON/YAML で状態遷移図を 定義 • GUI でワークフローを作成 • Python で DAG を定義 (blueprint) データ処理 • オペレータを使って AWS サー ビスと連携したデータ処理フ ローを構成 • MWAA のワーカー上動作する Python コードで記述した処理 を組み合わせることも可能 • Native でサポートする AWS サービスを組み合わせてデー タ処理フローを構成 • サポートされていない AWS サービスを AWS Lambda を 介して利⽤することも可能 • Python/PySpark/Scala で 実装した Glue Job と Crawler を組合せてデータ 処理フローを構成 • boto3 を利⽤することで 他 AWS サービスと連携した ワークフローも実現可能 環境 • マネージドサービス (ワーカー 部分はワークロードに応じて ⾃動調整) • サーバーレス • サーバーレス ※ Amazon EC2上にソフトウェアを導⼊して管理する場合、”⾃由度の⾼さ”と”運⽤管理⼯数を含めたTCO の増加” のトレードオフにご注意ください
  16. © 2021, Amazon Web Services, Inc. or its Affiliates. 21

    Analytics と ML のシステムやエンジニアの責任境界 データエンジニア データサイエンティスト Analytics ML • データ基盤整備 • データ加⼯・前処理 • 権限管理 • SQL 中⼼ • アルゴリズムや モデルの実装 • コーディング中⼼
  17. © 2021, Amazon Web Services, Inc. or its Affiliates. 22

    AWS の SQL クエリエンジン Amazon Athena Amazon Redshift AWS では、さまざまなサービスで SQL を利⽤することが可能 その中で Amazon Aurora, Amazon Athena, Amazon Redshift は SQL から⼿軽に ML を利⽤する仕組みを持つ Amazon Aurora
  18. © 2021, Amazon Web Services, Inc. or its Affiliates. 23

    ML with Amazon Aurora Amazon Aurora 内から、Amazon Comprehend や Amazon SageMaker を呼び出してセンチメント分析や予測分析を⾏うことが可能 • Amazon Comprehend や Amazon SageMaker 側で⽤意した モデルのエンドポイントを Stored Function の中で呼び出す形 • Amazon Aurora 内に保存されたデータに対して SQL で推論を実⾏可能 詳細: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-ml.html 対応リージョン: Amazon Comprehend, Amazon SageMaker が使えるすべてのリージョン
  19. © 2021, Amazon Web Services, Inc. or its Affiliates. 24

    ML with Amazon Athena Athena SQL クエリで SageMaker ML モデルエンドポイントを呼び出し, 推論を 実⾏可能 異常検出やコホート分析, 販売予測などの複雑な作業が SQL クエリで関数を呼び 出す感覚で利⽤できる USING FUNCTION predict_customer_registration(age INTEGER) RETURNS DOUBLE TYPE SAGEMAKER_INVOKE_ENDPOINT WITH (sagemaker_endpoint = ’xxxxxxxxxx’) SELECT predict_customer_registration(age) AS probability_of_enrolling, customer_id FROM "sampledb"."ml_test_dataset” WHERE predict_customer_registration(age) < 0.5 ; ML 関数を定義、名前付き変数を複数指定可能 ML 関数呼び出し https://docs.aws.amazon.com/athena/latest/ug/querying-mlmodel.html 対応リージョン: バージニア、ムンバイ、アイルランド、オレゴン
  20. © 2021, Amazon Web Services, Inc. or its Affiliates. 25

    Amazon Redshift ML SQL 経由で Amazon SageMaker と連携し、機械学習モデルの作成・トレーニングが可能に N ew G A! CREATE MODEL demo_ml.customer_churn FROM (SELECT c.age, c.zip, c.monthly_spend, c.monthly_cases, c.active FROM customer_info_table c) TARGET c.active; ユースケース : 製品のリコメンデーション、 不正防⽌、顧客離反の削減など SQL で機械学習モデルの作成、トレーニング、 デプロイ 推論モデルを Amazon Redshift 上にデプロイし SQL ステートメントの⼀部としてユーザー定義 関数を呼び出すように推論を実⾏可能 モデルの前処理、作成、トレーニング、 デプロイを⾃動で実⾏ https://aws.amazon.com/es/about-aws/whats-new/2021/05/aws-announces-general-availability-of-amazon-redshift-ml/ 機械学習アルゴリズムは⾃動選択 または XGBoost を指定可能
  21. © 2021, Amazon Web Services, Inc. or its Affiliates. 26

    Amazon Redshift ML のしくみ Amazon Redshift CREATE MODEL customer_churn FROM (SELECT c.age, c.zip, c.monthly_spend, c.monthly_cases, c.active AS label FROM customer_info_table c) TARGET label FUNCTION predict_customer_churn Amazon SageMaker Autopilot が実⾏され モデルを返却 SELECT n.id, n.firstName, n.lastName, predict_customer_churn(n.age,c.zip,..) AS activity_prediction FROM new_customers n WHERE n.marital_status = ‘single’ 構築済みモデルを使⽤して推論を実⾏ (処理は Amazon Redshift 内で実⾏) Amazon Redshift シンプルな SQL コマンドでモデ ルを作成, トレーニング, デプロイ SageMaker Autopilot を使⽤した モデルの⾃動選択, 前処理, トレー ニング (アルゴリズム指定も可) トレーニングされたモデルは, Amazon Redshift 上にコンパイル され, 以後ユーザーは SQL を使⽤ して推論を⾏うことが可能
  22. © 2021, Amazon Web Services, Inc. or its Affiliates. 27

    SageMaker モデルのインポート Bring your own model (BYOM) Amazon Redshift CREATE MODEL customer_ltv( integer,integer) RETURNS float4 LOCATION “S3://<bucket” IAM_ROLE ‘…’; Amazon SageMaker SELECT n.id, n.firstName, n.lastName, customer_ltv(n.age,c.zip) AS activity_prediction FROM new_customers n WHERE n.marital_status = ‘single’ 構築済みモデルを使⽤して推論を実⾏ (処理は Amazon Redshift 内で実⾏) Amazon Redshift SageMaker 側でモデルの作成、 トレーニングを実施し、Redshift にインポート トレーニングされたモデルは, Amazon Redshift 上にコンパイル され, 以後ユーザーは SQL を使⽤ して推論を⾏うことが可能
  23. © 2021, Amazon Web Services, Inc. or its Affiliates. 28

    SageMaker モデルのリモート呼び出し Bring your own model (BYOM) Amazon Redshift CREATE MODEL customer_ltv( integer,integer) RETURNS float4 SAGEMAKER ‘…’ IAM_ROLE ‘…’; Amazon SageMaker 構築済みモデルを使⽤して推論を実⾏ (処理は Amazon SageMaker 側で実⾏) SageMaker 側でモデルの作成、 トレーニングを実施し、Redshift からエンドポイント呼び出し 推論処理は SageMaker 側で実⾏ される。SageMaker の機能を フルに活⽤することが可能 SELECT n.id, n.firstName, n.lastName, customer_ltv(n.age,c.zip) AS activity_prediction FROM new_customers n WHERE n.marital_status = ‘single’ Amazon Redshift Amazon SageMaker
  24. © 2021, Amazon Web Services, Inc. or its Affiliates. 29

    Amazon Redshift MLを使⽤すると 推論処理に対して追加費⽤なし トレーニング処理のみの料⾦お⽀払い トレーニングデータの⾏数×列数 (セル数) に応じて料⾦が発⽣ Amazon Redshift ML: コストを最適化 トレーニ ング 10% 推論 90% コスト https://aws.amazon.com/jp/redshift/pricing/ Amazon Redshift の料⾦
  25. © 2021, Amazon Web Services, Inc. or its Affiliates. 30

    Aurora, Athena, Redshift の ML 連携機能の違い DB/Analytics サービス 連携 ML サービス モデル作成 / トレーニング 推論 コスト Amazon Comprehend (Amazon Comprehend の モデルを使⽤) SQL 内で Stored Function を実⾏して Amazon Comprehend を呼び出し 追加費⽤なし (Amazon Comprehend に 対する通常料⾦のみ) Amazon SageMaker (Amazon SageMaker 側に カスタムモデルを準備) UDF を作成し、SQL 内で 実⾏して Amazon SageMaker を呼び出し 追加費⽤なし (Amazon SageMaker に対 する通常料⾦のみ) Amazon SageMaker (Amazon SageMaker 側に カスタムモデルを準備) UDF を作成し、SQL 内で 実⾏して Amazon SageMaker を呼び出し 追加費⽤なし (Amazon SageMaker に対 する通常料⾦のみ) Amazon SageMaker (Autopilot) CREATE MODEL ⽂を実⾏ し Amazon SageMaker (Autopilot) を呼び出しモ デルを作成 SQL 内で UDF を実⾏し Amazon Redshift 内で処理 を実⾏ (Amazon SageMaker のエ ンドポイントを呼び出し、 Amazon SageMaker 側で 処理することも可能) CREATE MODEL 実⾏時の トレーニング対象データの セル数(⾏数×列数)を基に Amazon S3 や Amazon SageMaker の費⽤が発⽣ Amazon Redshift Amazon Athena Amazon Aurora
  26. © 2021, Amazon Web Services, Inc. or its Affiliates. 31

    Data engineer を楽にする AWS サービス/機能: まとめ ワークフロー制御のためのサービスの選択肢は増えている • AWS Glue Workflows • AWS Step Functions • Amazon MWAA SQL でネイティブに ML 連携を⾏うサービスも増えている • ML with Aurora • ML with Athena • Redshift ML ユースケースにあったサービス/機能を選択しましょう︕
  27. © 2021, Amazon Web Services, Inc. or its Affiliates. 32

    内容についての注意点 • 本資料では2021年6月1日時点のサービス内容および価格についてご説明しています。最新の情報はAWS公 式ウェブサイト(http://aws.amazon.com)にてご確認ください。 • 資料作成には十分注意しておりますが、資料内の価格とAWS公式ウェブサイト記載の価格に相違があった 場合、AWS公式ウェブサイトの価格を優先とさせていただきます。 • 価格は税抜表記となっています。日本居住者のお客様には別途消費税をご請求させていただきます。 • AWS does not offer binding price quotes. AWS pricing is publicly available and is subject to change in accordance with the AWS Customer Agreement available at http://aws.amazon.com/agreement/. Any pricing information included in this document is provided only as an estimate of usage charges for AWS services based on certain information that you have provided. Monthly charges will be based on your actual use of AWS services, and may vary from the estimates provided.