基于时间的滑动窗口,以汇总过去一分钟的秒数平均消息数

时间:2018-06-19 16:14:22

标签: java apache-storm moving-average sliding-window

我想使用Apache Storm内置的滑动窗口功能(具有60秒的长度和5秒的滑动间隔)来计算卡夫卡在过去一分钟内每秒消耗的平均味精率。

public class SlidingWindowBolt extends BaseWindowedBolt {
private static final Logger LOG = Logger.getLogger(SlidingWindowBolt.class);

private OutputCollector collector;

@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    this.collector = collector;
}

//executed every 5 sec(sliding interval)
@Override
public void execute(TupleWindow inputWindow) {

    List<Tuple> tuplesInWindow = inputWindow.get();
    List<Tuple> newTuples = inputWindow.getNew();
    List<Tuple> expiredTuples = inputWindow.getExpired();
    LOG.debug("ALL EVENTS " + tuplesInWindow.stream().map(t -> t.getValue(4).toString()).collect(Collectors.joining(",")));
    LOG.debug("EXPIRED EVENTS " + expiredTuples.stream().map(t -> t.getValue(4).toString()).collect(Collectors.joining(",")));
    LOG.debug("NEWLY ARRIVED EVENTS " + newTuples.stream().map(t -> t.getValue(4).toString()).collect(Collectors.joining(",")));

    // divide msg counts on the window size (60sec)
    double avgCountPerSec = tuplesInWindow.get().size()/60d;
    LOG.warn("AVG MSG PER SEC: " + new BigDecimal(avgCountPerSec).setScale(2, RoundingMode.HALF_UP).doubleValue());

    collector.emit(new Values(avgCountPerSec));
}

@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
    declarer.declare(new Fields("avg"));
}
}

如您所见,我将事件计数在60秒的窗口内,然后将其除以60秒即可获得味精/秒值。我做对了吗?

0 个答案:

没有答案