Hive UDF - Generic UDF for all Primitive Type

1.9k Views Asked by At

I am trying to implement the Hive UDF with Parameter and so I am extending GenericUDF class.

The problem is my UDF works find on String Datatype however it throws error if I run on other data types. I want UDF to run regardless of data type.

Would someone please let me know what's wrong with following code.

@Description(name = "Encrypt", value = "Encrypt the Given Column", extended = "SELECT Encrypt('Hello World!', 'Key');")
public class Encrypt extends GenericUDF {
    StringObjectInspector key;
    StringObjectInspector col;

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        if (arguments.length != 2) {
            throw new UDFArgumentLengthException("Encrypt only takes 2 arguments: T, String");
        }

        ObjectInspector keyObject = arguments[1];
        ObjectInspector colObject = arguments[0];

        if (!(keyObject instanceof StringObjectInspector)) {
            throw new UDFArgumentException("Error: Key Type is Not String");
        }

        this.key = (StringObjectInspector) keyObject;
        this.col = (StringObjectInspector) colObject;

        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    @Override
    public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
        String keyString = key.getPrimitiveJavaObject(deferredObjects[1].get());
        String colString = col.getPrimitiveJavaObject(deferredObjects[0].get());
        return AES.encrypt(colString, keyString);
    }

    @Override
    public String getDisplayString(String[] strings) {
        return null;
    }

}


Error

java.lang.ClassCastException: org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaIntObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector

1

There are 1 best solutions below

1
On

I would suggest you to replace StringObjectInspector col with PrimitiveObjectInspector col and the corresponding cast this.col = (PrimitiveObjectInspector) colObject. Then there are two ways:

First is to process every possible Primitive type, like this

    switch (((PrimitiveTypeInfo) colObject.getTypeInfo()).getPrimitiveCategory()) {
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case TIMESTAMP:
            cast_long_type;
        case FLOAT:
        case DOUBLE:
            cast_double_type;
        case STRING:
             everyting_is_fine;
        case DECIMAL:
        case BOOLEAN:
            throw new UDFArgumentTypeException(0, "Unsupported yet");
        default:
            throw new UDFArgumentTypeException(0,
                    "Unsupported type");
    }
}

Another way, is to use PrimitiveObjectInspectorUtils.getString method:

Object colObject = col.getPrimitiveJavaObject(deferredObjects[0].get());
String colString = PrimitiveObjectInspectorUtils.getString(colObject, key);

It just pseudocode like examples. Hope it helps.