Hi All,
       I follow this link http://wiki.apache.org/hadoop/Hive/HivePlugins
     First, to create a new class that extends UDF, with one or more methods
named evaluate
 Once hive is started up with your jars in the classpath, the final step is
to register your function:

create temporary function ip_to_country as 'com.example.hive.udf.IpToCountry';

it gives me the error the following error


Exception in thread "main" java.lang.NoClassDefFoundError:
com/maxmind/geoip/LookupService
    at com.econify.geoip.IpToCountry.<clinit>(IpToCountry.java:15)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at
org.apache.hadoop.hive.ql.exec.FunctionTask.getUdfClass(FunctionTask.java:118)
    at
org.apache.hadoop.hive.ql.exec.FunctionTask.createFunction(FunctionTask.java:74)
    at
org.apache.hadoop.hive.ql.exec.FunctionTask.execute(FunctionTask.java:62)
    at org.apache.hadoop.hive.ql.exec.Task.executeTask(Task.java:107)
    at
org.apache.hadoop.hive.ql.exec.TaskRunner.runSequential(TaskRunner.java:55)
    at org.apache.hadoop.hive.ql.Driver.launchTask(Driver.java:630)
    at org.apache.hadoop.hive.ql.Driver.execute(Driver.java:504)
    at org.apache.hadoop.hive.ql.Driver.run(Driver.java:382)
    at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:138)
    at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:197)
    at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.ClassNotFoundException: com.maxmind.geoip.LookupService
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 19 more


I have attached my java file with it.
/* Similar to a module declaration in Ruby (for example, in Ruby, we'd probably say `module Econify::GeoIP`) */
package com.econify.geoip;

/*  Similar to require.  We're loading up the classes that our code refers to: the Maxmind GeoIP LookupService, 
    Hive's User Defined Function class, and Hadoop's Text class (which is the class of the return value).
*/
import com.maxmind.geoip.LookupService;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * Originally found at http://www.mail-archive.com/[email protected]/msg00741.html
 */
 
 /** 
    This is a User Defined Function (UDF) to allow Hive to guess which country an IP address is in.
    When a UDF is registered (see the Hive wiki), Hive will call the "evaluate" method.
 */
public class IpToCountry extends UDF {    
    /*  Sets up a class variable pointing to the lookup service, the entry point for the Maxmind GeoIP db.  
        This assumes that you have their jar installed somewhere loadable (e.g CLASSPATH) */
    static LookupService ls;
    /*  Load the lookup service from the Maxmind GeoIP db on the filesystem */
    static {
        try {
            ls = new LookupService("/usr/local/share/GeoIP/GeoIP.dat",
                                        LookupService.GEOIP_MEMORY_CACHE );
        } catch (java.io.IOException ex) { 
            System.err.println(ex);
        }
    }
    
    /*  Sets up an instance variable to hold the result.
        I don't think this is actually necessary since the evaluate method returns its result, but I never tried removing it...
    */
    Text result = new Text();
    
    public Text evaluate(Text t){
        if (t == null) {
            return null;
        }
        
        /*  Try to set the result value to the name of the Country that the LookupService returns.  
            On error, just print the exception and move on. */
        try {
            result.set(ls.getCountry(t.toString()).getName());
        } catch (Exception ex){
            System.err.println(ex);
        }
        return result;
   }
}

Reply via email to