LoginSignup
3

More than 3 years have passed since last update.

たくさんのAWS Glue Jobを実行している環境で実行履歴を一覧表示するPythonスクリプト

Last updated at Posted at 2020-08-17

Glue Jobの実行履歴をコマンドラインのワンライナーで見る方法を以前にブログに書きました。

が、日時の表示がイケてなくて、使いづらかったので、Pythonのスクリプトで書き直しました。

import sys
import boto3

profile = "default"
# コマンドラインパラメータがあれば、default以外のprofileに設定
if len(sys.argv) > 1:
    profile = sys.argv[1]

session = boto3.session.Session(profile_name = profile)

client = session.client("glue")

jobs = client.get_jobs()

header = [
    "started",
    "completed",
    "executionTime",
    "status",
    "name",
    "allocatedCapacity",
    "maxCapacity",
    "glueVersion",
    "errorMessage",
]

result = []

for job in jobs["Jobs"]:
    name = job["Name"]
    history = client.get_job_runs(JobName = name)
    for run in history["JobRuns"]:
        started = run["StartedOn"].strftime("%Y-%m-%d %H:%M:%S")
        if "CompletedOn" in run:
            completed = run["CompletedOn"].strftime("%Y-%m-%d %H:%M:%S")
        else:
            completed = ""
        executionTime = str(run["ExecutionTime"])
        if executionTime == "0":
            executionTime = ""
        status = run["JobRunState"]
        if "ErrorMessage" in run:
            errorMessage = run["ErrorMessage"]
        else:
            errorMessage = ""
        allocatedCapacity = str(run["AllocatedCapacity"])
        maxCapacity = str(run["MaxCapacity"])
        glueVersion = str(run["GlueVersion"])
        result.append([
            started,
            completed,
            executionTime,
            status,
            name,
            allocatedCapacity,
            maxCapacity,
            glueVersion,
            errorMessage,
        ])

# 起動時間でソート
result.sort(key = lambda r: r[0])

# タブ区切りで出力
print("\t".join(header))
for r in result:
    print("\t".join(r))

起動時刻と終了時刻を表示しますが、環境変数 TZ に依存したローカルタイムで表示されるようです。

2021/02/27追記
GitHubにアップしました。
https://github.com/suzuki-navi/aws-glue-job-history

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
3