不可變與延遲處理

使用 PySpark 清理資料

Mike Metzger

Data Engineering Consultant

變數複習

Python 變數:

  • 可變
  • 彈性高
  • 併發可能出問題
  • 容易增加複雜度
使用 PySpark 清理資料

不可變性

不可變變數:

  • 函數式程式設計的一部分
  • 定義一次
  • 不能直接修改
  • 重新指派會重建
  • 可高效共享
使用 PySpark 清理資料

不可變性範例

定義新的資料框:

voter_df = spark.read.csv('voterdata.csv')

進行變更:

voter_df = voter_df.withColumn('fullyear', 
    voter_df.year + 2000)

voter_df = voter_df.drop(voter_df.year)
使用 PySpark 清理資料

延遲處理

  • 不會很慢嗎?
  • 轉換
  • 動作
  • 便於高效規劃
voter_df = voter_df.withColumn('fullyear', 
    voter_df.year + 2000)
voter_df = voter_df.drop(voter_df.year)

voter_df.count()
使用 PySpark 清理資料

一起來練習吧!

使用 PySpark 清理資料

Preparing Video For Download...