-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSGlue_PySpark_csv2parquet.py
75 lines (65 loc) · 2.71 KB
/
AWSGlue_PySpark_csv2parquet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
#added by Roshna
from awsglue.dynamicframe import DynamicFrame
args = getResolvedOptions(sys.argv, ["JOB_NAME"])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args["JOB_NAME"], args)
#added by Roshna
predicate_pushdown = "region in ('ca','gb','us')"
# Script generated for node S3 bucket
S3bucket_node1 = glueContext.create_dynamic_frame.from_catalog(
database="roshna_youtube_raw",
table_name="raw_statistics_data",
transformation_ctx="S3bucket_node1",
push_down_predicate = predicate_pushdown
)
# Script generated for node ApplyMapping
ApplyMapping_node2 = ApplyMapping.apply(
frame=S3bucket_node1,
mappings=[
("video_id", "string", "video_id", "string"),
("trending_date", "string", "trending_date", "string"),
("title", "string", "title", "string"),
("channel_title", "string", "channel_title", "string"),
("category_id", "long", "category_id", "bigint"),
("publish_time", "string", "publish_time", "string"),
("tags", "string", "tags", "string"),
("views", "long", "views", "bigint"),
("likes", "long", "likes", "bigint"),
("dislikes", "long", "dislikes", "bigint"),
("comment_count", "long", "comment_count", "bigint"),
("thumbnail_link", "string", "thumbnail_link", "string"),
("comments_disabled", "boolean", "comments_disabled", "boolean"),
("ratings_disabled", "boolean", "ratings_disabled", "boolean"),
("video_error_or_removed", "boolean", "video_error_or_removed", "boolean"),
("description", "string", "description", "string"),
("region", "string", "region", "string"),
],
transformation_ctx="ApplyMapping_node2",
)
#added by Roshna
resolvechoice2 = ResolveChoice.apply(frame = ApplyMapping_node2, choice = "make_struct", transformation_ctx = "resolvechoice2")
#added by Roshna
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
datasink1 = dropnullfields3.toDF().coalesce(1)
df_final_output = DynamicFrame.fromDF(datasink1, glueContext, "df_final_output")
# Script generated for node S3 bucket
S3bucket_node3 = glueContext.write_dynamic_frame.from_options(
frame=df_final_output,
connection_type="s3",
format="glueparquet",
connection_options={
"path": "s3://roshna-youtube-cleansed-version-ohio-dev/youtube/raw-statistics-data/",
"partitionKeys": ["region"],
},
transformation_ctx="S3bucket_node3",
)
job.commit()