com.datatorrent.lib.multiwindow.MultiWindowSumKeyVal Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.datatorrent.lib.multiwindow;
import java.util.Map;
import javax.validation.constraints.Min;
import com.datatorrent.api.annotation.OperatorAnnotation;
import com.datatorrent.lib.math.SumKeyVal;
import com.datatorrent.lib.util.KeyValPair;
/**
* A sum operator of KeyValPair schema which accumulates sum across multiple
* streaming windows.
*
* This is an end window operator which emits only at Nth window.
*
* StateFull : Yes, sum is computed across streaming windows.
* Partitions : No, sum is not unified at output port.
*
* Ports:
* data: expects KeyValPair<K,V extends Number>
* sum: emits KeyValPair<K,V>
* count: emits KeyValPair<K,Integer>
*
* Properties:
* inverse: If set to true the key in the filter will block tuple.
* filterBy: List of keys to filter on.
* windowSize i.e. N: Number of streaming windows that define application
* window.
*
* @displayName Multi Window Sum Key Value
* @category Stats and Aggregations
* @tags key value, sum, numeric
* @since 0.3.3
*/
@OperatorAnnotation(partitionable = false)
public class MultiWindowSumKeyVal extends SumKeyVal
{
/**
* Number of streaming window after which tuple got emitted.
*/
@Min(2)
private int windowSize = 2;
private long windowCount = 0;
public void setWindowSize(int windowSize)
{
this.windowSize = windowSize;
}
/**
* Emit only at the end of windowSize window boundary.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void endWindow()
{
boolean emit = (++windowCount) % windowSize == 0;
if (!emit) {
return;
}
// Emit only at the end of application window boundary.
boolean dosum = sum.isConnected();
if (dosum) {
for (Map.Entry e : sums.entrySet()) {
K key = e.getKey();
if (dosum) {
sum.emit(new KeyValPair(key, getValue(e.getValue().sum.doubleValue())));
}
}
}
// Clear cumulative sum at the end of application window boundary.
sums.clear();
}
}