java - Hive GenericUDF return array<String> Error -
im new genericudf. i'm try generate function create telephone numbers use of array<strings>.
but have error:
caused by: java.lang.classcastexception: org.apache.hadoop.hive.serde2.lazy.lazystring cannot cast java.lang.string
in line:
string inumber = (string) listoi.getlistelement(args[1].get(), i);
could me, please? thanks.
this code:
@description(name = "create_numbers", value = "_funct_(prefix, array(number1, number2, ...)); first argument prefix(string) , second argument array of strings." + "return list of phone numbers. " + "example: prefix = +49 , arraynumbers = array[1234, 2346, 1356] - result: array[+491234, +492346, +491356].") public class udfgenericlistnumbers extends genericudf { listobjectinspector listoi; stringobjectinspector elemoi; public object evaluate(deferredobject[] args) throws hiveexception { // must have 2 arguments if(args==null || args.length != 2) { throw new hiveexception("received " + (args == null? "null" : integer.tostring(args.length) + " elements instead of 2")); } string prefix = elemoi.getprimitivejavaobject(args[0].get()); //if of them null, return null if(args[0] == null || args[1] == null){ return null; } list<string> resultlnumerbs = new arraylist<string>(); int numelem = listoi.getlistlength(args[1].get()); for(int i=0; i<numelem; i++){ string inumber = (string) listoi.getlistelement(args[1].get(), i); resultlnumerbs.add(prefix + inumber); } return resultlnumerbs; } public objectinspector initialize(objectinspector[] args) throws udfargumentexception { // check number of arguments. accept 2 if (args.length != 2) { throw new udfargumentlengthexception("two arguments needed: string , list<string>, found "+ args.length); } //check received right objects types. if (!(args[0] instanceof stringobjectinspector) || !(args[1] instanceof listobjectinspector)) { throw new udfargumentexception("first argument must string , second argument must list / array of string"); } this.elemoi = (stringobjectinspector) args[0]; this.listoi = (listobjectinspector) args[1]; //check list contains strings if(!(listoi.getlistelementobjectinspector() instanceof stringobjectinspector)) { throw new udfargumentexception("second argument must list of strings"); } // return type of our function array of string, provide correct object inspector return (stringobjectinspector)listoi.getlistelementobjectinspector(); } @override public string getdisplaystring(string[] arg0) { return "create_numbers"; } }
instead of
string inumber = (string) listoi.getlistelement(args[1].get(), i); try
string inumber = listoi.getlistelement(args[1].get(), i).tostring(); since getlistelement() return object should castable.
Comments
Post a Comment