1. 问题
flinksql 开发,除了sql开发本身,还可能涉及到udf,组件,兑现有包扩展等需求,整一个项目方便开发测试打包版本管理。
2. 框架
2-1. 父模块: flink_parent
没有任何代码, 只有 在pom中有flink开发通用的依赖,供子模块集成。 子模块特有的jar不在这里配置。比如easyrule 模块需要的easyrule包, 没在flink_parent 模块配置,在easyrule子模块配置。
3. 现有包扩展:flink-connector-kafka
在对应版本的 flink-connector-kafka 源码基础上的扩展,在包里增加了一个接口:
@FunctionalInterface
public interface GetTopic {
List<String> getTopic( String param);
}
外部提供这个接口的实现类,并在createtable时调用,就可以根据自定义的函数,返回需要消费的topic。
with (
'connector' = 'kafka',
'topic_function' = 'sql.GetTopic1#11', ----实现类和函数方法的参数
'properties.bootstrap.servers' = '10.86.25.6:9092',
'properties.group.id' = 'testGroup1111',
'scan.startup.mode' = 'earliest-offset',
'format' = 'pwrd_decode'
);
忘记'scan.topic-partition-discovery.interval'='10s' 这个参数了, 每隔多久,调一下这个函数动态发现topic的partition。
还有一个关键参数 table.exec.source.idle-timeout=60s". flink很多计算依赖水印,水印取并发中最小的一个。 如果其中一个并发长期没数,水印不会涨,后面其他并发和下游的计算不会被处理。 设置一个时间,长时间没数,这个进程的水印就不再被考虑。
打包,替换原版的jar。
对其他包的扩展, 也这么建子模块。
4. 自定义组件:format
自定义了一个format组件,通过外部的正则表达式,和建表ddl字段,对日志进行解析。
建表ddl:
CREATE TABLE source_table (
$_logName string,
f0 string ,
f1 string ,
f2 string ,
f3 string ,
$_logSendTime string ,
$_logId string ,
$_projectName string,
`partition` BIGINT METADATA VIRTUAL,
`offset` BIGINT METADATA VIRTUAL
)
with (
'connector' = 'kafka',
'topic_function' = 'sql.GetTopic1#11',
'properties.bootstrap.servers' = '10.86.25.6:9092',
'properties.group.id' = 'testGroup1111',
'scan.startup.mode' = 'earliest-offset',
'format' = 'pwrd_decode' ------自定义format 标记
);
正则表达式配置:
{
"columns": [{
"regular": "savg=(?<f0>\\d+)"
}, {
"regular": "^.*IOPS=(?<f2>\\d+)\\b.*\\bavg=(?<f1>\\d+)\\b.*$"
},{
"regular": "savg=(?<f3>\\d+)"
}],
"filter": "^AAA.*$"
}
根据ddl表名字,找到对应的配置文件,通过多个正则表达式,给ddl中的属性赋值。
打包,上传。
5. common 通用类
通用的一些java类。
6. sql 开发模块
基于前面的模块, 写sql文件进行开发测试。内部有一个sql提交器,执行sql。
一个完整的sql文件例子,包含以下功能:
- set 参数
- 自定义函数
- 自定义kafkasource 方法
- 自定义format
- 建表
- 建视图
- 执行insert
- 执行select
-- -- 开启 mini-batch
-- SET table.exec.mini-batch.enabled=true;
-- -- mini-batch的时间间隔,即作业需要额外忍受的延迟
-- SET table.exec.mini-batch.allow-latency=10s;
-- -- 一个 mini-batch 中允许最多缓存的数据
-- SET table.exec.mini-batch.size=1000;
-- 状态保留时间
set table.exec.state.ttl= 20 s;
-- -- 开启 local-global 优化
-- SET table.optimizer.agg-phase-strategy=TWO_PHASE;
-- -- 开启 distinct agg 切分
-- SET table.optimizer.distinct-agg.split.enabled=true;
CREATE FUNCTION ToUpper AS 'udf.TestScalarFunction';
CREATE TABLE source_table (
$_logName string,
f0 string ,
f1 string ,
f2 string ,
f3 string ,
$_logSendTime string ,
$_logId string ,
$_projectName string,
`partition` BIGINT METADATA VIRTUAL,
`offset` BIGINT METADATA VIRTUAL
)
with (
'connector' = 'kafka',
'topic_function' = 'sql.GetTopic1#11',
'properties.bootstrap.servers' = '10.86.25.6:9092',
'properties.group.id' = 'testGroup1111',
'scan.startup.mode' = 'earliest-offset',
'format' = 'pwrd_decode'
);
create view source_view as select * from source_table;
create table print_table (
upper_name String,
$_logName string,
f0 string ,
f1 string ,
f2 string ,
f3 string ,
$_logSendTime string ,
$_logId string ,
$_projectName string
) with (
'connector'='print'
);
-- insert into print_table select toUpper($_logName), * from source_view;
select * from source_view ;
7. 总结
分别打包上传, sql调试完上线。