Chethan,
not quite sure what error you're getting. I implemented the following UDF
and saw no issue:
package org.apache.pig;
import java.io.IOException;
import org.apache.pig.impl.logicalLayer.schema.Schema;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
public class Test extends EvalFunc<Tuple> {
TupleFactory mTupleFactory = TupleFactory.getInstance();
public Tuple exec(Tuple input) throws IOException {
Tuple t = mTupleFactory.newTuple(5);
for (int i = 0; i < 5; i++) {
t.set(i,input.get(4-i));
}
return t;
}
public Schema outputSchema(Schema input) {
try {
Schema tupleSchema = new Schema();
tupleSchema.add(input.getField(4));
tupleSchema.add(input.getField(3));
tupleSchema.add(input.getField(2));
tupleSchema.add(input.getField(1));
tupleSchema.add(input.getField(0));
return new Schema(new
Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(),input),tupleSchema,
DataType.TUPLE));
} catch (Exception e) {
return null;
}
}
}
2012/2/29 chethan <[email protected]>
> Hi,
>
> I have written a UDF which will return Tuple and i am giving Tuple to
> another command, but that function expects schema for that Tuple.
>
> i have got the sample but schema is not defining, below is the sample.
>
> public Schema outputSchema(Schema input) {
> try{
> Schema tupleSchema = new Schema();
> tupleSchema.add(input.getField(4));
> tupleSchema.add(input.getField(3));
> tupleSchema.add(input.getField(2));
> tupleSchema.add(input.getField(1));
> tupleSchema.add(input.getField(0));
> System.out.println("inside schema");
> return new Schema(new
> Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(),
> input),
>
> tupleSchema, DataType.TUPLE));
>
> }catch (Exception e){
> return null;
> }
> }
>
> // this how i am initializing.
> Tuple result = TupleFactory.getInstance().newTuple(5);
>
> //values to the tuple are been added.
> result.set(0, 10);
> result.set(1, 20);
> result.set(2, 30);
> result.set(3, 40);
> result.set(4, 50);
>
> I am going wrong,
> so how to define schema for Tuple in java.
>