/** * Initialize this GenericUDF. This will be called once and only once per * GenericUDF instance. * * @param arguments The ObjectInspector for the arguments * @return The ObjectInspector for the return value * @throws UDFArgumentException Thrown when arguments have wrong types, wrong length, etc. */ @Override public ObjectInspector initialize(ObjectInspector[] arguments)throws UDFArgumentException { ObjectInspector arg = arguments[0]; if (arguments.length != 1) { thrownew UDFArgumentLengthException( "The operator 'SubstrCgi' accepts one arguments."); } try { IPDB = new City(this.getClass().getResourceAsStream("/ipipfree.ipdb")); } catch (IOException e) { } this.allCgi = (StringObjectInspector) arg; return PrimitiveObjectInspectorFactory.javaStringObjectInspector; }
/** * Evaluate the GenericUDF with the arguments. * * @param arguments The arguments as DeferedObject, use DeferedObject.get() to get the * actual argument Object. The Objects can be inspected by the * ObjectInspectors passed in the initialize call. * @return The */ @Override public Object evaluate(GenericUDF.DeferredObject[] arguments)throws HiveException { String cgi = allCgi.getPrimitiveJavaObject(arguments[0].get()); if(null == cgi) { returnnull; } String ipInfo = ""; try { ipInfo = IPDB.find(cgi.toString(), "CN")[2]; } catch (Exception e) { } return ipInfo; }
/** * Get the String to be displayed in explain. * * @param children */ @Override public String getDisplayString(String[] children){ return"Usage: SubstrCgi(String cgi)"; } }