This article provides a step-by-step guide for installing the RisingWave Java UDF SDK, defining functions using Java, starting a Java process as a UDF server, and declaring and using UDFs in RisingWave.
The RisingWave Java UDF SDK is distributed as a Maven artifact. We have prepared a sample project so you don’t have to create it from scratch. Run the following command to clone the template repository.
A user-defined scalar function maps zero, one, or multiple scalar values to a new scalar value.
In order to define a scalar function, you have to create a new class that implements the ScalarFunction
interface in com.risingwave.functions
and implement exactly one evaluation method named eval(...)
. This method must be declared public and non-static.
Any data type listed in Data type mapping can be used as a parameter or return type of an evaluation method.
Here’s an example of a scalar function that calculates the greatest common divisor (GCD) of two integers:
DIFFERENCES WITH FLINK
ScalarFunction
is an interface instead of an abstract class.eval
methods are not supported.eval(Integer...)
are not supported.A user-defined table function maps zero, one, or multiple scalar values to one or multiple rows (structured types).
In order to define a table function, you have to create a new class that implements the TableFunction
interface in com.risingwave.functions
and implement exactly one evaluation method named eval(...)
. This method must be declared public and non-static.
The return type must be an Iterator
of any data type listed in Data type mapping.
Similar to scalar functions, input and output data types are automatically extracted using reflection. This includes the generic argument T of the return value for determining an output data type.
Here’s an example of a table function that generates a series of integers:
DIFFERENCES WITH FLINK
TableFunction
is an interface instead of an abstract class. It has no generic arguments.collect
to emit a row, the eval
method returns an Iterator
of the output rows.eval
methods are not supported.eval(Integer...)
are not supported.FROM
clause directly. JOIN LATERAL TABLE
is not supported.Run the following command to create a UDF server and register for the functions you defined.
Run the following command start the UDF server.
The UDF server will start running, allowing you to call the defined UDFs from RisingWave.
In RisingWave, use the CREATE FUNCTION command to declare the functions you defined.
Here are the SQL statements for declaring the two UDFs defined in step 3.
Once the UDFs are created in RisingWave, you can use them in SQL queries just like any built-in functions. For example:
The RisingWave Java UDF SDK supports the following data types:
SQL Type | Java Type | Notes |
---|---|---|
BOOLEAN | boolean, Boolean | |
SMALLINT | short, Short | |
INT | int, Integer | |
BIGINT | long, Long | |
REAL | float, Float | |
DOUBLE PRECISION | double, Double | |
DECIMAL | BigDecimal | |
DATE | java.time.LocalDate | |
TIME | java.time.LocalTime | |
TIMESTAMP | java.time.LocalDateTime | |
INTERVAL | com.risingwave.functions.PeriodDuration | |
VARCHAR | String | |
BYTEA | byte[] | |
JSONB | String | Use @DataTypeHint(“JSONB”) String as the type. See example. |
T[] | T’[] | T can be any of the above SQL types. T’ should be the corresponding Java type. |
STRUCT<> | user-defined class | Define a data class as the type. See example. |
Create the function in RisingWave
Create the function in RisingWave
This article provides a step-by-step guide for installing the RisingWave Java UDF SDK, defining functions using Java, starting a Java process as a UDF server, and declaring and using UDFs in RisingWave.
The RisingWave Java UDF SDK is distributed as a Maven artifact. We have prepared a sample project so you don’t have to create it from scratch. Run the following command to clone the template repository.
A user-defined scalar function maps zero, one, or multiple scalar values to a new scalar value.
In order to define a scalar function, you have to create a new class that implements the ScalarFunction
interface in com.risingwave.functions
and implement exactly one evaluation method named eval(...)
. This method must be declared public and non-static.
Any data type listed in Data type mapping can be used as a parameter or return type of an evaluation method.
Here’s an example of a scalar function that calculates the greatest common divisor (GCD) of two integers:
DIFFERENCES WITH FLINK
ScalarFunction
is an interface instead of an abstract class.eval
methods are not supported.eval(Integer...)
are not supported.A user-defined table function maps zero, one, or multiple scalar values to one or multiple rows (structured types).
In order to define a table function, you have to create a new class that implements the TableFunction
interface in com.risingwave.functions
and implement exactly one evaluation method named eval(...)
. This method must be declared public and non-static.
The return type must be an Iterator
of any data type listed in Data type mapping.
Similar to scalar functions, input and output data types are automatically extracted using reflection. This includes the generic argument T of the return value for determining an output data type.
Here’s an example of a table function that generates a series of integers:
DIFFERENCES WITH FLINK
TableFunction
is an interface instead of an abstract class. It has no generic arguments.collect
to emit a row, the eval
method returns an Iterator
of the output rows.eval
methods are not supported.eval(Integer...)
are not supported.FROM
clause directly. JOIN LATERAL TABLE
is not supported.Run the following command to create a UDF server and register for the functions you defined.
Run the following command start the UDF server.
The UDF server will start running, allowing you to call the defined UDFs from RisingWave.
In RisingWave, use the CREATE FUNCTION command to declare the functions you defined.
Here are the SQL statements for declaring the two UDFs defined in step 3.
Once the UDFs are created in RisingWave, you can use them in SQL queries just like any built-in functions. For example:
The RisingWave Java UDF SDK supports the following data types:
SQL Type | Java Type | Notes |
---|---|---|
BOOLEAN | boolean, Boolean | |
SMALLINT | short, Short | |
INT | int, Integer | |
BIGINT | long, Long | |
REAL | float, Float | |
DOUBLE PRECISION | double, Double | |
DECIMAL | BigDecimal | |
DATE | java.time.LocalDate | |
TIME | java.time.LocalTime | |
TIMESTAMP | java.time.LocalDateTime | |
INTERVAL | com.risingwave.functions.PeriodDuration | |
VARCHAR | String | |
BYTEA | byte[] | |
JSONB | String | Use @DataTypeHint(“JSONB”) String as the type. See example. |
T[] | T’[] | T can be any of the above SQL types. T’ should be the corresponding Java type. |
STRUCT<> | user-defined class | Define a data class as the type. See example. |
Create the function in RisingWave
Create the function in RisingWave