A scalar function takes zero, one, or more scalar values as input and returns a single scalar value.
Add the flink-table-common dependency to pom.xml. Set the version according to the Flink engine version selected for the task.
<dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-table-common</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency>
Code Implementation
Inherit the org.apache.flink.table.functions.ScalarFunction class and implement one or more eval functions. The eval function can accept multiple parameters.
This document provides an example of a simple scalar function that takes a String as input and returns an integer hash value.
public class HashCode extends ScalarFunction { // 0. Must be implemented. public int eval(String s) { return s.hashCode() * 100; } // 1. If an input parameter is a complex type, use getParameterTypes to specify its type. @Override public TypeInformation<?>[] getParameterTypes(Class<?>[] signature) { TypeInformation[] typeInformation = new TypeInformation[1]; // The input parameter is a String. typeInformation[0] = Types.STRING; return typeInformation; } // 2. If the return data is a complex type, use getResultType to specify its type. @Override public TypeInformation<?> getResultType(Class<?>[] signature) // The return type is int. return Types.INT; } }
To learn how to use a custom scalar function, see Create a Custom Function.