flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "sunjincheng (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FLINK-4781) Add User Defined Function(UDF) API for Table API
Date Mon, 10 Oct 2016 01:13:20 GMT

    [ https://issues.apache.org/jira/browse/FLINK-4781?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15560958#comment-15560958
] 

sunjincheng commented on FLINK-4781:
------------------------------------

User can define own functions to apply on fields. Build-in UDFs includes md5, length, concatWs,
hash and so on.


To defind your own user defined function, 
inherit from UDF class
define one or more evel function. 

NOTE: 
the eval method must be public and non-static.
eval should never be a void method.
eval method can be overload. Blink will choose the best match eval method to call according
to parameter types and number.

The following is a example of ABS:


public class UDFAbs extends UDF {

    public Double eval(Double n) {
        if (n == null) {
            return null;
        }
        return Math.abs(n);
    }

    public Long eval(Long n) {
        if (n == null) {
            return null;
        }
        return Math.abs(n);
    }

    public Integer eval(Integer n) {
        if (n == null) {
            return null;
        }
        return Math.abs(n);
    }
}
And in Scala Table API , we can use abs like this : 

val abs = new UDFAbs()
val res = tab.select(‘a, abs(‘b), ‘c)

And in Java Table API, we can use abs like this:

tableEnv.registerFunction("abs", new UDFAbs())
table.select("a, abs(b)")

And in SQL:

tableEnv.registerFunction("abs", new UDFAbs())
tableEnv.sql("SELECT a, abs(b) FROM MyTable")


> Add User Defined Function(UDF) API for Table API
> ------------------------------------------------
>
>                 Key: FLINK-4781
>                 URL: https://issues.apache.org/jira/browse/FLINK-4781
>             Project: Flink
>          Issue Type: New Feature
>          Components: Table API & SQL
>            Reporter: Shaoxuan Wang
>            Assignee: sunjincheng
>




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message