Created
March 22, 2012 14:54
-
-
Save tf0054/2158792 to your computer and use it in GitHub Desktop.
Hack#48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.hadoophacks.pig; | |
| import org.apache.pig.EvalFunc; | |
| import org.apache.pig.data.Tuple; | |
| import org.apache.pig.backend.executionengine.ExecException; | |
| import java.io.IOException; | |
| import org.apache.pig.data.TupleFactory; | |
| import org.apache.pig.data.DataType; | |
| import org.apache.pig.impl.logicalLayer.schema.Schema; | |
| public class ToTuple extends EvalFunc<Tuple>{ | |
| private TupleFactory tupleFactory = TupleFactory.getInstance(); | |
| public Tuple exec(Tuple input)throws IOException{ | |
| if(input == null || input.size() == 0){ | |
| return null; | |
| } | |
| try{ | |
| int size = input.size(); | |
| Tuple output = tupleFactory.newTuple(size); | |
| for(int i = 0;i < size;i++){ | |
| output.set(i , input.get(i)); | |
| } | |
| return output; | |
| } | |
| catch(ExecException e){ | |
| throw new IOException(e); | |
| } | |
| } | |
| public Schema outputSchema(Schema input){ | |
| try{ | |
| Schema tupleSchema = new Schema(); | |
| int numFields = input.size(); | |
| for(int i = 0;i < numFields;i++){ | |
| tupleSchema.add(input.getField(i)); | |
| } | |
| return new Schema(new Schema.FieldSchema("col0" , tupleSchema , DataType.TUPLE)); | |
| }catch(Exception e){ | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment