Search in sources :

Example 26 with StringObjectInspector

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

the class RegexSerDe method serialize.

@Override
public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
    if (outputFormatString == null) {
        throw new SerDeException("Cannot write data into table because \"output.format.string\"" + " is not specified in serde properties of the table.");
    }
    // Get all the fields out.
    // NOTE: The correct way to get fields out of the row is to use
    // objInspector.
    // The obj can be a Java ArrayList, or a Java class, or a byte[] or
    // whatever.
    // The only way to access the data inside the obj is through
    // ObjectInspector.
    StructObjectInspector outputRowOI = (StructObjectInspector) objInspector;
    List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs();
    if (outputFieldRefs.size() != numColumns) {
        throw new SerDeException("Cannot serialize the object because there are " + outputFieldRefs.size() + " fields but the table has " + numColumns + " columns.");
    }
    // Get all data out.
    for (int c = 0; c < numColumns; c++) {
        Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c));
        ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector();
        // The data must be of type String
        StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI;
        // Convert the field to Java class String, because objects of String type
        // can be
        // stored in String, Text, or some other classes.
        outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field);
    }
    // Format the String
    String outputRowString = null;
    try {
        outputRowString = String.format(outputFormatString, outputFields);
    } catch (MissingFormatArgumentException e) {
        throw new SerDeException("The table contains " + numColumns + " columns, but the outputFormatString is asking for more.", e);
    }
    outputRowText.set(outputRowString);
    return outputRowText;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) MissingFormatArgumentException(java.util.MissingFormatArgumentException) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 27 with StringObjectInspector

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

the class TypedBytesSerDe method serializeField.

