Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

4/29/24, 3:04 PM Creating User-Defined Functions (UDFs) for DataFrames in Java | Snowflake Documentation

Developer Snowpark API Java Creating User Defined Functions


Creating User-Defined Functions (UDFs)
for DataFrames in Java
The Snowpark API provides methods that you can use to create a user-defined
function from a lambda expression in Java. This topic explains how to create these
types of functions.
Introduction
You can call Snowpark APIs to create user-defined functions (UDFs) for lambda
expressions in Java, and you can call these UDFs to process the data in your
DataFrame.
When you use the Snowpark API to create a UDF, the Snowpark library serializes
and uploads the code for your UDF to a stage. When you call the UDF, the
Snowpark library executes your function on the server, where the data is located.
As a result, the data doesn’t need to be transferred to the client in order for the
function to process the data.
In your custom code, you can also call code that is packaged in JAR files (for
example, Java classes for a third-party library).
You can create a UDF for your custom code in one of two ways:
You can create an anonymous UDF and assign the function to a variable. As
long as this variable is in scope, you can use this variable to call the UDF.
import com.snowflake.snowpark_java.types.*;
...

// Create and register an anonymous UDF (doubleUdf)


// that takes in an integer argument and returns an integer valu
UserDefinedFunction doubleUdf =
Functions.udf((Integer x) -> x + x, DataTypes.IntegerType, Dat
// Call the anonymous UDF.
DataFrame df = session.table("sample_product_data");
DataFrame dfWithDoubleQuantity = df.withColumn("doubleQuantity",
dfWithDoubleQuantity.show();

You can create a named UDF and call the UDF by name. You can use this if, for
example, you need to call a UDF by name or use the UDF in a subsequent
https://docs.snowflake.com/en/developer-guide/snowpark/java/creating-udfs 1/1

You might also like