Hortonworks Streaming Analytics Manager User Guide
Also available as:
PDF

Creating UDFs

About This Task

User Defined Functions (UDFs) enable you to perform simple transformations on event streams. This is used in the Projection processor.

Steps

  1. Create a UDF in which I is the input type and O is the output type:

    public interface UDF<O, I> {
       O evaluate(I i);
    }
    

    The evaluate method is invoked with the corresponding field value for each event in the stream.

  2. For functions that accept two or more parameters, create the corresponding UDF interfaces:

    public interface UDF2<O, I1, I2> {
       O evaluate(I1 input1, I2 input2);
    }
    

Example 1

The ConvertToTimestampLong UDF is a good example of a new UDF implementation.

Example 2

In the following example, you want to concatenate the values of two fields of an event. To do this, you define a MyConcat function by implementing the UDF2 interface:

pubic class MyConcat implements UDF2<String, String, String> {
   public String evaluate(String s1, String s2) {
       return s1.concat(s2);
   }
}