flink watermark介绍
转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7610412.html
一 概念
watermark是flink为了处理eventTime窗口计算提出的一种机制,本质上也是一种时间戳,由flink souce或者自定义的watermark生成器按照需求定期或者按条件生成一种系统event,与普通数据流event一样流转到对应的下游operations,接收到watermark数据的operator以此不断调整自己管理的window event time clock。
( A watermark is a special event signaling that time in the event stream (i.e., the real-world timestamps in the event stream) has reached a certain point (say, 10am), and thus no event with timestamp earlier than 10am will arrive from now on. These watermarks are part of the data stream alongside regular events, and a Flink operator advances its event time clock to 10am once it has received a 10am watermark from all its upstream operations/sources)

二 TimestampAssigner和Watermark
首先,eventTime计算意味着flink必须有一个地方用于抽取每条消息中自带的时间戳,所以TimestampAssigner的实现类都要具体实现
long extractTimestamp(T element, long previousElementTimestamp);方法用来抽取当前元素的eventTime,这个eventTime会用来决定元素落到下游的哪个或者哪几个window中进行计算。
其次,在数据进入window前,需要有一个Watermarker生成当前的event time对应的水位线,flink支持两种后置的Watermarker:Periodic和Punctuated,一种是定期产生watermark(即使没有消息产生),一种是在满足特定情况的前提下触发。两种Watermark分别需要实现接口为
Watermark getCurrentWatermark()和Watermark checkAndGetNextWatermark(T lastElement, long extractedTimestamp);
帖几个官网给出的实现样例
Periodic Watermarks
/**
* This generator generates watermarks assuming that elements arrive out of order,
* but only to a certain degree. The latest elements for a certain timestamp t will arrive
* at most n milliseconds after the earliest elements for timestamp t.
*/
public class BoundedOutOfOrdernessGenerator extends AssignerWithPeriodicWatermarks<MyEvent> { private final long maxOutOfOrderness = 3500; // 3.5 seconds private long currentMaxTimestamp; @Override
public long extractTimestamp(MyEvent element, long previousElementTimestamp) {
long timestamp = element.getCreationTime();
currentMaxTimestamp = Math.max(timestamp, currentMaxTimestamp);
return timestamp;
} @Override
public Watermark getCurrentWatermark() {
// return the watermark as current highest timestamp minus the out-of-orderness bound
return new Watermark(currentMaxTimestamp - maxOutOfOrderness);
}
} /**
* This generator generates watermarks that are lagging behind processing time by a fixed amount.
* It assumes that elements arrive in Flink after a bounded delay.
*/
public class TimeLagWatermarkGenerator extends AssignerWithPeriodicWatermarks<MyEvent> { private final long maxTimeLag = 5000; // 5 seconds @Override
public long extractTimestamp(MyEvent element, long previousElementTimestamp) {
return element.getCreationTime();
} @Override
public Watermark getCurrentWatermark() {
// return the watermark as current time minus the maximum time lag
return new Watermark(System.currentTimeMillis() - maxTimeLag);
}
}
Punctuated Watermarks
public class PunctuatedAssigner extends AssignerWithPunctuatedWatermarks<MyEvent> {
@Override
public long extractTimestamp(MyEvent element, long previousElementTimestamp) {
return element.getCreationTime();
}
@Override
public Watermark checkAndGetNextWatermark(MyEvent lastElement, long extractedTimestamp) {
return lastElement.hasWatermarkMarker() ? new Watermark(extractedTimestamp) : null;
}
}
三代码调试
public class WindowWaterMark {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String hostName = "localhost";
Integer port = Integer.parseInt("8001");
// set up the execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment
.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
env.setParallelism(2);
env.getConfig().setAutoWatermarkInterval(9000);
// get input data
DataStream<String> text = env.socketTextStream(hostName, port);
DataStream<Tuple3<String, Long, Integer>> counts =
text.filter(new FilterClass()).map(new LineSplitter())
.assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks<Tuple3<String, Long, Integer>>(){
private long currentMaxTimestamp = 0l;
private final long maxOutOfOrderness = 10000l;
@Override
public long extractTimestamp(Tuple3<String, Long, Integer> element,
long previousElementTimestamp) {
// TODO Auto-generated method stub
long timestamp= element.f1;
currentMaxTimestamp = Math.max(timestamp, currentMaxTimestamp);
System.out.println("get timestamp is "+timestamp+" currentMaxTimestamp "+currentMaxTimestamp);
return timestamp;
}
@Override
public Watermark getCurrentWatermark() {
// TODO Auto-generated method stub
System.out.println("wall clock is "+ System.currentTimeMillis() +" new watermark "+(currentMaxTimestamp - maxOutOfOrderness));
return new Watermark(currentMaxTimestamp - maxOutOfOrderness);
}
})
.keyBy(0)
.timeWindow(Time.seconds(20))
// .allowedLateness(Time.seconds(10))
.sum(2);
counts.print();
// execute program
env.execute("Java WordCount from SocketTextStream Example");
}
public static final class LineSplitter implements
MapFunction<String, Tuple3<String, Long, Integer>> {
@Override
public Tuple3<String, Long, Integer> map(String value) throws Exception {
// TODO Auto-generated method stub
String[] tokens = value.toLowerCase().split("\\W+");
long eventtime = Long.parseLong(tokens[1]);
return new Tuple3<String, Long, Integer>(tokens[0], eventtime, 1);
}
}
private static class MyTimestamp extends
AscendingTimestampExtractor<Tuple3<String, Long, Integer>> {
private static final long serialVersionUID = 1L;
@Override
public long extractAscendingTimestamp(
Tuple3<String, Long, Integer> element) {
// TODO Auto-generated method stub
return element.f1;
}
}
public static final class FilterClass implements FilterFunction<String>
{
@Override
public boolean filter(String value) throws Exception {
// TODO Auto-generated method stub
if(StringUtils.isNullOrWhitespaceOnly(value))
{
return false;
}
else
{
return true;
}
}
}
}
测试代码如上,注意这段代码手动更改了autowatermarkinterval的时间为9s以便于观察方法调用顺序。
首先启动job不输入数据,30s后日志输出为
wall clock is 1506680562679 new watermark -10000
wall clock is 1506680562679 new watermark -10000
wall clock is 1506680571683 new watermark -10000
wall clock is 1506680571683 new watermark -10000
wall clock is 1506680580687 new watermark -10000
wall clock is 1506680580687 new watermark -10000
.........................
这说明在没有数据输入的情况下PeriodicWatermarks仍然会周期性调用getCurrentWatermark这个方法,每次有两条相同wall clock的日志跟程序里env.setParallelism(2)这个参数相同,表明watermark与operator的并发一致。
输入数据aaaa 1506590035000
日志输出为
wall clock is 1506681868124 new watermark -10000
wall clock is 1506681877129 new watermark -10000
wall clock is 1506681877129 new watermark -10000
get timestamp is 1506590035000 currentMaxTimestamp 1506590035000
wall clock is 1506681886132 new watermark 1506590025000
wall clock is 1506681886132 new watermark -10000
wall clock is 1506681895136 new watermark -10000
wall clock is 1506681895136 new watermark 1506590025000
...........................................
上述日志表明接收到消息后extractTimestamp这个方法会被立即调用,但是同时注意到wall clock日志的打印时间完全没有受到数据流入的影响,所以在PeriodicWatermarks这个是线下,watermark的产生时间和速率与数据流的输入无关。
需要说明的是,时间窗口的起始时间计算方法为
public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
return timestamp - (timestamp - offset + windowSize) % windowSize;
}
所有对于上述测试代码里时间长度为20s的滚动窗口,默认下,在每分钟内窗口的起止时间都是(0~20)(20~40)(40~60)这样,我们的第一条数据aaaa 1506590035000对应时间为2017/9/28 17:13:55,所以它将会在(2017/9/28 17:13:40~2017/9/28 17:14:00)这个窗口完成计算
继续输入数据
cc 1506590035000
cc 1506590035000
bb 1506590035000
aaaa 1506590035000
bb 1506590035000
aaaa 1506590041000 //上调数据的eventTime至2017/9/28 17:14:01,超过前一个window的结束时间
bb 1506590041000
cc 1506590041000
日志输出为
get timestamp is 1506590041000 currentMaxTimestamp 1506590041000
wall clock is 1507522499419 new watermark 1506590031000
wall clock is 1507522499424 new watermark 1506590025000
wall clock is 1507522508422 new watermark 1506590031000
wall clock is 1507522508429 new watermark 1506590025000
wall clock is 1507522517426 new watermark 1506590031000
wall clock is 1507522517434 new watermark 1506590025000
get timestamp is 1506590041000 currentMaxTimestamp 1506590041000
wall clock is 1507522526429 new watermark 1506590031000
wall clock is 1507522526435 new watermark 1506590031000
wall clock is 1507522535431 new watermark 1506590031000
wall clock is 1507522535440 new watermark 1506590031000
wall clock is 1507522544433 new watermark 1506590031000
可以看到虽然后来的数据已经超过了第一个window的endtime,但是由于getCurrentWatermark方法的设定系统目前的watermark为2017/9/28 17:13:51小于endtime,所以flink并不会立即执行整个窗口的运算
继续增加数据和eventtime
aaaa 1506590051000
bb 1506590051000
cc 1506590051000
日志输出如下
get timestamp is 1506590051000 currentMaxTimestamp 1506590051000
get timestamp is 1506590051000 currentMaxTimestamp 1506590051000
get timestamp is 1506590051000 currentMaxTimestamp 1506590051000
wall clock is 1507522589449 new watermark 1506590041000
wall clock is 1507522589461 new watermark 1506590041000
1> (aaaa,1506590035000,2)
2> (cc,1506590035000,2)
2> (bb,1506590035000,2)
这个时候watermark刚好大于了第一个window的endtime,整个(2017/9/28 17:13:40~2017/9/28 17:14:00)窗口对应的数据开始执行计算,输出对应结果。
参考文档
1 http://vishnuviswanath.com/flink_eventtime.html
2 https://data-artisans.com/blog/how-apache-flink-enables-new-streaming-applications-part-1
3 https://www.youtube.com/watch?v=3UfZN59Nsk8
4 Flink流计算编程--watermark(水位线)简介
flink watermark介绍的更多相关文章
- [源码分析] 从源码入手看 Flink Watermark 之传播过程
[源码分析] 从源码入手看 Flink Watermark 之传播过程 0x00 摘要 本文将通过源码分析,带领大家熟悉Flink Watermark 之传播过程,顺便也可以对Flink整体逻辑有一个 ...
- 《从0到1学习Flink》—— 介绍Flink中的Stream Windows
前言 目前有许多数据分析的场景从批处理到流处理的演变, 虽然可以将批处理作为流处理的特殊情况来处理,但是分析无穷集的流数据通常需要思维方式的转变并且具有其自己的术语(例如,"windowin ...
- Apache Flink 整体介绍
前言 Flink 是一种流式计算框架,为什么我会接触到 Flink 呢?因为我目前在负责的是监控平台的告警部分,负责采集到的监控数据会直接往 kafka 里塞,然后告警这边需要从 kafka topi ...
- Flink窗口介绍及应用
Windows是Flink流计算的核心,本文将概括的介绍几种窗口的概念,重点只放在窗口的应用上. 本实验的数据采用自拟电影评分数据(userId, movieId, rating, timestamp ...
- Flink - watermark生成
参考,Flink - Generating Timestamps / Watermarks watermark,只有在有window的情况下才用到,所以在window operator前加上assig ...
- flink架构介绍
前言 flink作为基于流的大数据计算引擎,可以说在大数据领域的红人,下面对flink-1.7的架构进行逻辑上的分析并和spark做了一些关键点的对比. 架构 如图1,flink架构分为3个部分,cl ...
- Flink入门(二)——Flink架构介绍
1.基本组件栈 了解Spark的朋友会发现Flink的架构和Spark是非常类似的,在整个软件架构体系中,同样遵循着分层的架构设计理念,在降低系统耦合度的同时,也为上层用户构建Flink应用提供了丰富 ...
- [Flink原理介绍第四篇】:Flink的Checkpoint和Savepoint介绍
原文:https://blog.csdn.net/hxcaifly/article/details/84673292 https://blog.csdn.net/zero__007/article/d ...
- flink原理介绍-数据流编程模型v1.4
数据流编程模型 抽象级别 程序和数据流 并行数据流 窗口 时间 有状态操作 检查点(checkpoint)容错 批量流处理 下一步 抽象级别 flink针对 流式/批处理 应用提供了不同的抽象级别. ...
随机推荐
- wepy - 与原生有什么不同(watcher监听器.)
<style> </style> <template> <view>监听值:{{num}}</view> </template> ...
- wepy - 与原生有什么不同(slot插槽)
wepy官方文档是这样介绍的 简单描述就是: index.wpy:子组件 panel.wpy:父组件 1.slot是内容分发的占位符 2.slot父组件在子组件使用时,名称必须一致 3.slot子组件 ...
- 应用程序正常初始化(0xc0000135)失败的解决方法
转自:http://blog.sina.com.cn/s/blog_64fba4e00100mzf9.html 这是由于没有安装.NET framework 所造成的,请安装.NET framewor ...
- git删除本地的资源,如何恢复?
1.$ git reset --hard HEAD 将提交重置 2.使用 $ git checkout TestTimer.java(文件名) 恢复过来了
- Linux系统Domino704升级为901 64位的步骤及注意事项
[背景] 随便系统业务量的不断增大,应用数据库越来越多.与第三方接口的需求越来越多.文档量越来越多,32位的domino对server的利用率已无法满足系统需求的日益增长,低版本号的domino ...
- SXi5.5不识别硬件驱动的光盘定制
SXi5.5不识别硬件驱动的光盘定制~~RealTek8111E,Intel217V,Z97 AHCI [复制链接] gmx168 电梯直达 1# 发表于 2014-9-23 17:06 ...
- 深入PHP内核之opcode handler
1.opcode结构 在Zend/zend_compile.h文件下 struct _zend_op { opcode_handler_t handler; znode_op op1; znode_o ...
- &&和;和||符号的意思
http://www.cnblogs.com/xuxm2007/archive/2011/01/16/1936836.html在命令行可以一次执行多个命令,有以下几种: 1.每个命令之间用;隔开 ...
- jQuery+ajax中,让window.open不被拦截(转)
方法1:<input type="button" class="preview" value="预览"/>$('.preview ...
- C 应用
前言 1)操作符两端必须加空格,(每行第一个赋值语句对齐). 2)变量名必须是英文(不能是拼音):英文.数字.下划线和美元符号. 3)等于号 == 反过来写(0 == i%4)防止少些赋值号的错误. ...