AWS glue 開発エンドポイントを使用してのPyspark検証

glueジョブ作成で生成されたスクリプトをもとに、glueの開発エンドポイントを使用して検証していく。

目的:姓(surname)と名(firstname)に分かれているカラムを結合して表示する。

モジュールpyspark.sqlのインポート

from pyspark.sql.functions import *

Pysparkを使用するため、DynamicFrameからDataFrameに変換

df = dropnullfields3.toDF()

Pyspark、concatを使用してカラムを結合して表示

colum_join = df.select(
    concat(
        df.surname, df.firstname
            ).alias('name')
        )

colum_join.show()

Jupyterにて結合されたカラムが表示された。

すべてのソース

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

#モジュールpyspark.sqlのインポート
from pyspark.sql.functions import *

## @params: [JOB_NAME]
#コメント
#args = getResolvedOptions(sys.argv, ['JOB_NAME'])

#コメント
#sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
#コメント
#job.init(args['JOB_NAME'], args)

## @type: DataSource
## @args: [database = "from_csv_to_datacatalog", table_name = "from_csv_to_datacatalog_upload_csv", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "from_csv_to_datacatalog", table_name = "from_csv_to_datacatalog_upload_csv", transformation_ctx = "datasource0")

## @type: ApplyMapping
## @args: [mapping = [("id", "long", "id", "long"), ("firstname", "string", "firstname", "string"), ("surname", "string", "surname", "string"), ("firstname_kana", "string", "firstname_kana", "string"), ("surname_kana", "string", "surname_kana", "string"), ("zipcode", "string", "zipcode", "string"), ("prefectures", "string", "prefectures", "string"), ("tel", "string", "tel", "string"), ("email", "string", "email", "string"), ("birthday", "string", "birthday", "date")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(
    frame = datasource0, mappings = [
        ("id", "long", "id", "long"), 
        ("firstname", "string", "firstname", "string"), 
        ("surname", "string", "surname", "string"), 
        ("firstname_kana", "string", "firstname_kana", "string"), 
        ("surname_kana", "string", "surname_kana", "string"), 
        ("zipcode", "string", "zipcode", "string"), 
        ("prefectures", "string", "prefectures", "string"), 
        ("tel", "string", "tel", "string"), 
        ("email", "string", "email", "string"), 
        ("birthday", "string", "birthday", "string")
        ], 
        transformation_ctx = "applymapping1"
    )

## @type: ResolveChoice
## @args: [choice = "make_struct", transformation_ctx = "resolvechoice2"]
## @return: resolvechoice2
## @inputs: [frame = applymapping1]
resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_struct", transformation_ctx = "resolvechoice2")

## @type: DropNullFields
## @args: [transformation_ctx = "dropnullfields3"]
## @return: dropnullfields3
## @inputs: [frame = resolvechoice2]
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")

## @type: colum_join
## @args: [transformation_ctx = "dropnullfields3"]
## @return: colum_join
## @inputs: [frame = dropnullfields3]

#Pysparkを使用するため、DynamicFrameからDataFrameに変換
df = dropnullfields3.toDF()

#Pyspark、concatを使用してカラムを結合して表示

colum_join = df.select(
    concat(
        df.surname, df.firstname
            ).alias('name')
        )

colum_join.show()

## @type: DataSink
## @args: [connection_type = "s3", connection_options = {"path": "s3://datalake-test-datacatalog-s3/parquet"}, format = "parquet", transformation_ctx = "datasink4"]
## @return: datasink4
## @inputs: [frame = dropnullfields3]

#コメント
#datasink4 = glueContext.write_dynamic_frame.from_options(frame = dropnullfields3, connection_type = "s3", connection_options = {"path": "s3://datalake-test-datacatalog-s3/parquet"}, format = "parquet", transformation_ctx = "datasink4")

#コメント
#job.commit()

参考ページ

Pyspark dataframe操作

https://qiita.com/wwacky/items/e687c0ef05ae7f1de980