Created
September 11, 2014 21:05
-
-
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
This file contains 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
// 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