0

I am new to write custom udf for hive. I have tried writing custom udf for toupper function succecfully.

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

@Description(
    name="SimpleUDFExample",
    value="returns 'hello x', where x is whatever you give it (STRING)",
    extended="SELECT simpleudfexample('world') from foo limit 1;"
    )
class SimpleUDFExample extends UDF {
  
 public Text evaluate(Text input) {
     if(input == null) return null;
     return new Text("Hello " + input.toString());
   }
}

I have to write different implementations of evaluate based on different input parameters.

My query is - How can i write multiple evaluate methods in a single class.Do I need to write different classes for different methods? If i write these overridden methods in a single class then How Hive is going to differ between different method which contains same type parameter.

Please Guide me about the right way.

DennisLi
  • 3,915
  • 6
  • 30
  • 66
Ashu
  • 1
  • 1

2 Answers2

1

In hive you can overload the method same as JAVA. But in UDF you have to use Hadoop Datatypes likes IntWritable, FloatWritable...

Please find below the code.

public class ToUpper extends UDF{

    public String evaluate(Text word) {
        String upperCase=word.toString();
        return upperCase;

    }

    public String evaluate(IntWritable word) {
        String upperCase="Error : Input type is Integer. Cannot convert to UpperCase";
        return upperCase;

    }

    public String evaluate(FloatWritable word) {
        String upperCase="Error : Input type is Float. Cannot convert to UpperCase";
        return upperCase;

    }

    public String evaluate(LongWritable word) {
        String upperCase="Error : Input type is Long. Cannot convert to UpperCase";
        return upperCase;

    }



}

For more Information about UDF visit this page

Nithin
  • 9,661
  • 14
  • 44
  • 67
0

Is overloading of this type possible?

Like -

public class ExampleUDF extends UDF{

    public Integer evaluate(Integer num) {
        return num*2;

    }

    public Integer evaluate(Integer num1, Integer num2) {
        return num1*num2;
    }
}