Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Pig Wiki" for change 
notification.

The following page has been changed by OlgaN:
http://wiki.apache.org/pig/UDFManual

------------------------------------------------------------------------------
  
  The first line indicates that the function is part of the `myudfs` package. 
The UDF class extends the `EvalFunc` class which is the base class for all eval 
functions. It is parameterized with the return type of the UDF which is a Java 
`String` in this case. We will look into the `EvalFunc` class in more detail 
later, but for now all we need to do is to implement the `exec` function. This 
function is invoked on every input tuple. The input into the function is a 
tuple with input parameters in the order they are passed to the function in the 
Pig script. In our example, it will contain a single string field corresponding 
to the student name.
  
- The first thing to decide is what to do with invalid data. This depends on 
the format of the data. If the data is of type `bytearray` it means that it has 
not yet been converted to its proper type. In this case, if the format of the 
data does not match the expected type, a NULL value should be returned. If, on 
the other hand, the input data is of another type, this means that the 
conversion has already happened and the data should be in the correct format. 
This is the case with our example and that's why it throws an error (line 15.) 
Note that `WrappedIOException` is a helper class to convert the actual 
exception to an IOException. 
+ The first thing to decide is what to do with invalid data. This depends on 
the format of the data. If the data is of type `bytearray` it means that it has 
not yet been converted to its proper type. In this case, if the format of the 
data does not match the expected type, a NULL value should be returned. If, on 
the other hand, the input data is of another type, this means that the 
conversion has already happened and the data should be in the correct format. 
This is the case with our example and that's why it throws an error (line 16.) 
Note that `WrappedIOException` is a helper class to convert the actual 
exception to an IOException. 
  
- Also, note that lines 9-10 check if the input data is null or empty and if so 
returns null.
+ Also, note that lines 10-11 check if the input data is null or empty and if 
so returns null.
  
- The actual function implementation is on lines 12-13 and is self-explanatory.
+ The actual function implementation is on lines 13-14 and is self-explanatory.
  
  Now that we have the function implemented, it needs to be compiled and 
included in a jar. You will need a `pig.jar` built from the `types` branch to 
compile your UDF. You can use the following set of commands to checkout the 
code from SVN repository and create pig.jar:
  

Reply via email to