0

Im new with GenericUDF. I'm try to generate a function to create a telephone numbers with the use of Array<strings>.

But I have an ERROR:

Caused by: java.lang.ClassCastException: org.apache.hadoop.hive.serde2.lazy.LazyString cannot be cast to java.lang.String

In this Line:

String inumber = (String) listOi.getListElement(args[1].get(), i);

Could anyone help me, please? Thanks.

This is my code:

@Description(name = "create_numbers",
value = "_FUNCT_(prefix, array(number1, number2, ...)); first argument is a prefix(string) and the second argument is a array of strings."
        + "Return a list of phone numbers. "
        + "Example: prefix = +49 and 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 {        

    // we 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 any of them null, return also 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. We only accept two
    if (args.length != 2) {
          throw new UDFArgumentLengthException("Two arguments are needed: String and List<String>, found "+ args.length);
    }

    //Check we received the right objects types.
    if (!(args[0] instanceof StringObjectInspector) || !(args[1] instanceof ListObjectInspector)) {
      throw new UDFArgumentException("first argument must be a string and the second argument must be a list / array of string");
    }

    this.elemOi = (StringObjectInspector) args[0];
    this.listOi = (ListObjectInspector) args[1];

    //Check that the list contains strings
    if(!(listOi.getListElementObjectInspector() instanceof StringObjectInspector)) {
      throw new UDFArgumentException("second argument must be a list of strings");
    }

    // the return type of our function is an array of string, so we provide the correct object inspector        
    return (StringObjectInspector)listOi.getListElementObjectInspector();
}

@Override
public String getDisplayString(String[] arg0) {
    return "create_numbers";
}

}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
ChAkO
  • 1
  • 3

3 Answers3

0

Instead of

String inumber = (String) listOi.getListElement(args[1].get(), i); 

try

String inumber =  listOi.getListElement(args[1].get(), i).toString();

Since getListElement() return an Object so it should be castable.

Garry
  • 4,493
  • 3
  • 28
  • 48
  • Thanks for answer but the error persist Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.apache.hadoop.hive.serde2.lazy.LazyString at org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyStringObjectInspector.getPrimitiveWritableObject(LazyStringObjectInspector.java:51) – ChAkO Jul 08 '15 at 11:46
0

try

String inumber =  ((org.apache.hadoop.hive.serde2.lazy.LazyString) listOi.getListElement(args[1].get(), i)).getObject().toString();
Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • thanks .. but the error continue: Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.apache.hadoop.hive.serde2.lazy.LazyString at org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyStringObjectInspector.getPrimitiveWritableObject(LazyStringObjectInspector.java:51) – ChAkO Jul 08 '15 at 12:08
0

This is bit late but this may solve try once.

use below code in the loop:

public Object evaluate(DeferredObject[] args) throws HiveException { ... ... for(int i=0; i<numElem; i++){
LazyString inumber = (LazyString) listOi.getListElement(args[1].get(), i); String inumberLS = elemOi.get(inumber); resultlnumerbs.add(prefix + inumberLS); return resultlnumerbs; }

Return type in initialize method use

public ObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { ... ... return ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}