Search in sources :

Example 1 with AbstractPrimitiveJavaObjectInspector

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

the class RegexSerDe method initialize.

@Override
public void initialize(Configuration configuration, Properties tableProperties, Properties partitionProperties) throws SerDeException {
    super.initialize(configuration, tableProperties, partitionProperties);
    numColumns = getColumnNames().size();
    // Read the configuration parameters
    inputRegex = properties.getProperty(INPUT_REGEX);
    boolean inputRegexIgnoreCase = "true".equalsIgnoreCase(properties.getProperty(INPUT_REGEX_CASE_SENSITIVE));
    // output format string is not supported anymore, warn user of deprecation
    if (null != properties.getProperty("output.format.string")) {
        log.warn("output.format.string has been deprecated");
    }
    // Parse the configuration parameters
    if (inputRegex != null) {
        inputPattern = Pattern.compile(inputRegex, Pattern.DOTALL + (inputRegexIgnoreCase ? Pattern.CASE_INSENSITIVE : 0));
    } else {
        inputPattern = null;
        throw new SerDeException("This table does not have serde property \"input.regex\"!");
    }
    /* Constructing the row ObjectInspector:
     * The row consists of some set of primitive columns, each column will
     * be a java object of primitive type.
     */
    List<ObjectInspector> columnOIs = new ArrayList<>(getColumnNames().size());
    for (int c = 0; c < numColumns; c++) {
        TypeInfo typeInfo = getColumnTypes().get(c);
        if (typeInfo instanceof PrimitiveTypeInfo) {
            PrimitiveTypeInfo pti = (PrimitiveTypeInfo) typeInfo;
            AbstractPrimitiveJavaObjectInspector oi = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pti);
            columnOIs.add(oi);
        } else {
            throw new SerDeException(getClass().getName() + " doesn't allow column [" + c + "] named " + getColumnNames().get(c) + " with type " + typeInfo);
        }
    }
    // StandardStruct uses ArrayList to store the row.
    rowOI = ObjectInspectorFactory.getStandardStructObjectInspector(getColumnNames(), columnOIs, Lists.newArrayList(Splitter.on('\0').split(properties.getProperty("columns.comments"))));
    // Constructing the row object, etc, which will be reused for all rows.
    row = new ArrayList<>(Collections.nCopies(numColumns, null));
    outputFields = new Object[numColumns];
    outputRowText = new Text();
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) AbstractPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector) ArrayList(java.util.ArrayList) AbstractPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector) Text(org.apache.hadoop.io.Text) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 2 with AbstractPrimitiveJavaObjectInspector

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

the class VectorUDFArgDesc method prepareConstant.

/* Prepare the constant for use when the function is called. To be used
   * during initialization.
   */
public void prepareConstant() {
    final Object writableValue;
    if (constExpr != null) {
        Object constantValue = constExpr.getValue();
        TypeInfo typeInfo = constExpr.getTypeInfo();
        ObjectInspector objectInspector = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(typeInfo);
        Category category = typeInfo.getCategory();
        switch(category) {
            case PRIMITIVE:
                {
                    PrimitiveCategory pc = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
                    // Convert from Java to Writable
                    AbstractPrimitiveJavaObjectInspector primitiveJavaObjectInspector = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pc);
                    writableValue = primitiveJavaObjectInspector.getPrimitiveWritableObject(constantValue);
                }
                break;
            case STRUCT:
                {
                    if (constantValue.getClass().isArray()) {
                        constantValue = java.util.Arrays.asList((Object[]) constantValue);
                    }
                    StructObjectInspector structObjectInspector = (StructObjectInspector) objectInspector;
                    List<? extends StructField> fields = structObjectInspector.getAllStructFieldRefs();
                    List<String> fieldNames = new ArrayList<String>(fields.size());
                    List<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>(fields.size());
                    for (StructField f : fields) {
                        fieldNames.add(f.getFieldName());
                        fieldObjectInspectors.add(ObjectInspectorUtils.getStandardObjectInspector(f.getFieldObjectInspector(), ObjectInspectorCopyOption.WRITABLE));
                    }
                    StandardConstantStructObjectInspector constantStructObjectInspector = ObjectInspectorFactory.getStandardConstantStructObjectInspector(fieldNames, fieldObjectInspectors, (List<?>) constantValue);
                    writableValue = constantStructObjectInspector.getWritableConstantValue();
                }
                break;
            default:
                throw new RuntimeException("Unexpected category " + category);
        }
    } else {
        writableValue = null;
    }
    constObjVal = new GenericUDF.DeferredJavaObject(writableValue);
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StandardConstantStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardConstantStructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) AbstractPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) AbstractPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) ArrayList(java.util.ArrayList) List(java.util.List) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) StandardConstantStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardConstantStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StandardConstantStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardConstantStructObjectInspector)

Aggregations

ArrayList (java.util.ArrayList)2 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)2 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)2 AbstractPrimitiveJavaObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.AbstractPrimitiveJavaObjectInspector)2 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)2 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)2 List (java.util.List)1 GenericUDF (org.apache.hadoop.hive.ql.udf.generic.GenericUDF)1 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)1 Category (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category)1 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)1 StandardConstantStructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StandardConstantStructObjectInspector)1 StructField (org.apache.hadoop.hive.serde2.objectinspector.StructField)1 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)1 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)1 Text (org.apache.hadoop.io.Text)1