You need to enable JavaScript to run this app.
Streaming Computing Flink Version

Streaming Computing Flink Version

Copy page
Download PDF
Custom Function Demo
Scalar Functions
Copy page
Download PDF
Scalar Functions

A scalar function takes zero, one, or more scalar values as input and returns a single scalar value.

How to Implement

  1. Add the flink-table-common dependency to pom.xml. Set the version according to the Flink engine version selected for the task.

    1. Version 1.16.3 is recommended for Flink 1.16.
    2. Version 1.17.2 is recommended for Flink 1.17.
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-table-common</artifactId>
        <version>${flink.version}</version>
        <scope>provided</scope>
    </dependency>
    
  2. 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.

Demo

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;
  }
}

How to Use

To learn how to use a custom scalar function, see Create a Custom Function.

Last updated: 2026.04.01 11:03:59