UDFRegistration.registerJavaFunction(name: str, javaClassName: str, returnType: Optional[DataTypeOrString] = None) → None[source]

Register a Java user-defined function as a SQL function.

In addition to a name and the function itself, the return type can be optionally specified. When the return type is not specified we would infer it via reflection.

New in version 2.3.0.

Changed in version 3.4.0: Supports Spark Connect.


name of the user-defined function


fully qualified name of java class

returnTypepyspark.sql.types.DataType or str, optional

the return type of the registered Java function. The value can be either a pyspark.sql.types.DataType object or a DDL-formatted type string.


>>> from pyspark.sql.types import IntegerType
>>> spark.udf.registerJavaFunction(
...     "javaStringLength", "", IntegerType())
>>> spark.sql("SELECT javaStringLength('test')").collect()  
>>> spark.udf.registerJavaFunction(
...     "javaStringLength2", "")
>>> spark.sql("SELECT javaStringLength2('test')").collect()  
>>> spark.udf.registerJavaFunction(
...     "javaStringLength3", "", "integer")
>>> spark.sql("SELECT javaStringLength3('test')").collect()