Search code examples
hivehiveqludf

Strange behavior of simple UDF in Hive


I have a hive table, which created using this code:

CREATE EXTERNAL TABLE IPRegions (
    ip STRING,
    -- other fields don't matter 
)

I need to output each ip in reverse order (yes, it is a very simple problem similar to SELECT ip FROM IPRegions) using self-written UDF. For doing this, I wrote the following UDF:

public class ReverseUDF extends UDF {
    /**
     * Reversing of a string.
     * @param str input string
     * @return reversed string
     */
    public String reverse(String str){
        return new StringBuilder(str).reverse().toString();
    }
}

Than I built the jar-file with this UDF and than wrote such query:

USE my_base;
add jar Reverser/jar/Reverser.jar;
create temporary function reverse as 'com.lab4udf.ReverseUDF';

select reverse(ip)
from IPRegions;

But I cought an exception:

FAILED: SemanticException [Error 10014]: Line 3:7 Wrong arguments 'ip': No matching method for class com.lab4udf.ReverseUDF with (string).

As I saw in many examples, in UDF often used hadoop types and not plain java types. Than I rewrote and rebuild my UDF:

public Text reverse(Text str){
        return new Text(new StringBuilder(str.toString())
                .reverse().toString());
}

But it didn't help. So, where does my error is? How I should change the UDF or the query to solve this problem?


Solution

  • So, I solved my problem.

    The method, which realize the functionality of UDF (e.g. reverse() in my UDF) must be called only evaluate() and nothing else!