iT邦幫忙

2021 iThome 鐵人賽

DAY 19
1
AI & Data

Data on AWS - 實作建立 Data Analytics Pipeline系列 第 19

【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 中。


上一篇
【Day 18】 實作 - 透過 AWS 服務 Glue Crawler 自動建立 VPC Log 資料表
下一篇
【Day 20】 實作 - 於 AWS Quicksight 建立 Sankey diagram 以及設定 Action
系列文
Data on AWS - 實作建立 Data Analytics Pipeline30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言