private void serializeField(Object o, ObjectInspector oi, Object reuse) throws IOException {
    switch(oi.getCategory()) {
        case PRIMITIVE:
            {
                PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
                switch(poi.getPrimitiveCategory()) {
                    case VOID:
                        {
                            return;
                        }
                    case BOOLEAN:
                        {
                            BooleanObjectInspector boi = (BooleanObjectInspector) poi;
                            BooleanWritable r = reuse == null ? new BooleanWritable() : (BooleanWritable) reuse;
                            r.set(boi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case BYTE:
                        {
                            ByteObjectInspector boi = (ByteObjectInspector) poi;
                            ByteWritable r = reuse == null ? new ByteWritable() : (ByteWritable) reuse;
                            r.set(boi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case SHORT:
                        {
                            ShortObjectInspector spoi = (ShortObjectInspector) poi;
                            ShortWritable r = reuse == null ? new ShortWritable() : (ShortWritable) reuse;
                            r.set(spoi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case INT:
                        {
                            IntObjectInspector ioi = (IntObjectInspector) poi;
                            IntWritable r = reuse == null ? new IntWritable() : (IntWritable) reuse;
                            r.set(ioi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case LONG:
                        {
                            LongObjectInspector loi = (LongObjectInspector) poi;
                            LongWritable r = reuse == null ? new LongWritable() : (LongWritable) reuse;
                            r.set(loi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case FLOAT:
                        {
                            FloatObjectInspector foi = (FloatObjectInspector) poi;
                            FloatWritable r = reuse == null ? new FloatWritable() : (FloatWritable) reuse;
                            r.set(foi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case DOUBLE:
                        {
                            DoubleObjectInspector doi = (DoubleObjectInspector) poi;
                            DoubleWritable r = reuse == null ? new DoubleWritable() : (DoubleWritable) reuse;
                            r.set(doi.get(o));
                            tbOut.write(r);
                            return;
                        }
                    case STRING:
                        {
                            StringObjectInspector soi = (StringObjectInspector) poi;
                            Text t = soi.getPrimitiveWritableObject(o);
                            tbOut.write(t);
                            return;
                        }
                    default:
                        {
                            throw new RuntimeException("Unrecognized type: " + poi.getPrimitiveCategory());
                        }
                }
            }
        case LIST:
        case MAP:
        case STRUCT:
            {
                // For complex object, serialize to JSON format
                String s = SerDeUtils.getJSONString(o, oi);
                Text t = reuse == null ? new Text() : (Text) reuse;
                // convert to Text and write it
                t.set(s);
                tbOut.write(t);
            }
        default:
            {
                throw new RuntimeException("Unrecognized type: " + oi.getCategory());
            }
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) Text(org.apache.hadoop.io.Text) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) FloatWritable(org.apache.hadoop.io.FloatWritable) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) BooleanWritable(org.apache.hadoop.io.BooleanWritable) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 28 with StringObjectInspector

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

the class DruidSerDe method serialize.

@Override
public Writable serialize(Object o, ObjectInspector objectInspector) throws SerDeException {
    if (objectInspector.getCategory() != ObjectInspector.Category.STRUCT) {
        throw new SerDeException(getClass().toString() + " can only serialize struct types, but we got: " + objectInspector.getTypeName());
    }
    // Prepare the field ObjectInspectors
    StructObjectInspector soi = (StructObjectInspector) objectInspector;
    List<? extends StructField> fields = soi.getAllStructFieldRefs();
    List<Object> values = soi.getStructFieldsDataAsList(o);
    // We deserialize the result
    Map<String, Object> value = new HashMap<>();
    for (int i = 0; i < columns.length; i++) {
        if (values.get(i) == null) {
            // null, we just add it
            value.put(columns[i], null);
            continue;
        }
        final Object res;
        switch(types[i].getPrimitiveCategory()) {
            case TIMESTAMP:
                res = ((TimestampObjectInspector) fields.get(i).getFieldObjectInspector()).getPrimitiveJavaObject(values.get(i)).getTime();
                break;
            case BYTE:
                res = ((ByteObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case SHORT:
                res = ((ShortObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case INT:
                res = ((IntObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case LONG:
                res = ((LongObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case FLOAT:
                res = ((FloatObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case DOUBLE:
                res = ((DoubleObjectInspector) fields.get(i).getFieldObjectInspector()).get(values.get(i));
                break;
            case DECIMAL:
                res = ((HiveDecimalObjectInspector) fields.get(i).getFieldObjectInspector()).getPrimitiveJavaObject(values.get(i)).doubleValue();
                break;
            case STRING:
                res = ((StringObjectInspector) fields.get(i).getFieldObjectInspector()).getPrimitiveJavaObject(values.get(i));
                break;
            default:
                throw new SerDeException("Unknown type: " + types[i].getPrimitiveCategory());
        }
        value.put(columns[i], res);
    }
    value.put(Constants.DRUID_TIMESTAMP_GRANULARITY_COL_NAME, ((TimestampObjectInspector) fields.get(columns.length).getFieldObjectInspector()).getPrimitiveJavaObject(values.get(columns.length)).getTime());
    return new DruidWritable(value);
}
Also used : TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HashMap(java.util.HashMap) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 29 with StringObjectInspector

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

the class GenericUDFBaseCompare method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentException(opName + " requires two arguments.");
    }
    argumentOIs = arguments;
    for (int i = 0; i < arguments.length; i++) {
        Category category = arguments[i].getCategory();
        if (category != Category.PRIMITIVE) {
            throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + "  is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
        }
    }
    if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.stringTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.stringTypeInfo)) {
        soi0 = (StringObjectInspector) arguments[0];
        soi1 = (StringObjectInspector) arguments[1];
        if (soi0.preferWritable() || soi1.preferWritable()) {
            compareType = CompareType.COMPARE_TEXT;
        } else {
            compareType = CompareType.COMPARE_STRING;
        }
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.intTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.intTypeInfo)) {
        compareType = CompareType.COMPARE_INT;
        ioi0 = (IntObjectInspector) arguments[0];
        ioi1 = (IntObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.longTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.longTypeInfo)) {
        compareType = CompareType.COMPARE_LONG;
        loi0 = (LongObjectInspector) arguments[0];
        loi1 = (LongObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.byteTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.byteTypeInfo)) {
        compareType = CompareType.COMPARE_BYTE;
        byoi0 = (ByteObjectInspector) arguments[0];
        byoi1 = (ByteObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.booleanTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.booleanTypeInfo)) {
        compareType = CompareType.COMPARE_BOOL;
        boi0 = (BooleanObjectInspector) arguments[0];
        boi1 = (BooleanObjectInspector) arguments[1];
    } else {
        TypeInfo oiTypeInfo0 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
        TypeInfo oiTypeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]);
        if (oiTypeInfo0 == oiTypeInfo1 || TypeInfoUtils.doPrimitiveCategoriesMatch(oiTypeInfo0, oiTypeInfo1)) {
            compareType = CompareType.SAME_TYPE;
        } else {
            compareType = CompareType.NEED_CONVERT;
            TypeInfo compareType = FunctionRegistry.getCommonClassForComparison(oiTypeInfo0, oiTypeInfo1);
            // For now, we always convert to double if we can't find a common type
            compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo((compareType == null) ? TypeInfoFactory.doubleTypeInfo : compareType);
            converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
            converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
        }
    }
    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 30 with StringObjectInspector

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

the class ColumnStatsTask method unpackDoubleStats.

private void unpackDoubleStats(ObjectInspector oi, Object o, String fName, ColumnStatisticsObj statsObj) {
    if (fName.equals("countnulls")) {
        long v = ((LongObjectInspector) oi).get(o);
        statsObj.getStatsData().getDoubleStats().setNumNulls(v);
    } else if (fName.equals("numdistinctvalues")) {
        long v = ((LongObjectInspector) oi).get(o);
        statsObj.getStatsData().getDoubleStats().setNumDVs(v);
    } else if (fName.equals("max")) {
        double d = ((DoubleObjectInspector) oi).get(o);
        statsObj.getStatsData().getDoubleStats().setHighValue(d);
    } else if (fName.equals("min")) {
        double d = ((DoubleObjectInspector) oi).get(o);
        statsObj.getStatsData().getDoubleStats().setLowValue(d);
    } else if (fName.equals("ndvbitvector")) {
        PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
        String v = ((StringObjectInspector) poi).getPrimitiveJavaObject(o);
        statsObj.getStatsData().getDoubleStats().setBitVectors(v);
        ;
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)

Aggregations

StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)30 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)15 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)15 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)12 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)12 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)12 ByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector)11 FloatObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)11 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)11 BooleanObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector)10 ShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector)10 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)10 BinaryObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector)9 Text (org.apache.hadoop.io.Text)9 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)8 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)7 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)7 HiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)7 ArrayList (java.util.ArrayList)6