Skip to content

Instantly share code, notes, and snippets.

@rjurney
Created September 11, 2014 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjurney/d97f9ab600d5bde198f8 to your computer and use it in GitHub Desktop.
Save rjurney/d97f9ab600d5bde198f8 to your computer and use it in GitHub Desktop.
UDF for Pig to choose a field by the value of another field
// Enable multiple languages by specifying the model path. See http://text.sourceforge.net/models-1.5/
public Tuple exec(Tuple input) throws IOException
{
if(input.size() < 2) {
throw new IOException();
}
String fieldName = input.get(0).toString();
if(fieldName == null || fieldName == "") {
return null;
}
System.err.println("fieldName: " + fieldName);
Tuple outTuple = tf.newTuple(fieldName);
Schema inputSchema = getInputSchema();
int i = 0;
for(FieldSchema fieldSchema : inputSchema.getFields())
{
if(fieldSchema.alias == fieldName) {
outTuple.append(input.get(i));
System.err.println("Matched fieldname " + fieldName + " with value: " + input.get(i).toString());
}
}
return outTuple;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment