UDF of SQL transform plugin

Description

Use UDF SPI to extends the SQL transform functions lib.

UDF API

  1. package org.apache.seatunnel.transform.sql.zeta;
  2. public interface ZetaUDF {
  3. /**
  4. * Function name
  5. *
  6. * @return function name
  7. */
  8. String functionName();
  9. /**
  10. * The type of function result
  11. *
  12. * @param argsType input arguments type
  13. * @return result type
  14. */
  15. SeaTunnelDataType<?> resultType(List<SeaTunnelDataType<?>> argsType);
  16. /**
  17. * Evaluate
  18. *
  19. * @param args input arguments
  20. * @return result value
  21. */
  22. Object evaluate(List<Object> args);
  23. }

UDF Implements Example

Add these dependencies and provided scope to your maven project:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.seatunnel</groupId>
  4. <artifactId>seatunnel-transforms-v2</artifactId>
  5. <version>2.3.2</version>
  6. <scope>provided</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.apache.seatunnel</groupId>
  10. <artifactId>seatunnel-api</artifactId>
  11. <version>2.3.2</version>
  12. <scope>provided</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>com.google.auto.service</groupId>
  16. <artifactId>auto-service</artifactId>
  17. <version>1.0.1</version>
  18. <scope>provided</scope>
  19. </dependency>
  20. </dependencies>

Add a Java Class implements of ZetaUDF like this:

  1. @AutoService(ZetaUDF.class)
  2. public class ExampleUDF implements ZetaUDF {
  3. @Override
  4. public String functionName() {
  5. return "EXAMPLE";
  6. }
  7. @Override
  8. public SeaTunnelDataType<?> resultType(List<SeaTunnelDataType<?>> argsType) {
  9. return BasicType.STRING_TYPE;
  10. }
  11. @Override
  12. public Object evaluate(List<Object> args) {
  13. String arg = (String) args.get(0);
  14. if (arg == null) return null;
  15. return "UDF: " + arg;
  16. }
  17. }

Package the UDF project and copy the jar to the path: ${SEATUNNEL_HOME}/lib. And if your UDF use third party library, you also need put it to ${SEATUNNEL_HOME}/lib.
If you use cluster mode, you need put the lib to all your node’s ${SEATUNNEL_HOME}/lib folder and re-start the cluster.

Example

The data read from source is a table like this:

id name age
1 Joy Ding 20
2 May Ding 21
3 Kin Dom 24
4 Joy Dom 22

We use UDF of SQL query to transform the source data like this:

  1. transform {
  2. Sql {
  3. source_table_name = "fake"
  4. result_table_name = "fake1"
  5. query = "select id, example(name) as name, age from fake"
  6. }
  7. }

Then the data in result table fake1 will update to

id name age
1 UDF: Joy Ding 20
2 UDF: May Ding 21
3 UDF: Kin Dom 24
4 UDF: Joy Dom 22

Changelog

new version

  • Add UDF of SQL Transform Connector