2021-09-27 11:13:25 +08:00
|
|
|
package com.zdjizhi.topology;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.log.Log;
|
|
|
|
|
import cn.hutool.log.LogFactory;
|
|
|
|
|
import com.zdjizhi.common.StreamAggregateConfig;
|
|
|
|
|
import com.zdjizhi.utils.functions.*;
|
2022-03-09 10:05:54 +08:00
|
|
|
import com.zdjizhi.utils.kafka.KafkaConsumer;
|
|
|
|
|
import com.zdjizhi.utils.kafka.KafkaProducer;
|
2021-11-20 11:30:08 +03:00
|
|
|
import org.apache.flink.api.java.tuple.Tuple2;
|
2022-01-19 08:56:23 +03:00
|
|
|
import org.apache.flink.api.java.tuple.Tuple3;
|
2021-09-27 11:13:25 +08:00
|
|
|
import org.apache.flink.streaming.api.datastream.DataStream;
|
|
|
|
|
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
|
|
|
|
|
import org.apache.flink.streaming.api.datastream.WindowedStream;
|
|
|
|
|
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
|
|
|
|
|
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
|
|
|
|
|
import org.apache.flink.streaming.api.windowing.time.Time;
|
|
|
|
|
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author qidaijie
|
|
|
|
|
* @Package com.zdjizhi.topology
|
|
|
|
|
* @Description:
|
|
|
|
|
* @date 2021/5/2016:42
|
|
|
|
|
*/
|
|
|
|
|
public class StreamAggregateTopology {
|
|
|
|
|
private static final Log logger = LogFactory.get();
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
try {
|
|
|
|
|
final StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
|
|
|
|
|
|
2021-11-20 11:30:08 +03:00
|
|
|
//两个输出之间的最大时间 (单位milliseconds)
|
|
|
|
|
environment.setBufferTimeout(StreamAggregateConfig.BUFFER_TIMEOUT);
|
|
|
|
|
|
2022-03-09 10:05:54 +08:00
|
|
|
DataStream<String> streamSource = environment.addSource(KafkaConsumer.getKafkaConsumer())
|
2022-04-01 11:47:56 +08:00
|
|
|
.setParallelism(StreamAggregateConfig.SOURCE_PARALLELISM).name(StreamAggregateConfig.SOURCE_KAFKA_TOPIC);
|
2021-09-27 11:13:25 +08:00
|
|
|
|
2022-01-19 08:56:23 +03:00
|
|
|
SingleOutputStreamOperator<Tuple3<String, String, String>> parseDataMap = streamSource.map(new ParseMapFunction())
|
2021-11-20 11:30:08 +03:00
|
|
|
.name("ParseDataMap")
|
2021-09-27 11:13:25 +08:00
|
|
|
.setParallelism(StreamAggregateConfig.PARSE_PARALLELISM);
|
|
|
|
|
|
2022-01-19 08:56:23 +03:00
|
|
|
WindowedStream<Tuple3<String, String, String>, String, TimeWindow> firstWindow = parseDataMap.keyBy(new FirstKeyByFunction())
|
|
|
|
|
.window(TumblingProcessingTimeWindows.of(Time.seconds(StreamAggregateConfig.FIRST_COUNT_WINDOW_TIME)));
|
2021-11-20 11:30:08 +03:00
|
|
|
|
|
|
|
|
SingleOutputStreamOperator<Tuple2<String, String>> metricCountWindow = firstWindow.process(new FirstCountWindowFunction())
|
|
|
|
|
.name("FirstCountWindow")
|
|
|
|
|
.setParallelism(StreamAggregateConfig.FIRST_WINDOW_PARALLELISM);
|
|
|
|
|
|
|
|
|
|
WindowedStream<Tuple2<String, String>, String, TimeWindow> secondWindow = metricCountWindow.keyBy(new SecondKeyByFunction())
|
2022-01-19 08:56:23 +03:00
|
|
|
.window(TumblingProcessingTimeWindows.of(Time.seconds(StreamAggregateConfig.SECOND_COUNT_WINDOW_TIME)));
|
2021-09-27 11:13:25 +08:00
|
|
|
|
2021-11-20 11:30:08 +03:00
|
|
|
SingleOutputStreamOperator<String> secondCountWindow = secondWindow.process(new SecondCountWindowFunction())
|
|
|
|
|
.name("SecondCountWindow").setParallelism(StreamAggregateConfig.SECOND_WINDOW_PARALLELISM);
|
2021-09-27 11:13:25 +08:00
|
|
|
|
2022-04-01 11:47:56 +08:00
|
|
|
SingleOutputStreamOperator<String> resultFlatMap = secondCountWindow.flatMap(new ResultFlatMapFunction())
|
|
|
|
|
.name("ResultFlatMap").setParallelism(StreamAggregateConfig.SINK_PARALLELISM);
|
|
|
|
|
|
|
|
|
|
resultFlatMap.addSink(KafkaProducer.getKafkaProducer()).name("LogSinkKafka")
|
|
|
|
|
.setParallelism(StreamAggregateConfig.SINK_PARALLELISM).name(StreamAggregateConfig.SINK_KAFKA_TOPIC);
|
2021-09-27 11:13:25 +08:00
|
|
|
|
|
|
|
|
environment.execute(args[0]);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("This Flink task start ERROR! Exception information is :" + e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|