Array कॉलम का अभ्यास
SQL फंक्शन udf उपलब्ध है, साथ ही एक dataframe df_before भी उपलब्ध है, जिसका टाइप DataFrame[doc: array<string>, in: array<string>, out: array<string>] है.
TRIVIAL_TOKENS वैरिएबल एक set है। इसमें वे कुछ शब्द हैं जिन्हें हम हटाना चाहते हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Spark SQL परिचय
अभ्यास निर्देश
df_beforeकी वे rows दिखाएँ जिनमेंdocमें item5शामिल है.- ऐसा udf बनाएँ जो array कॉलम से
TRIVIAL_TOKENSमें मौजूद items हटा दे। ऑर्डर को बनाए रखना ज़रूरी नहीं है. df2कीinऔरoutकॉलम से वे tokens हटा दें जोTRIVIAL_TOKENSमें आते हैं.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# 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()