การใช้ Parquet sink แบบ Multiplex
ทีมต้องการดึงข้อมูลจากคิวรีที่ผ่านการทำความสะอาดแล้วออกมาสองชุด ได้แก่ ชุดหนึ่งสำหรับการยืมแบบดิจิทัล และอีกชุดสำหรับแบบกายภาพ สร้างการเขียนทั้งสองแบบ lazy เพื่อให้ Polars วางแผนการสแกนร่วมกันเพียงครั้งเดียว แล้วรันพร้อมกันในรอบเดียว
clean_checkouts ถูกโหลดไว้ล่วงหน้าแล้ว พร้อมกับ DIGITAL_EXPORT_PATH และ PHYSICAL_EXPORT_PATH
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การปรับขนาดและเพิ่มประสิทธิภาพ Data Pipeline ด้วย Polars
คำแนะนำการฝึกหัด
- สร้างทั้งสอง sink แบบ lazy เพื่อไม่ให้รันทันที
- รันทั้งสอง sink พร้อมกันบน streaming engine
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Lazy sink for digital
digital_sink = (
clean_checkouts
.filter(pl.col("use") == "Digital")
.sink_parquet(DIGITAL_EXPORT_PATH, lazy=____)
)
# Lazy sink for physical
physical_sink = (
clean_checkouts
.filter(pl.col("use") == "Physical")
.sink_parquet(PHYSICAL_EXPORT_PATH, lazy=____)
)
# Run both sinks together
pl.____([digital_sink, physical_sink], engine="streaming")
# Check the row counts of each extract
result = pl.DataFrame(
{
"extract": ["digital", "physical"],
"rows": [
pl.scan_parquet(DIGITAL_EXPORT_PATH).select(pl.len()).collect().item(),
pl.scan_parquet(PHYSICAL_EXPORT_PATH).select(pl.len()).collect().item(),
],
}
)
print(result)