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 functionsmap 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中:

哪些函数强制要求实现

哪些函数是可选的,可以不实现。

 

 成员函数实现要求哪些特殊场景下必须实现该函数备注官方文档完整实例
UDFeval必须实现 返回常见类型的数据完整案例
UDAFcreateAccumulator必须实现 返回自定义类完整案例
accumulate必须实现 操作自定义类
getValue必须实现 

操作自定义类

返回常见类型的数据

merge

不要求实现

bounded aggregations

session group window

session window

hop window

two phase aggregation optimization

操作自定义类

返回常见类型的数据

retract不要求实现

OVER windows

操作自定义类

返回常见类型的数据

UDTFeval必须实现 返回Collector完整案例
UDTAFcreateAccumulator必须实现 操作自定义类完整案例
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. 
The emitValue(...) would emit all N values each time. 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值