この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
本日からPython 3.6と互換性のある Python Shell ジョブを使用してスクリプトを実行できるようになりました。今後はPython 2.7互換とPython 3.6互換のいずれかを選択可能になります。早速確認してみたいと思います。
Python Shell ジョブ の作成
Configure the job properties 画面の [Type] でPython shell
を選択すると、[Python version]でPython 3
とPython 2
を選べるように変わりました。今後のデフォルトは、Python 3
になります。
※ しかし、執筆時点(2019/06/07)のawscliからPython Shell ジョブを登録すると、Python 2
がデフォルトのようです。helpを参照してもPython versionの指定がありませんでしたので、今後のアップデートで変更されるのかもしれません。
$ aws --version
aws-cli/1.16.173 Python/3.6.3 Darwin/18.6.0 botocore/1.12.163
$ aws glue create-job --name python-shell-job --role AWSGlueServiceRoleDefault --command '{"Name" : "pythonshell", "ScriptLocation" : "s3://aws-glue-scripts-123456789012-ap-northeast-1/admin/python-shell-job"}'
{
"Name": "python-shell-job"
}
Python Shell ジョブ の実行
Python 2では、print res
ですが、Python 3 なのでprint(res)
と書きます。
import boto3
import pg
def get_connection():
rs_conn_string = "host=%s port=%s dbname=%s user=%s password=%s" % (
"cm-cluster.abcdefghijkl.us-east-1.redshift.amazonaws.com",
"5439",
"cmdb",
"cm_user",
"Cm20190607")
rs_conn = pg.connect(dbname=rs_conn_string)
rs_conn.query("set statement_timeout = 1200000")
return rs_conn
def query(con):
statement = "Select * from lineorder limit 1;"
res = con.query(statement)
return res
# Run Query
con = get_connection()
res = query(con)
print(res)
上記のPythonスクリプトを実行しました。
当たり前ですが、print(res)
をprint res
と書くとちゃんと(?)エラーになります。スタックトレースの中でpython3.6が確認できます。
まとめ
Python 2系を敬遠していた方には朗報だと思いブログにて紹介しました。今後はdeprecatedを控えているPython 2の利用は避けて、この機会にPython 3に移行するのが良いでしょう。願わくば、ジョブタイプ Spark
もPython 3対応することをお祈りしています。