Skip to main content

Prerequisites

  • Ensure that you have Java Developer’s Kit (JDK) (11 or later) installed on your computer.
  • Ensure that you have Apache Maven (3.0 or later) installed on your computer. Maven is a build tool that helps manage Java projects and dependencies.

1. Create a Maven project from template

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.
To create a new project using the RisingWave Java UDF SDK, follow these steps:Generate a new Maven project:
Configure your pom.xml file as follows:
The --add-opens flag must be added when running unit tests through Maven:

2. Define your functions in Java

Scalar functions

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 ScalarFunctioninterface 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
  • The ScalarFunction is an interface instead of an abstract class.
  • Multiple overloaded eval methods are not supported.
  • Variable arguments such as eval(Integer...) are not supported.

Table functions

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 TableFunctioninterface 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
  • The TableFunction is an interface instead of an abstract class. It has no generic arguments.
  • Instead of calling collect to emit a row, the eval method returns an Iterator of the output rows.
  • Multiple overloaded eval methods are not supported.
  • Variable arguments such as eval(Integer...) are not supported.
  • In SQL, table functions can be used in the FROM clause directly. JOIN LATERAL TABLE is not supported.

3. Start a UDF server

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.

4. Declare your functions in 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.

5. Use your functions in RisingWave

Once the UDFs are created in RisingWave, you can use them in SQL queries just like any built-in functions. For example:

Data type mapping

The RisingWave Java UDF SDK supports the following data types:

Example - JSONB

Define the function in Java
Create the function in RisingWave

Example - Struct type

Define the function in Java
Create the function in RisingWave