001/* An operator for cogroup.
002 * 
003 * Copyright (c) 2014 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2015-11-05 00:27:41 +0000 (Thu, 05 Nov 2015) $' 
008 * '$Revision: 34216 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.spark.operator;
031
032import java.util.Iterator;
033
034import org.apache.spark.api.java.JavaPairRDD;
035import org.apache.spark.api.java.function.PairFlatMapFunction;
036
037import scala.Tuple2;
038
039/** An operator for cogroup.
040 * 
041 *  @author Daniel Crawl
042 *  @version $Id: CoGroupOperator.java 34216 2015-11-05 00:27:41Z crawl $
043 */
044public class CoGroupOperator extends Operator {
045
046    /** Create a new CoGroupOperator.
047     *  @param stub the stub class to run in the cogroup operator
048     *  @param name the operator name
049     */
050    public CoGroupOperator(PairFlatMapFunction<Iterator<Tuple2<?,Tuple2<Iterable<?>,Iterable<?>>>>,?,?> stub, String name) {
051        super(2, stub, name);
052    }
053
054    /** Execute the operator. */
055    @Override
056    public JavaPairRDD<Object, ?> execute() {
057        if(_numInstances < 1) {
058            return _inputData[0].cogroup(_inputData[1]).mapPartitionsToPair((PairFlatMapFunction)_stub);
059        } else {
060            return _inputData[0].cogroup(_inputData[1], _numInstances).mapPartitionsToPair((PairFlatMapFunction)_stub);
061        }
062    }
063
064}