Coverage Report - datafu.pig.bags.BagConcat
 
Classes in this File Line Coverage Branch Coverage Complexity
BagConcat
70%
12/17
83%
5/6
5.5
 
 1  
 /*
 2  
  * Copyright 2010 LinkedIn, Inc
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5  
  * use this file except in compliance with the License. You may obtain a copy of
 6  
  * the License at
 7  
  * 
 8  
  * http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12  
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13  
  * License for the specific language governing permissions and limitations under
 14  
  * the License.
 15  
  */
 16  
 
 17  
 package datafu.pig.bags;
 18  
 
 19  
 import java.io.IOException;
 20  
 
 21  
 import org.apache.pig.EvalFunc;
 22  
 import org.apache.pig.data.BagFactory;
 23  
 import org.apache.pig.data.DataBag;
 24  
 import org.apache.pig.data.DataType;
 25  
 import org.apache.pig.data.Tuple;
 26  
 import org.apache.pig.data.TupleFactory;
 27  
 import org.apache.pig.impl.logicalLayer.schema.Schema;
 28  
 
 29  
 /**
 30  
  * Concatenates the tuples from a set of bags, producing a single bag containing all tuples.
 31  
  * <p>
 32  
  * Example:
 33  
  * <pre>
 34  
  * {@code
 35  
  * define BagConcat datafu.pig.bags.BagConcat();
 36  
  * 
 37  
  * -- input:
 38  
  * -- ({(1),(2),(3)},{(3),(4),(5)})
 39  
  * -- ({(20),(25)},{(40),(50)})
 40  
  * input = LOAD 'input' AS (A: bag{T: tuple(v:INT)}, B: bag{T: tuple(v:INT)});
 41  
  * 
 42  
  * -- output:
 43  
  * -- ({(1),(2),(3),(3),(4),(5)})
 44  
  * -- ({(20),(25),(40),(50)})
 45  
  * output = FOREACH input GENERATE BagConcat(A,B); 
 46  
  * }
 47  
  * </pre>
 48  
  */
 49  1264
 public class BagConcat extends EvalFunc<DataBag>
 50  
 {
 51  1
   private static final BagFactory bagFactory = BagFactory.getInstance();
 52  1
   private static final TupleFactory tupleFactory = TupleFactory.getInstance();
 53  
 
 54  
   @Override
 55  
   public DataBag exec(Tuple input) throws IOException
 56  
   {
 57  2
     DataBag outputBag = bagFactory.newDefaultBag();
 58  
 
 59  
     try {
 60  8
       for (int i=0; i < input.size(); i++) {
 61  6
         Object o = input.get(i);
 62  6
         if (!(o instanceof DataBag))
 63  0
           throw new RuntimeException("parameters must be databags");
 64  
 
 65  6
         DataBag inputBag = (DataBag) o;
 66  6
         for (Tuple elem : inputBag) 
 67  14
           outputBag.add(elem);
 68  
       }
 69  
 
 70  2
       return outputBag;
 71  
     }
 72  0
     catch (Exception e) {
 73  0
       throw new IOException(e);
 74  
     }
 75  
   }
 76  
 
 77  
   @Override
 78  
   public Schema outputSchema(Schema input)
 79  
   {
 80  
     try {
 81  275
       return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input),
 82  
                           input.getField(0).schema, DataType.BAG));
 83  
     }
 84  0
     catch (Exception e) {
 85  0
       return null;
 86  
     }
 87  
   }
 88  
 }
 89