赞
踩
本篇幅介绍Flink Table/SQL中如何自定义一个表函数(TableFunction),介绍其基本用法以及与源码结合分析其调用流程。
表函数TableFunction相对标量函数ScalarFunction一对一,它是一个一对多的情况,通常使用TableFunction来完成列转行的一个操作。先通过一个实际案例了解其用法:终端设备上报数据,数据类型包含温度、耗电量等,上报方式是以多条方式上报,例如:
现在希望得到如下数据格式:
这是一个典型的列转行或者一行转多行的场景,需要将data列进行拆分成为多行多列,先看下代码实现:
- public class MyUDTF extends TableFunction<Row>{
-
- public void eval(String s){
- JSONArray jsonArray =JSONArray.parseArray(s);
- for(int i =0; i < jsonArray.size(); i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- String type = jsonObject.getString("type");
- String
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。