Flink的UDF写法整理
概述
这篇博客并不是简单地翻译官方文档,
而是挑选常用的信息,其他忽略(例如类型推断这种内容)
Overview
| 几种UDF | 文档中的解释 | 备注 |
| Scalar functions | map scalar values to a new scalar value. | 进一出一 |
| Table functions | map scalar values to new rows(row指的是一行数据). | 进一出多 |
| Aggregate functions | map scalar values of multiple rows to a new scalar value. | 聚合操作 |
| Table aggregate functions | map scalar values of multiple rows to new rows. | 进多出多 |
| Async table functions | are special functions for table sources that perform a lookup. | 文档里面没写 |
Function Class
udf类必须是public,不能是abstract,必须全局可访问,non-static或者匿名的类是不允许的。
For storing a user-defined function in a persistent catalog, the class must have a default constructor and must be instantiable during runtime.
Mandatory and Optional Methods
这里讲的是各種UDF中:
哪些函数强制要求实现
哪些函数是可选的,可以不实现。
| 成员函数 | 实现要求 | 哪些特殊场景下必须实现该函数 | 备注 | 官方文档完整实例 | |
| UDF | eval | 必须实现 | 返回常见类型的数据 | 完整案例 | |
| UDAF | createAccumulator | 必须实现 | 返回自定义类 | 完整案例 | |
| accumulate | 必须实现 | 操作自定义类 | |||
| getValue | 必须实现 | 操作自定义类 返回常见类型的数据 | |||
| merge | 不要求实现 | bounded aggregations session group window session window hop window two phase aggregation optimization | 操作自定义类 返回常见类型的数据 | ||
| retract | 不要求实现 | OVER windows | 操作自定义类 返回常见类型的数据 | ||
| UDTF | eval | 必须实现 | 返回Collector | 完整案例 | |
| UDTAF | createAccumulator | 必须实现 | 操作自定义类 | 完整案例 | |
| accumulate | 必须实现 | 操作自定义类 | |||
| emitValue 或 emitUpdateWithRetract 二选一 | 必须实现 | 操作Collector | |||
| merge | 不要求实现 | session group window many bounded aggregations unbounded session hop window aggregations | 操作自定义类 | ||
| retract | 不要求实现 | aggregations on OVER windows | |||
| emitValue | 不要求实现 | bounded and window aggregations | emits the full data according to the accumulator
Take a Top N function as an example. | ||
| emitUpdateWithRetract | 不要求实现 | emit values that have been updated under retract mode | Once there is an update, the method can retract old records before sending new, updated ones. The method will be used in preference to the emitValue(...) method.
操作Collector |
所有上述提到的函数必须被声明为public以及非static
返回多条数据的都需要写和Collector相关的语句。
上述表格中的所有案例都来自官方文档的补全,并且测试通过。(需要注意例子中的value是FLINK SQL的关键词,所以需要加上``,也就是`value`)
下面这个没看懂
If the table aggregate function can only be applied in an OVER window, this can be declared by returning the requirement FunctionRequirement.OVER_WINDOW_ONLY in getRequirements().
Reference:
[1]User-defined Functions
[2]User-defined Sources & Sinks
[3]General User-defined Functions
创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖总结
以上是生活随笔为你收集整理的Flink的UDF写法整理的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: csv->Flink SQL->Clic
- 下一篇: 记录apt安装时间