This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhanghongqing-knowledge-log/src/main/java/com/zdjizhi/etl/DnsRelationProcessFunction.java
2022-07-11 10:05:14 +08:00

59 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zdjizhi.etl;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import org.apache.flink.api.java.tuple.Tuple3;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashMap;
import java.util.Map;
import static com.zdjizhi.common.FlowWriteConfig.LOG_AGGREGATE_DURATION;
/**
* @author 94976
*/
public class DnsRelationProcessFunction extends ProcessWindowFunction<Map<String, Object>, Map<String, Object>, Tuple3<String, String, String>, TimeWindow> {
private static final Logger logger = LoggerFactory.getLogger(DnsRelationProcessFunction.class);
/**
* 拆分dns_record
* 聚合统计
* 五种a/aaaa/cname/mx/ns
*
* @param elements
* @return
*/
@Override
public void process(Tuple3<String, String, String> keys, Context context, Iterable<Map<String, Object>> elements, Collector<Map<String, Object>> out) {
try {
long sessions = 0L;
long startTime = DateUtil.currentSeconds();
long endTime = DateUtil.currentSeconds();
for (Map<String, Object> log : elements) {
sessions++;
long logStartTime = Convert.toLong(log.get("start_time"));
startTime = logStartTime < startTime ? logStartTime : startTime;
endTime = logStartTime > endTime ? logStartTime : endTime;
}
Map<String, Object> newDns = new LinkedHashMap<>();
newDns.put("start_time", startTime);
newDns.put("end_time", endTime + LOG_AGGREGATE_DURATION);
newDns.put("record_type", keys.f0);
newDns.put("qname", keys.f1);
newDns.put("record", keys.f2);
newDns.put("sessions", sessions);
out.collect(newDns);
} catch (Exception e) {
logger.error("dns relation 日志聚合失败: {}", e);
}
}
}