com.datatorrent.lib.algo.FirstN 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.algo;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.mutable.MutableInt;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.OperatorAnnotation;
import com.datatorrent.lib.util.AbstractBaseNOperatorMap;
/**
* This operator filters the incoming stream of key value pairs by emitting the first N key value pairs with a specified key in each window.
*
* Emits first N tuples of a particular key.
*
*
* This module is a pass through module
*
* StateFull : Yes, tuple are compare across application window(s).
* Partitions : No, will yield wrong results.
*
* Ports:
* data: Input data port expects HashMap<K,V>
* bottom: Output data port, emits HashMap<K,V>
*
* Properties:
* N: The number of top values to be emitted per key
*
* Specific compile time checks are:
* N: Has to be >= 1
*
*
*
*
* @displayName First N Keyval Pairs Matching Key
* @category Rules and Alerts
* @tags filter, key value
*
* @since 0.3.2
*/
@OperatorAnnotation(partitionable = false)
public class FirstN extends AbstractBaseNOperatorMap
{
/**
* key count map.
*/
HashMap keycount = new HashMap();
/**
* Inserts tuples into the queue
* @param tuple to insert in the queue
*/
@Override
public void processTuple(Map tuple)
{
for (Map.Entry e: tuple.entrySet()) {
MutableInt count = keycount.get(e.getKey());
if (count == null) {
count = new MutableInt(0);
keycount.put(e.getKey(), count);
}
count.increment();
if (count.intValue() <= getN()) {
first.emit(cloneTuple(e.getKey(), e.getValue()));
}
}
}
/**
* The output port on which the first N key value pairs are emitted.
*/
public final transient DefaultOutputPort> first = new DefaultOutputPort>();
/**
* Clears the cache to start anew in a new window
*/
@Override
public void endWindow()
{
keycount.clear();
}
/**
* First N number of KeyValue pairs for each Key.
*
* @param val
*/
public void setN(int val)
{
super.setN(val);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy