ฝึกใช้คอลัมน์ประเภท Array
ฟังก์ชัน SQL udf พร้อมใช้งานแล้ว รวมถึง DataFrame df_before ที่มีชนิดข้อมูลเป็น DataFrame[doc: array<string>, in: array<string>, out: array<string>]
ตัวแปร TRIVIAL_TOKENS เป็น set ที่เก็บคำที่ต้องการลบออก
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Spark SQL in Python
คำแนะนำการฝึกหัด
- แสดงแถวของ
df_beforeที่คอลัมน์docมีรายการ5 - สร้าง UDF สำหรับลบรายการที่อยู่ใน
TRIVIAL_TOKENSออกจากคอลัมน์ประเภท array โดยไม่จำเป็นต้องรักษาลำดับ - ลบ token ที่ปรากฏใน
TRIVIAL_TOKENSออกจากคอลัมน์inและoutในdf2
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Show the rows where doc contains the item '5'
df_before.where(array_contains('doc', '____')).show()
# UDF removes items in TRIVIAL_TOKENS from array
rm_trivial_udf = udf(lambda x:
list(set(x) - ____) if x
else x,
ArrayType(____()))
# Remove trivial tokens from 'in' and 'out' columns of df2
df_after = df_before.withColumn('in', ____('in'))\
.withColumn('out', ____('out'))
# Show the rows of df_after where doc contains the item '5'
df_after.where(array_contains('doc','5')).show()