Introduction to PySpark
Benjamin Schmidt
Data Engineer
joined_df = large_df.join(broadcast(small_df),
on="key_column", how="inner")
joined_df.show()
# Using explain() to view the execution plan
df.filter(df.Age > 40).select("Name").explain()
== Physical Plan ==
*(1) Filter (isnotnull(Age) AND (Age > 30))
+- Scan ExistingRDD[Name:String, Age:Int]
df = spark.read.csv("large_dataset.csv", header=True, inferSchema=True) # Cache the DataFrame df.cache()
# Perform multiple operations on the cached DataFrame df.filter(df["column1"] > 50).show() df.groupBy("column2").count().show()
# Persist the DataFrame with storage level from pyspark import StorageLevel df.persist(StorageLevel.MEMORY_AND_DISK)
# Perform transformations result = df.groupBy("column3").agg({"column4": "sum"}) result.show() # Unpersist after use df.unpersist()
map()
over groupby()
due to selectivity of methodsIntroduction to PySpark