Search in sources :

Example 6 with TextConverter

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter in project hive by apache.

the class GenericUDFBaseTrim method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentException(udfName + " requires one value argument. Found :" + arguments.length);
    }
    PrimitiveObjectInspector argumentOI;
    if (arguments[0] instanceof PrimitiveObjectInspector) {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
    } else {
        throw new UDFArgumentException(udfName + " takes only primitive types. found " + arguments[0].getTypeName());
    }
    switch(argumentOI.getPrimitiveCategory()) {
        case STRING:
        case CHAR:
        case VARCHAR:
            break;
        default:
            throw new UDFArgumentException(udfName + " takes only STRING/CHAR/VARCHAR types. Found " + argumentOI.getPrimitiveCategory());
    }
    converter = new TextConverter(argumentOI);
    return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) TextConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter)

Example 7 with TextConverter

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter in project hive by apache.

the class TestObjectInspectorConverters method convertText.

private void convertText() {
    Converter textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    assertEquals("TextConverter", new Text("0"), textConverter.convert(Integer.valueOf(0)));
    assertEquals("TextConverter", new Text("1"), textConverter.convert(Integer.valueOf(1)));
    assertEquals("TextConverter", null, textConverter.convert(null));
    textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.writableBinaryObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    assertEquals("TextConverter", new Text("hive"), textConverter.convert(new BytesWritable(new byte[] { (byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e' })));
    assertEquals("TextConverter", null, textConverter.convert(null));
    textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    assertEquals("TextConverter", new Text("hive"), textConverter.convert(new Text("hive")));
    assertEquals("TextConverter", null, textConverter.convert(null));
    textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    assertEquals("TextConverter", new Text("hive"), textConverter.convert(new String("hive")));
    assertEquals("TextConverter", null, textConverter.convert(null));
    textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaHiveDecimalObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    assertEquals("TextConverter", new Text("100.001000000000000000"), textConverter.convert(HiveDecimal.create("100.001")));
    assertEquals("TextConverter", null, textConverter.convert(null));
}
Also used : Converter(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable)

Aggregations

UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)5 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)4 TextConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter)4 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)2 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)2 Converter (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter)2 TimestampConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter)2 BytesWritable (org.apache.hadoop.io.BytesWritable)2 Text (org.apache.hadoop.io.Text)2 ArrayList (java.util.ArrayList)1 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)1 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)1 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)1 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)1 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)1 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)1 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)1 HiveJsonReader (org.apache.hadoop.hive.serde2.json.HiveJsonReader)1