flink SourceFunction SinkFunction timeWindowAll reduce
1、实现SourceFunction接口生成数据源
/**
* @Description: 产生数据 traceid,userid,timestamp,status,response time
*/
public class SourceData implements SourceFunction<String> {
private volatile boolean Running = true;
static int status[] = {200, 404, 500, 501, 301}; @Override
public void run(SourceContext<String> ctx) throws Exception {
while (Running) {
Thread.sleep((int) (Math.random() * 10)); StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(UUID.randomUUID().toString());
stringBuffer.append(",");
stringBuffer.append((int) (Math.random() * 100));
stringBuffer.append(",");
stringBuffer.append(System.currentTimeMillis());
stringBuffer.append(",");
stringBuffer.append(status[(int) (Math.random() * 4)]);
stringBuffer.append(",");
stringBuffer.append((int)(Math.random()*200)); ctx.collect(stringBuffer.toString());
}
} @Override
public void cancel() { }
}
2、实现SinkFunction接口,实现数据下沉存储及使用
public class TraceSourceData {
public static void main(String args[]) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<Tuple5<String, Integer, Long, Integer, Integer>> ds =
env.addSource(new SourceData())
.flatMap(new FlatMapFunction<String, Tuple5<String, Integer, Long, Integer, Integer>>() {
@Override
public void flatMap(String value, Collector<Tuple5<String, Integer, Long, Integer, Integer>> out) throws Exception {
String ss[] = value.split(",");
out.collect(Tuple5.of(ss[0], Integer.parseInt(ss[1]), Long.parseLong(ss[2]), Integer.parseInt(ss[3]), Integer.parseInt(ss[4])));
}
});
//5秒窗口统计各状态的次数
DataStream<Tuple2<Integer, Integer>> statusData = ds
.flatMap(new FlatMapFunction<Tuple5<String, Integer, Long, Integer, Integer>, Tuple2<Integer, Integer>>() {
@Override
public void flatMap(Tuple5<String, Integer, Long, Integer, Integer> value, Collector<Tuple2<Integer, Integer>> out) throws Exception {
out.collect(Tuple2.of(value.f3, 1));
}
})
.keyBy(0)
.timeWindow(Time.seconds(5))
.sum(1);
statusData.print().setParallelism(1);
//5秒窗口统计响应时间大于50的用户访问次数在整个响应中的占比
//大于50,小于等于50,所有次数
DataStream<Tuple3<Integer, Integer, Integer>> greater100UserPer = ds
.flatMap(new FlatMapFunction<Tuple5<String, Integer, Long, Integer, Integer>, Tuple3<Integer, Integer, Integer>>() {
@Override
public void flatMap(Tuple5<String, Integer, Long, Integer, Integer> value, Collector<Tuple3<Integer, Integer, Integer>> out) throws Exception {
if (value.f4 > 50)
out.collect(Tuple3.of(1, 0, 1));
else
out.collect(Tuple3.of(0, 1, 1));
}
})//注意这里,没有使用keyBy
.timeWindowAll(Time.seconds(5))
.reduce(new ReduceFunction<Tuple3<Integer, Integer, Integer>>() {
@Override
public Tuple3<Integer, Integer, Integer> reduce(Tuple3<Integer, Integer, Integer> value1, Tuple3<Integer, Integer, Integer> value2) throws Exception {
return Tuple3.of(value1.f0 + value2.f0, value1.f1 + value2.f1, value1.f2 + value2.f2);
}
})//正常情况下应该重新起一个Double的数据类型,这里懒得麻烦,直接就做map转换了
.map(new MapFunction<Tuple3<Integer, Integer, Integer>, Tuple3<Integer, Integer, Integer>>() {
@Override
public Tuple3<Integer, Integer, Integer> map(Tuple3<Integer, Integer, Integer> value) throws Exception {
Double rate1 = (value.f0.doubleValue() / value.f2.doubleValue()) * 100;
Double rate2 = (value.f1.doubleValue() / value.f2.doubleValue()) * 100;
return Tuple3.of(rate1.intValue(), rate2.intValue(), 1);
}
});
//SinkFunction,实现接口后,可以随意处理数据
greater100UserPer.addSink(new SinkFunction<Tuple3<Integer, Integer, Integer>>() {
@Override
public void invoke(Tuple3<Integer, Integer, Integer> value, Context context) throws Exception {
System.out.println(LocalDateTime.ofInstant(Instant.ofEpochMilli(context.timestamp()), ZoneId.systemDefault()) + " " + value);
}
});
env.execute("TraceSourceData");
}
}
flink SourceFunction SinkFunction timeWindowAll reduce的更多相关文章
- [源码解析] Flink的groupBy和reduce究竟做了什么
[源码解析] Flink的groupBy和reduce究竟做了什么 目录 [源码解析] Flink的groupBy和reduce究竟做了什么 0x00 摘要 0x01 问题和概括 1.1 问题 1.2 ...
- Generic/Template Programming in Flink
Generic/Template Programming in Flink SourceFunction<T> @Public public interface SourceFunctio ...
- [源码解析] GroupReduce,GroupCombine 和 Flink SQL group by
[源码解析] GroupReduce,GroupCombine和Flink SQL group by 目录 [源码解析] GroupReduce,GroupCombine和Flink SQL grou ...
- [源码解析] Flink UDAF 背后做了什么
[源码解析] Flink UDAF 背后做了什么 目录 [源码解析] Flink UDAF 背后做了什么 0x00 摘要 0x01 概念 1.1 概念 1.2 疑问 1.3 UDAF示例代码 0x02 ...
- Alink漫谈(八) : 二分类评估 AUC、K-S、PRC、Precision、Recall、LiftChart 如何实现
Alink漫谈(八) : 二分类评估 AUC.K-S.PRC.Precision.Recall.LiftChart 如何实现 目录 Alink漫谈(八) : 二分类评估 AUC.K-S.PRC.Pre ...
- Alink漫谈(十六) :Word2Vec源码分析 之 建立霍夫曼树
Alink漫谈(十六) :Word2Vec源码分析 之 建立霍夫曼树 目录 Alink漫谈(十六) :Word2Vec源码分析 之 建立霍夫曼树 0x00 摘要 0x01 背景概念 1.1 词向量基础 ...
- flink Reduce、GroupReduce、GroupCombine笔记
1.reduce操作,在分组的dataset上使用,也可以在不分组的dataset上使用 应用于分组DataSet的Reduce转换使用用户定义的reduce函数将每个组减少为单个元素.对于每组输入元 ...
- flink流处理从0到1
一.DataStream API之Data Sources(消费者之数据源) 介绍: source是程序的数据源输入,你可以通过StreamExecutionEnvironment.addSource ...
- flink实时数仓从入门到实战
第一章.flink实时数仓入门 一.依赖 <!--Licensed to the Apache Software Foundation (ASF) under oneor more contri ...
随机推荐
- soapui学习
另外分享几个公开的Webservice站点,你可以随便招几个服务来测试 http://www.webservicex.net/WS/wscatlist.aspx http://www.service- ...
- SpringDataJPA对SimpleJpaRepository/JPARepository返回结果的进一步处理(大体浏览,没细看)
package com.yb.fw.core.helper; public enum Op { LIKE,// like NOTLIKE,// notlike EQ,// = NOTEQ,// != ...
- RSA介绍
RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用. 公钥(Public Key)与私钥(Private Key)是通过一种算法得到的一个密钥对(即一个公钥和一个私钥),公 ...
- linux中sleep函数的使用和总结
在linux编程中,有时候会用到定时功能,常见的是用sleep(time)函数来睡眠time秒:但是这个函数是可以被中断的,也就是说当进程在睡眠的过程中,如果被中断,那么当中断结束回来再执行该进程的时 ...
- ASP.NET Core 中的 Main 方法
ASP.NET Core 中的 Main 方法 在 ASP.NET Core 项目中,我们有一个名为Program.cs的文件.在这个文件中,我们有一个public static void Main( ...
- Linux性能优化实战学习笔记:第十二讲
一.性能优化方法论 不可中断进程案例 二.怎么评估性能优化的效果? 1.评估思路 2.几个为什么 1.为什么要选择不同维度的指标? 应用程序和系统资源是相辅相成的关系 2.性能优化的最终目的和结果? ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- 打造个人专属网盘nextcloud
原文 https://edu.aliyun.com/course/150/lesson/list?spm=5176.9278281.815111.sence.114d4f3eLLMS53
- 工作中常用的Linux命令介绍与实践
前言 做后端开发的同学,一般都会接触到服务器,而我们现在的系统用的比较多的服务器系统就是linux了,平时多多少少也会接触到一些linux下的shell命令.我们来介绍下linux一些常用的命令和使用 ...
- C# HTTP系列1 HttpWebRequest类
系列目录 [已更新最新开发文章,点击查看详细] .NET Framework 中 System.Net 命名空间下提供了 HttpWebRequest 和 HttpWebResponse 2个 ...