summaryrefslogtreecommitdiff
path: root/src/main/java/com/zdjizhi/etl/Ip2IpGraphProcessFunction.java
blob: 5bce25db4483c4f6c1550860bf79de20299e569c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.zdjizhi.etl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.HashMap;
import java.util.Map;


/**
 * 对ip去重
 */
public class Ip2IpGraphProcessFunction extends ProcessWindowFunction<Map<String, Object>, Map<String, Object>, Tuple2<String, String>, TimeWindow> {

    private static final Log logger = LogFactory.get();

    @Override
    public void process(Tuple2<String, String> keys, Context context, Iterable<Map<String, Object>> elements, Collector<Map<String, Object>> out) {

        try {
            long lastFoundTime = DateUtil.currentSeconds();
            for (Map<String, Object> log : elements) {
                long connStartTimetime = Convert.toLong(log.get("start_time"));
                lastFoundTime = connStartTimetime > lastFoundTime ? connStartTimetime : lastFoundTime;
            }
            Map<String, Object> newLog = new HashMap<>();
            newLog.put("src_ip", keys.f0);
            newLog.put("dst_ip", keys.f1);
            newLog.put("last_found_time", lastFoundTime);
            out.collect(newLog);
            logger.debug("获取中间聚合结果:{}", newLog.toString());

        } catch (Exception e) {
            logger.error("获取中间聚合结果失败,middleResult: {}", e);
        }
    }

}