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

com.datatorrent.lib.math.ChangeKeyVal 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.math;

import java.util.HashMap;

import org.apache.commons.lang.mutable.MutableDouble;

import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;

import com.datatorrent.lib.util.BaseNumberKeyValueOperator;
import com.datatorrent.lib.util.KeyValPair;

/**
 * Operator compares <key,value> pairs arriving at data and base input ports and stores <key,value> pairs arriving at base port in hash map across the windows.
 * 

* The <key,value> pairs that arrive at data port are compared with base value if the key exists in the hash map.  * Change value and percentage are emitted on separate ports. * StateFull : Yes, base map values are stored across windows.
* Partitions : Yes, values on the base port are replicated across all partitions. However the order of tuples on the * output stream may change. *
* Ports:
* data: expects KeyValPair<K,V extends Number>
* base: expects KeyValPair<K,V extends Number>
* change: emits KeyValPair<K,V>(1)
* percent: emits KeyValPair<K,Double>(1)
*
*
* Properties:
* inverse: if set to true the key in the filter will block tuple
* filterBy: List of keys to filter on
* * @displayName Change Key Value * @category Math * @tags change, key value * @since 0.3.3 */ public class ChangeKeyVal extends BaseNumberKeyValueOperator { /** * basemap is a stateful field. It is retained across windows */ private HashMap basemap = new HashMap(); /** * Input data port that takes key value pairs. */ public final transient DefaultInputPort> data = new DefaultInputPort>() { /** * Process each key, compute change or percent, and emit it. */ @Override public void process(KeyValPair tuple) { K key = tuple.getKey(); if (!doprocessKey(key)) { return; } MutableDouble bval = basemap.get(key); if (bval != null) { // Only process keys that are in the basemap double cval = tuple.getValue().doubleValue() - bval.doubleValue(); change.emit(new KeyValPair(cloneKey(key), getValue(cval))); percent.emit(new KeyValPair(cloneKey(key), (cval / bval.doubleValue()) * 100)); } } }; /** * Base value input port, stored in base map for comparison. */ public final transient DefaultInputPort> base = new DefaultInputPort>() { /** * Process each key to store the value. If same key appears again update * with latest value. */ @Override public void process(KeyValPair tuple) { if (tuple.getValue().doubleValue() != 0.0) { // Avoid divide by zero, Emit // an error tuple? MutableDouble val = basemap.get(tuple.getKey()); if (val == null) { val = new MutableDouble(0.0); basemap.put(cloneKey(tuple.getKey()), val); } val.setValue(tuple.getValue().doubleValue()); } } }; /** * Key, Change output port. */ @OutputPortFieldAnnotation(optional = true) public final transient DefaultOutputPort> change = new DefaultOutputPort>(); /** * Key, Percentage Change pair output port. */ @OutputPortFieldAnnotation(optional = true) public final transient DefaultOutputPort> percent = new DefaultOutputPort>(); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy