当前位置:网站首页>Flink SQL自定义解析 Map和Array数据类型
Flink SQL自定义解析 Map和Array数据类型
2022-07-20 05:31:00 【我的名字叫洪】
话不多说,直接上代码
数据样例:
{
"fileInfo": [
{
"fileId": 4169200573588,
"fileSize": 21.0,
"fileType": 5,
"md5": "B54FA0BB16D9D1180619FC5D4D653494"
},
"subjectMap": {
"1234": 0,
"4455": 4373
}
],
"serverIp": "10.101.0.1"
}
解析Array:
/**
* 解析数组类型数据,转成多行
*/
@FunctionHint(output = @DataTypeHint("ROW<fileId BIGINT,fileSize STRING,fileType INT,md5 STRING>"))
public class ParserJsonArray extends TableFunction<Row> {
private static final Logger LOG = LoggerFactory.getLogger(ParserJsonArray.class);
public void eval(String value) {
try {
JSONArray jsonArray = JSONArray.parseArray(value);
Iterator<Object> iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JSONObject jsonObject = (JSONObject) iterator.next();
Long fileId = jsonObject.getLong("fileId");
String fileSize = jsonObject.getString("fileSize");
Integer fileType = jsonObject.getInteger("fileType");
String md5 = jsonObject.getString("md5");
collect(Row.of(fileId, fileSize,fileType,md5));
}
} catch (Exception e) {
LOG.error("parser json failed :" + e.getMessage());
}
}
}
解析Map:
/**
* 解析map类型数据,转成多行
*/
@FunctionHint(output = @DataTypeHint("Row< key STRING, value INT>"))
public class ParserMap extends TableFunction<Row> {
public void eval(Map<String, Integer> map) {
try {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Integer value = map.get(key);
collect(Row.of(key, value));
}
} catch (Exception e) {
}
}
}
引用函数:
//引用自定义udf函数,解析map类型数据
tableEnv.createTemporaryFunction("parserMap", classOf[com.data.ParserMap])
//构建数据查询语句
var querySql =
"select key,valuie from " + table + " CROSS JOIN lateral TABLE (parserMap(subjectMap)) AS t (key,valuie)"
var queryTable = tableEnv.sqlQuery(querySql.stripMargin)
完成
边栏推荐
猜你喜欢
如何用两个队列模拟实现一个栈
[dish of learning notes dog learning C] data storage
工具-IDEA 好看的样式字体/字号/
ClickHouse启动失败_Unit clickhouse-server.service entered failed state
如何使用 IDEA 打 jar 包
Chrome 进程架构
kettle_配置数据库连接_报错
OpenLayers ol-ext:Transform 对象,旋转、拉伸、放大
Introduction to microservice theory
【学习笔记之菜Dog学C】初识常见关键字、#define定义常量和宏
随机推荐
Ctfshow web entry information collection WP (1-20) (detailed explanation)
idea配置
问题 B: 蓝桥杯2020年第十一届省赛真题-回文日期
用户登录Demo
Basic syntax of symbol, iterator and generator in ES6
Oracle implements the limit effect: the use of rownum
Visual Studio 开发环境的配置
05—— mvvm 模型
Towards Representation Alignment and Uniformity in Collaborative Filtering
Call Win32 function in QT
B. Making Towers
数组常用方法
[dish of learning notes dog learning C] function recursion
Question B: the real topic of the 11th Bluebridge cup 2020 - palindrome date
[dish of learning notes dog learning C] array
ModuleNotFoundError: No module named ‘PyEMD‘ 解决方法
【C】 Introduction to C language
Los Angeles: t226229 arithmetic series
【学习笔记之菜Dog学C】初识常见关键字、#define定义常量和宏
B. All Distinct