Skip to main content

Spark User Defined Functions


Spark User Defined Functions

   
·        Write UDFs with a single DataFrame column inputs
·        Transform data using UDFs using both the DataFrame and SQL API
·        Analyze the performance trade-offs between built-in functions and UDFs


How to register UDF?
Python user-defined function (UDF) example:
def squared(s):
  return s * s
spark.udf.register("squaredWithPython", squared)

How to invoke UDF?
spark.range(1, 20).registerTempTable("test")



from pyspark.sql.functions import udf
from pyspark.sql.types import LongType
squared_udf = udf(squared, LongType())
df = spark.table("test")
display(df.select("id", squared_udf("id").alias("id_squared")))


Comments

Popular posts from this blog

Install pip on Windows

If you haven't downloaded get-pip.py yet: You need to download this file from the official pip website or use curl in your command prompt. Using curl (recommended): Open Command Prompt and type: DOS curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py This will download the file to your current directory in the Command Prompt. Manual Download: Go to https://bootstrap.pypa.io/get-pip.py in your web browser, right-click on the page, select "Save as...", and save the file as get-pip.py to a location you can easily navigate to (e.g., your Downloads folder, or a dedicated Python projects folder). The file is in a different directory: You might have downloaded get-pip.py to your Downloads folder, your Desktop, or another location, but you're trying to run the command from C:\Users\gparn . Solution: Navigate to the directory where you saved get-pip.py in your Command Prompt using the cd (change directory) command. Example (if in Downloads): DOS cd C:\User...