I've written the following UDF:

ISO8601ToHiveFormat.java:

package hiveudfs;

import org.apache.hadoop.hive.ql.exec.UDF;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ISO8601ToHiveFormat  extends UDF {

    public String hourFromISO8601(final String d){
        try{
            if( d == null )
                return null;
            SimpleDateFormat sdf1= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf2.format(sdf1.parse(d)); 
        } catch (ParseException pe) {
            return null;
        }
    }
}

In the src folder of my project I ran the follwing compile command to compile it:

javac -cp /usr/lib/hive/lib/hive-exec-0.10.0-cdh4.3.0.jar  ISO8601ToHiveFormat.java

and supsequntly I packed it into a jar

jar cf ../../HiveUDFs.jar hiveudfs/ISO8601ToHiveFormat.*

So, then I started hive and did:

hive> add jar /home/tom/Java/HiveUDFs.jar;
Added /home/tom/Java/HiveUDFs.jar to class path
Added resource: /home/tom/Java/HiveUDFs.jar
hive> create temporary function hourFromISO8601 as 'hiveudfs.ISO8601ToHiveFormat';
OK
Time taken: 0.083 seconds
hive> SELECT hourFromISO8601(logtimestamp) FROM mytable LIMIT 10;
FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments 'logtimestamp': No matching method for class hiveudfs.ISO8601ToHiveFormat with (string). Possible choices: 
hive>

The output of

hive> describe mytable;
OK
...
logtimestamp    string
...

What am I doing wrong here?

有帮助吗?

解决方案

toom - you have to override this(evaluate) method. then only UDF works

    public class yourclassname extends UDF {

       public String **evaluate**(your args) {

         // your computation logic

         return your_result;
    }

}

其他提示

As ramisetty.vijay says, you need to override the evaluate() method. Note you can provide multiple implementations of evaluate both with differing input parameters as well as return type.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top