storm-窗口

1. 简介

Storm可同时处理窗口内的所有tuple。窗口可以从时间或数量上来划分,由如下两个因素决定:

  1. 窗口的长度,可以是时间或Tuple数量;
  2. 滑动间隔(sliding Interval),同样也可以是时间或Tuple数量,后面Sliding Window中介绍;

比如每隔一段时间(10s)统计最近30s的请求量或者异常次数。

2. Window

2.1 Tumbling Window

按照固定的时间间隔或者Tuple数量划分窗口。下面例子就是5秒砍一个窗口:

| e1 e2 | e3 e4 e5 e6 | e7 e8 e9 |...
0       5             10         15    -> time
   w1         w2            w3

2.2 Sliding Window

滑动窗口,这个还是上图比较清晰。假设10s一个窗口,滑动5s(每5秒统计最近10秒的数据,数据会有重叠)。效果就这样了:

| e1 e2 | e3 e4 e5 e6 | e7 e8 e9 |...
0       5             10         15    -> time

|<------- w1 -------->|
        |------------ w2 ------->|

可以看到窗口数据是有重叠的。

2.3 参数

参数设置的几个方法(时间和数量的排列组合):

withWindow(Count windowLength, Count slidingInterval)

  每收到slidingInterval条数据统计最近的windowLength条数据。

withWindow(Count windowLength)

  每收到1条数据统计最近的windowLength条数据。

withWindow(Count windowLength, Duration slidingInterval)

  每过slidingInterval秒统计最近的windowLength条数据。

withWindow(Duration windowLength, Count slidingInterval)

  每收到slidingInterval条数据统计最近的windowLength秒的数据。

withWindow(Duration windowLength, Duration slidingInterval)

  每过slidingInterval秒统计最近的windowLength秒的数据。

public withWindow(Duration windowLength)

  每收到1条数据统计最近的windowLength秒的数据。

2.4 Tuple时间和乱序

2.4.1 Event time 和Process time

  1. Event time,事件时间,通常这个时间会带在Tuple中;
  2. Process time,到某一个处理环节的时间。

举例:A今天早上9点告诉B,说C昨天晚上9点在滨江国际;

这条信息中,可以认为C在滨江国际的Event time是昨天晚上9点,B接收到这条信息的时间,即Process time,是今天早上9点。

2.4.2 配置时间戳字段(timestamp field)

windows按照时间划分时,默认是Process time,也可以指定为Tuple中的Event time。

如果以Event time来划分窗口:
1. Tuple落入到哪个窗口,是看tuple里的Event time。
2. 窗口向后推进,主要依靠Event time的增长;

/**
* Specify a field in the tuple that represents the timestamp as a long value. If this
* field is not present in the incoming tuple, an {@link IllegalArgumentException} will be thrown.
*
* @param fieldName the name of the field that contains the timestamp
*/
public BaseWindowedBolt withTimestampField(String fieldName)

2.4.3 延时(lag)和水位线(watermark)

这两个建议不要分开理解,只有lag加watermark才能理解怎么处理乱序的。

假设数据乱序的情况下,跑最偏的数据时间相差为60s, 比如一个06:00:00的数据,就跑到06:01:00前面待着了。可以根据配置一个经验值差值lag,此种情况配置为60s.

具体怎么处理这种现象呢。

  1. 从当前最后一条数据算起,往前减去lag,得到一个时间,这个时间成为watermark;
  2. 认为watermark之前的数据都已经到了。收到06:01:00的数据时,认为06:00:00的数据都到了。给他们入window。

这样实际是一个延时处理,等到了06:01:00时,我才开始将06:00:00的数据放入窗口。

如果很不巧,06:00:00的数据在06:01:00之后,lag为60s,不好意思,进不了窗口。此数据不会被处理,并且会在worker的日志中加一行INFO信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值