前言
在很多情况下,业务上读取的数据源渠道可能有很多种,比如前文的读取外部文件,读取来自kafka的数据等,如果现有的输入数据源仍然不能满足要求的情况下,就可以考虑自定义source 数据源
代码展示
自定义数据源中,flink提供了一个 addSource的方法,通过传入自定义的函数类(该函数类需要实现SourceFuntion接口),然后再在自定义的这个类中编写自身的处理逻辑即可,下面看具体的代码实现
1、自定义source类
public static class MySensorSource implements SourceFunction<SensorReading> {
private boolean running = true;
@Override
public void run(SourceContext<SensorReading> ctx) throws Exception {
Random random = new Random();
Map<String, Double> tempMap = new HashMap<>();
for (int i = 0; i < 10; i++) {
tempMap.put("sensor_" + (i + 1), 60 + random.nextGaussian() * 20);
}
while (true) {
for (Strin