    001    /*
    002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
    003     *
    004     * Project and contact information: http://www.cascading.org/
    005     *
    006     * This file is part of the Cascading project.
    007     *
    008     * Licensed under the Apache License, Version 2.0 (the "License");
    009     * you may not use this file except in compliance with the License.
    010     * You may obtain a copy of the License at
    011     *
    012     *     http://www.apache.org/licenses/LICENSE-2.0
    013     *
    014     * Unless required by applicable law or agreed to in writing, software
    015     * distributed under the License is distributed on an "AS IS" BASIS,
    016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    017     * See the License for the specific language governing permissions and
    018     * limitations under the License.
    019     */
    020
    021    package cascading.flow.hadoop.stream;
    022
    023    import java.util.IdentityHashMap;
    024    import java.util.Map;
    025
    026    import cascading.CascadingException;
    027    import cascading.flow.FlowProcess;
    028    import cascading.flow.SliceCounters;
    029    import cascading.flow.hadoop.HadoopCoGroupClosure;
    030    import cascading.flow.stream.Duct;
    031    import cascading.flow.stream.DuctException;
    032    import cascading.flow.stream.SpliceGate;
    033    import cascading.flow.stream.StreamGraph;
    034    import cascading.pipe.CoGroup;
    035    import cascading.tuple.Tuple;
    036    import cascading.tuple.TupleEntry;
    037    import cascading.tuple.io.IndexTuple;
    038    import cascading.tuple.io.TuplePair;
    039
    040    /**
    041     *
    042     */
    043    public class HadoopCoGroupGate extends HadoopGroupGate
    044      {
    045      private final Map<Duct, Integer> posMap = new IdentityHashMap<Duct, Integer>();
    046
    047      public HadoopCoGroupGate( FlowProcess flowProcess, CoGroup coGroup, SpliceGate.Role role )
    048        {
    049        super( flowProcess, coGroup, role );
    050        }
    051
    052      @Override
    053      public void bind( StreamGraph streamGraph )
    054        {
    055        super.bind( streamGraph );
    056
    057        if( role == Role.sink )
    058          orderDucts( streamGraph );
    059        }
    060
    061      @Override
    062      public void prepare()
    063        {
    064        super.prepare();
    065
    066        if( role != Role.sink )
    067          closure = new HadoopCoGroupClosure( flowProcess, splice.getNumSelfJoins(), keyFields, valuesFields );
    068        else
    069          makePosMap( posMap );
    070
    071        if( grouping != null && splice.getJoinDeclaredFields() != null && splice.getJoinDeclaredFields().isNone() )
    072          grouping.joinerClosure = closure;
    073        }
    074
    075      @Override
    076      public void receive( Duct previous, TupleEntry incomingEntry )
    077        {
    078        Integer pos = posMap.get( previous );
    079
    080        Tuple groupTuple = keyBuilder[ pos ].makeResult( incomingEntry.getTuple(), null );
    081        Tuple sortTuple = sortFields == null ? null : sortBuilder[ pos ].makeResult( incomingEntry.getTuple(), null );
    082        Tuple valuesTuple = valuesBuilder[ pos ].makeResult( incomingEntry.getTuple(), null );
    083
    084        Tuple groupKey = sortTuple == null ? groupTuple : new TuplePair( groupTuple, sortTuple );
    085
    086        try
    087          {
    088          collector.collect( new IndexTuple( pos, groupKey ), new IndexTuple( pos, valuesTuple ) );
    089          flowProcess.increment( SliceCounters.Tuples_Written, 1 );
    090          }
    091        catch( OutOfMemoryError error )
    092          {
    093          handleReThrowableException( "out of memory, try increasing task memory allocation", error );
    094          }
    095        catch( CascadingException exception )
    096          {
    097          handleException( exception, incomingEntry );
    098          }
    099        catch( Throwable throwable )
    100          {
    101          handleException( new DuctException( "internal error: " + incomingEntry.getTuple().print(), throwable ), incomingEntry );
    102          }
    103        }
    104
    105      @Override
    106      protected Tuple unwrapGrouping( Tuple key )
    107        {
    108        return ( (IndexTuple) key ).getTuple();
    109        }
    110      }