【Day 19】 实作 - 透过 AWS 服务 Glue Job 调整 Partition 以及档案格式

昨天我们已经透过 AWS Glue Crawler 自动建立 VPC Log 资料表,并且我们也看到 AWS Glue Crawler 业已依据资料夹切立 Partition,那可能此 Partition 不符合你的架构规划,故我们可以透过 AWS Glue Job 来调整 Partition 分区结构以及将此格式转换成 Parquet 以加快查询速度。
https://ithelp.ithome.com.tw/upload/images/20211005/20131073HRlwyPWEEC.png

那我们就开始吧 GOGO


步骤一、於 S3 Bucket 建立一个资料夹

首先我们要建立一个资料夹来存放转置成 Parquet 的 VPC Log,故我们於先前的 S3 Bucket Create folder (命名为:vpclog-converted-log )
https://ithelp.ithome.com.tw/upload/images/20211003/20131073zTtaXr3DQn.png


步骤二、搜寻 AWS Glue,并点选 Job 後新增

https://ithelp.ithome.com.tw/upload/images/20211003/20131073kFooaxwiiF.jpg


步骤三、输入Job相关参数

  • Name:自行输入名称即可
  • IAM Role:选择先前建立的 Glue role 即可
    注:务必要确认此 role 是否有权限可以存取到 vpclog-converted-log 这个资料夹喔,如果没有要记得至 IAM 新增权限或修改 policy
  • Type、Glue version 选择 Spark
  • This job runs:选择 A proposed script generated by AWS Glue
    让 AWS Glue 依据我们设定自动产生程序码,并存放在下方路径
  • Job bookmark:选择 Enable,当启用後每次执行此 Job 会记住哪些先前已经处理过、哪些还没,仅会处理尚未处理过的资料
    https://ithelp.ithome.com.tw/upload/images/20211003/201310737HD50M4L3x.jpg

步骤四、选择资料来源

这边我们选择昨天创建的 vpc log 资料表,按 Next
https://ithelp.ithome.com.tw/upload/images/20211003/20131073zW4r7CI7AV.jpg


步骤五、因为我们要进行 Parquet 以及 Schema 转换,这边我们选择 Change schema 按 Next

https://ithelp.ithome.com.tw/upload/images/20211003/20131073IApk5TRz9c.jpg


步骤六、这边是要设定处理完後的资料表要另外创一个资料表还是更新原资料表,我们选 Create tables in your data target 按 Next

https://ithelp.ithome.com.tw/upload/images/20211003/201310732vddCi26lT.jpg


步骤七、设定资料表结构

左侧为原始资料结构,右侧是修改完的资料结构
因为我们要自行设定 Partition,故我们将 Target 的 Partition_x 都移除後按 Next
https://ithelp.ithome.com.tw/upload/images/20211003/20131073ovqOU06RO5.png


步骤八、接着 AWS Glue 就会自动产生对应的程序码,我们只要在对程序码进行微调即可

https://ithelp.ithome.com.tw/upload/images/20211003/20131073YBiCi6AyE8.png

首先我们需要在最前面汇入以下套件

from awsglue.dynamicframe import DynamicFrame
from pyspark.sql.functions import *

接着我们打算抓原始资料的 start 栏位来切出『年/月/日』的 Partition,故我们在这行程序码:
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
增加以下程序码:

dfdst = dropnullfields3.toDF()
dfdst = dfdst.withColumn("year",year(to_date(from_unixtime(dfdst.start))))
dfdst = dfdst.withColumn("month",month(to_date(from_unixtime(dfdst.start))))
dfdst = dfdst.withColumn("day",dayofmonth(to_date(from_unixtime(dfdst.start))))
resultFrame = DynamicFrame.fromDF(dfdst,glueContext,"result")

我们将此行程序码
datasink4 = …..
替换成以下程序码

datasink4 = glueContext.write_dynamic_frame.from_options(frame = resultFrame, connection_type = "s3", connection_options = {"path": "s3://<<桶子名称>> /<<存放VPC Log Parquet资料夹名称>> /","partitionKeys":["year","month","day","action"]}, format = "parquet", transformation_ctx = "datasink4")

调整如下所示:
https://ithelp.ithome.com.tw/upload/images/20211003/20131073qArW4OQHGt.jpghttps://ithelp.ithome.com.tw/upload/images/20211003/20131073jS7GsnAAen.jpg


接着执行 Run Job ~ 大概等一分钟就执行成功了
https://ithelp.ithome.com.tw/upload/images/20211003/201310730YZJrPkmg6.png

注意:
如果执行 AWS Glue Job 发生错误,可以看它的 Error 讯息是否为类似下方讯息
An error occurred while calling o138.pyWriteDynamicFrame. Failed to delete key: vpclog-converted-log/_temporary
那应该是 IAM role 没有授予适当权限,需要进行调整,下方为参考的 Policy ~
https://ithelp.ithome.com.tw/upload/images/20211003/20131073rXx0j70nt0.png


步骤九、接着创建新的 Glue crawler,增加『s3://dorothy-ithome/vpclog-converted-log/』为 data store,并指定先前 glue role,其余皆保持预设即可

https://ithelp.ithome.com.tw/upload/images/20211003/20131073Zxnnf0xUi5.png
https://ithelp.ithome.com.tw/upload/images/20211003/201310738yRXKPRVij.png


步骤十、执行此 crawler

https://ithelp.ithome.com.tw/upload/images/20211003/20131073z8SIdIVNUd.png


成功完成後,我们就可以看到此资料表的相关资讯,资料源格式为 Parquet,也可以看到我们刚刚手动新增的 Partition – year、month、day、action ~
https://ithelp.ithome.com.tw/upload/images/20211003/20131073mVGBzxRGBJ.png


颇长的一篇文章哈哈 XD 今天我们成功地透过 AWS Glue Job 将 gz 档案转换成 Parquet,且调整 Partition 分层结构,那明天就进入了 Data Analytics & Visualization 阶段啦

明天见罗 : D ~

如果有任何指点与建议,也欢迎留言交流,一起漫步在 Data on AWS 中。


<<:  Day18-TypeScript(TS)的类别(Class)继承(Inheritance)

>>:  Day 19 -HAVING 子句!

铁人赛失败了....

可能是我半夜发文的关系 所以超过十二点了 铁人赛给我失败了 不过也没关系拉我继续记录 今天继续看fl...

【Day22】立即函式(IIFE)

立即函式特点: 立即执行 无法在函式外再次执行 接着我们来看立即函式的语法 (function() ...

[区块链&DAPP介绍 Day21] contract 案例3 - 比大小下注游戏

今天来聊聊我们的第三个案例。我们来做一个比大小的下注游戏。 情境 需要有两个角色。 GM 玩家 功能...

【苹果用户必看】5套超级好用的iTunes替代数据传输软件

想要将 iPhone 数据备份到计算机或传输到另一部 iPhone? 寻找一个好的 iPhone 备...

流程与制度 - 打造一个「人」的系统

谈过故事、人、与文化,我们要到最後的一个元素 — 流程与制度。最後来谈流程与制度,并不是因为他们不...