PySpark 面试题与答案
问题 21. What is the purpose of the 'groupBy' operation in PySpark?
'groupBy' is used to group the data based on one or more columns. It is often followed by aggregation functions to perform operations on each group.
Example:
grouped_data = df.groupBy('Category').agg({'Price': 'mean'})
这有帮助吗?
添加评论
查看评论
问题 22. Explain the difference between 'cache' and 'persist' operations in PySpark.
'Cache' is a shorthand for 'persist(memory_only=True)', while 'persist' allows more flexibility by specifying storage levels (memory-only, disk-only, etc.).
Example:
df.cache()
这有帮助吗?
添加评论
查看评论
问题 23. How can you create a temporary view from a PySpark DataFrame?
You can use the 'createOrReplaceTempView' method to create a temporary view from a PySpark DataFrame.
Example:
df.createOrReplaceTempView('temp_view')
这有帮助吗?
添加评论
查看评论
问题 24. What is the purpose of the 'orderBy' operation in PySpark?
'OrderBy' is used to sort the rows of a DataFrame based on one or more columns.
Example:
result = df.orderBy('column')
这有帮助吗?
添加评论
查看评论
问题 25. Explain the role of the 'broadcast' variable in PySpark.
A 'broadcast' variable is used to cache a read-only variable in each node of a cluster to enhance the performance of joins.
Example:
from pyspark.sql.functions import broadcast
result = df1.join(broadcast(df2), 'key')
这有帮助吗?
添加评论
查看评论
用户评价最有帮助的内容: