All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datatorrent.lib.algo.TopN 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.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.Operator.Unifier;
import com.datatorrent.api.annotation.OperatorAnnotation;

import com.datatorrent.lib.util.AbstractBaseNNonUniqueOperatorMap;

/**
 * This operator orders tuples per key and emits the top N tuples per key at the end of the window.
 * 

* Orders tuples per key and emits top N tuples per key on end of window. *

*

* This is an end of window module.
*
* StateFull : Yes, Tuple are aggregated across application window(s).
* Partitions : Yes, Top values are unified on output port.
* Ports:
* data: Input data port expects HashMap<K,V>
* top: Output data port, emits HashMap<K, ArrayList<V>>
*
* Properties:
* N: The number of top values to be emitted per key
*
* Specific compile time checks are:
* N: Has to be >= 1
*
*

* * @displayName Top N Values Per Key * @category Stats and Aggregations * @tags filter, rank * * @since 0.3.3 */ @OperatorAnnotation(partitionable = true) public class TopN extends AbstractBaseNNonUniqueOperatorMap implements Unifier>> { /** * The output port which emits the top N values per key. */ public final transient DefaultOutputPort>> top = new DefaultOutputPort>>() { @Override public Unifier>> getUnifier() { TopN unifier = new TopN(); unifier.setN(getN()); return unifier; } }; /** * returns true * @return true */ @Override public boolean isAscending() { return true; } /** * Emits tuple on port "top" */ @Override public void emit(HashMap> tuple) { top.emit(tuple); } @Override public void process(HashMap> tuple) { for (Map.Entry> entry : tuple.entrySet()) { for (V value : entry.getValue()) { HashMap item = new HashMap(); item.put(entry.getKey(), value); this.processTuple(item); } } } /** * Top N tuples per key * @param val */ @Override public void setN(int val) { super.setN(val); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy