掃描 hive 分割區的資料集
團隊也把清理後的 Parquet 借出資料,以 hive 分割區的結構儲存,每個年份一個目錄(checkoutyear=2023/、checkoutyear=2024/)。請掃描這個分割區資料集,並在分割區欄位上篩選,讓 Polars 只讀取你實際需要的年份。
已將 polars 載入為 pl,根目錄在 HIVE_DIR。分割區目錄已為你列出,讓你可以看到其結構。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 使用正確的參數啟用 hive 分割區,掃描
HIVE_DIR。 - 將結果篩選為 2024 年(含)之後的借出紀錄。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
requests = pl.scan_parquet(
HIVE_DIR,
# Enable hive partitioning
____=True,
)
result = (
requests
# Filter to the 2024 partition
.filter(pl.col("checkoutyear") >= ____)
.group_by("format")
.agg(pl.col("checkouts").sum().alias("total"))
.sort("total", descending=True)
.collect()
)
print(result)