concept UDF in category pyspark

This is an excerpt from Manning's book Data Analysis with Python and PySpark.
PySpark also has a disadvantage when it comes to the Java and Scala API. Since Spark is at the core a Scala program, Python code have to be translated to and from JVM[3] instructions. While more recent versions have been bridging that gap pretty well, pure Python translation, which happens mainly when you’re defining your own User Defined Functions (UDF), will perform slower. We will cover UDF and some ways to mitigate the performance problem in Chapter 8.
In listing 8.9, I promote the
py_reduce_fraction()function to a UDF via theudf()function. Just like I did with the Python equivalent, I provide a return type to the UDF (this time, anArrayofLong, sinceArrayis the companion type of the tuple andLongthe one for Python integers). Once the UDF is created, we can apply it like any other PySpark function on columns. I chose to create a new column to showcase the before and after: in the sample shown, the fraction appears properly reduced.Listing 8.9. Creating a UDF explicitly with the
udf()function, and applying it to our data frame.SparkFrac = T.ArrayType(T.LongType()) #1 reduce_fraction = F.udf(py_reduce_fraction, SparkFrac) #2 frac_df = frac_df.withColumn( "reduced_fraction", reduce_fraction(F.col("fraction")) #3 ) frac_df.show(5, False) # +--------+----------------+ # |fraction|reduced_fraction| # +--------+----------------+ # |[0, 1] |[0, 1] | # |[0, 2] |[0, 1] | # |[0, 3] |[0, 1] | # |[0, 4] |[0, 1] | # |[0, 5] |[0, 1] | # +--------+----------------+ # only showing top 5 rowsYou also have the option to create your Python function and promote it as a UDF using the
udffunction as a decorator. In listing 8.10, I define bypy_fraction_to_float()(now called simplyfraction_to_float()) directly as a UDF by preceding my function definition by@F.udf([return_type]). In both cases, you can access the underlying function from the UDF by calling the attributefrac.Listing 8.10. Creating a UDF directly using the
udf()decorator.@F.udf(T.DoubleType()) #1 def fraction_to_float(frac: Frac) -> Optional[float]: """Transforms a fraction represented as a 2-tuple of integers into a float.""" num, denom = frac if denom: return num / denom return None frac_df = frac_df.withColumn( "fraction_float", fraction_to_float(F.col("reduced_fraction")) ) frac_df.select("reduced_fraction", "fraction_float").distinct().show(5, False) # +----------------+-------------------+ # |reduced_fraction|fraction_float | # +----------------+-------------------+ # |[3, 50] |0.06 | # |[3, 67] |0.04477611940298507| # |[7, 76] |0.09210526315789473| # |[9, 23] |0.391304347826087 | # |[9, 25] |0.36 | # +----------------+-------------------+ # only showing top 5 rows assert fraction_to_float.func((1, 2)) == 0.5 #2
At the core, pandas UDF can be seen as nothing more than distributing pandas data manipulation code within a data frame. In chapter 1, I explained that Spark distributes a large amount of data in multiple partitions and orchestrates transformations and actions through a master-workers split. In our model, a pandas UDF is just like treating each chunk of data like an independent pandas data frame.