public abstract class TableFunction<T> extends UserDefinedFunction
The behavior of a TableFunction
can be defined by implementing a custom evaluation
method. An evaluation method must be declared publicly, not static and named "eval".
Evaluation methods can also be overloaded by implementing multiple methods named "eval".
User-defined functions must have a default constructor and must be instantiable during runtime.
By default the result type of an evaluation method is determined by Flink's type extraction
facilities. This is sufficient for basic types or simple POJOs but might be wrong for more
complex, custom, or composite types. In these cases TypeInformation
of the result type
can be manually defined by overriding getResultType()
.
Internally, the Table/SQL API code generation works with primitive values as much as possible. If a user-defined table function should not introduce much overhead during runtime, it is recommended to declare parameters and result types as primitive types instead of their boxed classes. DATE/TIME is equal to int, TIMESTAMP is equal to long.
Example:
public class Split extends TableFunction<String> {
// implement an "eval" method with as many parameters as you want
public void eval(String str) {
for (String s : str.split(" ")) {
collect(s); // use collect(...) to emit an output row
}
}
// you can overload the eval method here ...
}
val tEnv: TableEnvironment = ...
val table: Table = ... // schema: [a: String]
// for Scala users
val split = new Split()
table.join(split('c) as ('s)).select('a, 's)
// for Java users
tEnv.registerFunction("split", new Split()) // register table function first
table.join("split(a) as (s)").select("a, s")
// for SQL users
tEnv.registerFunction("split", new Split()) // register table function first
tEnv.sql("SELECT a, s FROM MyTable, LATERAL TABLE(split(a)) as T(s)")
Constructor and Description |
---|
TableFunction() |
Modifier and Type | Method and Description |
---|---|
protected void |
collect(T row)
Emit an output row.
|
TypeInformation<T> |
getResultType()
Returns the result type of the evaluation method with a given signature.
|
void |
setCollector(Collector<T> collector)
Internal use.
|
String |
toString() |
close, functionIdentifier, open
protected void collect(T row)
row
- the output rowpublic final void setCollector(Collector<T> collector)
collector
- (undocumented)public TypeInformation<T> getResultType()
This method needs to be overriden in case Flink's type extraction facilities are not
sufficient to extract the TypeInformation
based on the return type of the evaluation
method. Flink's type extraction facilities can handle basic types or
simple POJOs but might be wrong for more complex, custom, or composite types.
TypeInformation
of result type or null if Flink should determine the typeCopyright © 2014–2018 The Apache Software Foundation. All rights reserved.