package shaman.hive.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import shaman.hive.udf.service.GeoService;

@Description(name="shaman_geo_countrycode", value="_FUNC_ get geo from ip. ")
public class UDFGeoIPCountryCode extends GenericUDF {
    
    private JavaStringObjectInspector stringInspector;
    private GeoService geoService;

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments)
            throws UDFArgumentException {
        if (arguments.length != 1) {
            throw new UDFArgumentLengthException("_FUNC_ take only one parameter ip address");
        }
        geoService = GeoService.getInstance();
        stringInspector = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
        return stringInspector;
    }

    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        String ip = stringInspector.getPrimitiveJavaObject(arguments[0].get());
        System.out.println(ip + " " + geoService.getCountryCode(ip) + " " + geoService.getCountryName(ip));
        return geoService.getCountryCode(ip);
    }

    @Override
    public String getDisplayString(String[] children) {
        return "get country code from ip address";
    }

}

@@ -1451,116 +1451,8 @@
 ));%0A
-        System.out.println(ip + %22 %22 + geoService.getCountryCode(ip) + %22 %22 + geoService.getCountryName(ip));%0A
     

delete debug output