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

com.datatorrent.lib.testbench.CountOccurance 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.testbench;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.datatorrent.api.Context.OperatorContext;
import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.common.util.BaseOperator;

/**
 * 

A base implementation of an operator which does count occurrence.

*

* @displayName Count Occurrence * @category Test Bench * @tags count * @since 0.3.2 */ public class CountOccurance extends BaseOperator { private Map collect; public final transient DefaultInputPort inport = new DefaultInputPort() { @Override public void process(k s) { if (collect.containsKey(s)) { Integer value = (Integer)collect.remove(s); collect.put(s, new Integer(value + 1)); } else { collect.put(s, new Integer(1)); } } }; @Override public void setup(OperatorContext context) { } @Override public void teardown() { } @Override public void beginWindow(long windowId) { collect = new HashMap(); } /** * Output port that emits a map of integer values. */ public final transient DefaultOutputPort> outport = new DefaultOutputPort>(); /** * Output dimensions port that emits a map of <string,object> values. */ public final transient DefaultOutputPort> dimensionOut = new DefaultOutputPort>(); /** * Output total port that emits a map of <string,integer> count values. */ public final transient DefaultOutputPort> total = new DefaultOutputPort>(); @Override public void endWindow() { outport.emit(collect); long timestamp = new Date().getTime(); int allcount = 0; for (Map.Entry entry : collect.entrySet()) { Map map = new HashMap(); map.put("timestamp", timestamp); map.put("item", entry.getKey()); map.put("view", entry.getValue()); dimensionOut.emit(map); allcount += entry.getValue(); } Map map = new HashMap(); map.put("total", new Integer(allcount)); total.emit(map); collect = null; collect = new HashMap(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy