Skip to content

Instantly share code, notes, and snippets.

@tf0054
Created March 22, 2012 14:54
Show Gist options
  • Save tf0054/2158792 to your computer and use it in GitHub Desktop.
Save tf0054/2158792 to your computer and use it in GitHub Desktop.
Hack#48
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