【AWS】AWS Glue ~ Boto3 / 基本編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2019/10/14/000000

の続き。長くなったので、分冊。

 今回は、boto3 API を使って、
PythonでGlueのコンポーネント(Workflow/Job/Trigger ※)を
デプロイする簡単なサンプルの実装および
環境周りなどの注意事項をまとめておく。

※ Crawler (クローラ)については、以下の関連記事を参照のこと

AWS Glue ~ Boto3 / クローラ編 ~
https://dk521123.hatenablog.com/entry/2021/04/16/135558

目次

【1】使用上の注意
 1)IAM権限について
 2)Workflow の WorkflowGraph 作成について
 3)「Spark SQL 用に データカタログ を有効」にする際の注意点
 4)その他 / トラブル

【2】サンプル
 例1:ジョブ作成 (create_job)
 例2:トリガー作成 (create_trigger) with YAML
 例3:ワークフロー存在チェック (get_workflow) with 例外処理

【1】使用上の注意

1)IAM権限について

以下のIAMロールが必要になる

* iam:PassRole
* AWSGlueServiceRole
* S3系の権限
etc

2)Workflow の WorkflowGraph 作成について

* Workflow の WorkflowGraph は、CreateTrigger() のタイミングでのみ作成される。
  => UpdateTrigger()では作成されない
  => トリガーを再作成する必要がある
  => DeleteTrigger() を実行
    -> GetTrigger で EntityNotFoundException になるまで待つ 
         (Response["Trigger"]["State"] で"DELETING"になっている)
    -> CreateTrigger() を再実行

WorkflowGraph
https://docs.aws.amazon.com/ja_jp/glue/latest/dg/aws-glue-api-workflow.html#aws-glue-api-workflow-WorkflowGraph
CreateTrigger / UpdateTrigger
https://docs.aws.amazon.com/glue/latest/webapi/API_CreateTrigger.html
https://docs.aws.amazon.com/glue/latest/webapi/API_UpdateTrigger.html
DeleteTrigger / GetTrigger
https://docs.aws.amazon.com/glue/latest/webapi/API_DeleteTrigger.html
https://docs.aws.amazon.com/glue/latest/webapi/API_GetTrigger.html

3)「Spark SQL 用に データカタログ を有効」にする際の注意点

「--enable-glue-datacatalog: ""」を追加する

※ YAMLやJSONなどで指定した場合、「""」も必要
 (これで、若干はまった)

https://docs.aws.amazon.com/ja_jp/glue/latest/dg/aws-glue-programming-etl-glue-data-catalog-hive.html
https://qiita.com/pioho07/items/34716b2b63761100096a

4)その他 / トラブル

詳細について、以下の関連記事を参照のこと。
 => 書き溜めていたら、めちゃくちゃ多くなってしまった。。。

boto3 AWS Glue API のトラブル ~ trigger全般 編 ~
https://dk521123.hatenablog.com/entry/2020/10/23/110821
boto3 AWS Glue API のトラブル ~ scheduled trigger編 ~
https://dk521123.hatenablog.com/entry/2020/01/16/205331
boto3 AWS Glue API のトラブル ~ job/crawler編 ~
https://dk521123.hatenablog.com/entry/2020/02/05/223307

【2】サンプル

例1:ジョブ作成 (create_job)

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/glue.html#Glue.Client.create_job

import boto3

glue = boto3.client('glue')

# ジョブ作成
job = glue.create_job(
  Name='sample-job',
  Role='Glue_IAMRole',
  Command={
      'Name': 'glueetl',
       # ★S3上にあるPythonソースを指定★
      'ScriptLocation': 's3://xxxx/yyyy/etl_sample.py'
  }
)

例2:トリガー作成 (create_trigger) with YAML

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/glue.html#Glue.Client.create_trigger

import boto3
import yaml

s3_client = boto3.client('s3')
glue_client = boto3.client('glue')

response = s3_client.get_object(
  Bucket='bucket-name',
  # Key="xxx/yyyy/zzzzz/trigger_on_demand.yaml")
  Key="xxx/yyyy/zzzzz/trigger_scheduled.yaml")
config = yaml.safe_load(response["Body"])

response_of_create_trigger = glue_client.create_trigger(**config)
print(response_of_create_trigger)

trigger_on_demand.yaml

Name: 'sample-trigger'
WorkflowName: 'sample-workflow'
Type: 'ON_DEMAND'
Actions:
   - JobName: 'sample-job'
     Arguments:
         --key1: 'value1'
Description: 'This is a sample trigger'

trigger_scheduled.yaml

Name: 'sample-trigger'
WorkflowName: 'sample-workflow'
Type: 'SCHEDULED'
# 指定の仕方は、以下のURLを参照。
# https://docs.aws.amazon.com/ja_jp/glue/latest/dg/monitor-data-warehouse-schedule.html
# UTC時間で指定 https://www.jisakeisan.com/timezone/utc/
Schedule: 'cron(30 12 31 1 ? 2020)'
# 「StartOnCreation: True」にしないと動作しない
# 詳細は以下の関連記事の「【2】create_trigger で SCHEDULED...」を参照
# https://dk521123.hatenablog.com/entry/2020/01/16/205331
StartOnCreation: True
Actions:
   - JobName: 'sample-job'
     Arguments:
         --key1: 'value1'
Description: 'This is a sample trigger'

例3:ワークフロー存在チェック (get_workflow) with 例外処理

import boto3
try:
    from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
    pass

def main():
    glue_client = boto3.client('glue')

    has_workflow = False
    workflow_name = 'sample-workflow'
    try:
        response = glue_client.get_workflow(
            Name=workflow_name,
            IncludeGraph=True)
        print(response)
        has_workflow = True
    except(BotoCoreError, ClientError) as ex:
        if ex.response['Error']['Code'] == 'EntityNotFoundException':
            has_workflow = False
        else:
            raise ex

    print('Is there the workflow {}? => {}'.format(
        workflow_name, has_workflow))

if __name__ == '__main__':
    main()

関連記事

AWS Glue ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/01/221926
AWS Glue ~ Boto3 / 入門編 ~
https://dk521123.hatenablog.com/entry/2019/10/14/000000
AWS Glue ~ Boto3 / クローラ編 ~
https://dk521123.hatenablog.com/entry/2021/04/16/135558
AWS Glue ~ Boto3 / セキュリティ設定編 ~
https://dk521123.hatenablog.com/entry/2020/04/08/171208
AWS Glue ~ Boto3 / Glue connection編 ~
https://dk521123.hatenablog.com/entry/2020/01/29/224525
AWS Glue ~ Boto3 / パーティション操作編 ~
https://dk521123.hatenablog.com/entry/2021/06/09/113458
AWS Glue ~ Boto3 / DB・テーブル操作編 ~
https://dk521123.hatenablog.com/entry/2021/06/11/164015
boto3 AWS Glue API のトラブル ~ trigger全般 編 ~
https://dk521123.hatenablog.com/entry/2020/10/23/110821
boto3 AWS Glue API のトラブル ~ scheduled trigger編 ~
https://dk521123.hatenablog.com/entry/2020/01/16/205331
boto3 AWS Glue API のトラブル ~ job/crawler編 ~
https://dk521123.hatenablog.com/entry/2020/02/05/223307
AWS Glue のトラブル ~ job編 - [1] ~
https://dk521123.hatenablog.com/entry/2019/10/25/232155
AWS Glue のトラブル ~ job編 - [2] ~
https://dk521123.hatenablog.com/entry/2020/10/12/152659
AWS Glue のトラブル ~ job編 - [3] ~
https://dk521123.hatenablog.com/entry/2021/02/16/145848
AWS Glue のトラブル ~ crawler編 ~
https://dk521123.hatenablog.com/entry/2020/05/07/144132