Java Code Examples for org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils#getTypeInfoFromObjectInspector()

The following examples show how to use org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils#getTypeInfoFromObjectInspector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String[] getConstStringArray(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    if (!ObjectInspectorUtils.isConstantObjectInspector(oi)) {
        throw new UDFArgumentException("argument must be a constant value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    ConstantObjectInspector constOI = (ConstantObjectInspector) oi;
    if (constOI.getCategory() != Category.LIST) {
        throw new UDFArgumentException(
            "argument must be an array: " + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    final List<?> lst = (List<?>) constOI.getWritableConstantValue();
    if (lst == null) {
        return null;
    }
    final int size = lst.size();
    final String[] ary = new String[size];
    for (int i = 0; i < size; i++) {
        Object o = lst.get(i);
        if (o != null) {
            ary[i] = o.toString();
        }
    }
    return ary;
}
 
Example 2
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
public static <T extends Writable> T getConstValue(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    if (!ObjectInspectorUtils.isConstantObjectInspector(oi)) {
        throw new UDFArgumentException("argument must be a constant value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    ConstantObjectInspector constOI = (ConstantObjectInspector) oi;
    Object v = constOI.getWritableConstantValue();
    return (T) v;
}
 
Example 3
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nullable
public static double[] getConstDoubleArray(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    if (!ObjectInspectorUtils.isConstantObjectInspector(oi)) {
        throw new UDFArgumentException("argument must be a constant value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    ConstantObjectInspector constOI = (ConstantObjectInspector) oi;
    if (constOI.getCategory() != Category.LIST) {
        throw new UDFArgumentException(
            "argument must be an array: " + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    StandardConstantListObjectInspector listOI = (StandardConstantListObjectInspector) constOI;
    PrimitiveObjectInspector elemOI =
            HiveUtils.asDoubleCompatibleOI(listOI.getListElementObjectInspector());

    final List<?> lst = listOI.getWritableConstantValue();
    if (lst == null) {
        return null;
    }
    final int size = lst.size();
    final double[] ary = new double[size];
    for (int i = 0; i < size; i++) {
        Object o = lst.get(i);
        if (o == null) {
            ary[i] = Double.NaN;
        } else {
            ary[i] = PrimitiveObjectInspectorUtils.getDouble(o, elemOI);
        }
    }
    return ary;
}
 
Example 4
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getConstString(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    if (!isStringOI(oi)) {
        throw new UDFArgumentException("argument must be a Text value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    Text v = getConstValue(oi);
    return v == null ? null : v.toString();
}
 
Example 5
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getConstString(@Nonnull final ObjectInspector[] argOIs, final int argIndex)
        throws UDFArgumentException {
    final ObjectInspector oi = getObjectInspector(argOIs, argIndex);
    if (!isStringOI(oi)) {
        throw new UDFArgumentTypeException(argIndex, "argument must be a Text value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    Text v = getConstValue(oi);
    return v == null ? null : v.toString();
}
 
Example 6
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static boolean getConstBoolean(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    if (!isBooleanOI(oi)) {
        throw new UDFArgumentException("argument must be a Boolean value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    BooleanWritable v = getConstValue(oi);
    return v.get();
}
 
Example 7
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static boolean getConstBoolean(@Nonnull final ObjectInspector[] argOIs,
        final int argIndex) throws UDFArgumentException {
    final ObjectInspector oi = getObjectInspector(argOIs, argIndex);
    if (!isBooleanOI(oi)) {
        throw new UDFArgumentTypeException(argIndex, "argument must be a Boolean value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    BooleanWritable v = getConstValue(oi);
    return v.get();
}
 
Example 8
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static int getConstInt(@Nonnull final ObjectInspector oi) throws UDFArgumentException {
    if (!isIntOI(oi)) {
        throw new UDFArgumentException("argument must be a Int value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    IntWritable v = getConstValue(oi);
    return v.get();
}
 
Example 9
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static long getConstLong(@Nonnull final ObjectInspector oi) throws UDFArgumentException {
    if (!isBigIntOI(oi)) {
        throw new UDFArgumentException("argument must be a BigInt value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    LongWritable v = getConstValue(oi);
    return v.get();
}
 
Example 10
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ConstantObjectInspector asConstantObjectInspector(
        @Nonnull final ObjectInspector oi) throws UDFArgumentException {
    if (!ObjectInspectorUtils.isConstantObjectInspector(oi)) {
        throw new UDFArgumentException("argument must be a constant value: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    return (ConstantObjectInspector) oi;
}
 
Example 11
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asPrimitiveObjectInspector(
        @Nonnull final ObjectInspector oi) throws UDFArgumentException {
    if (oi.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentException("Expecting PrimitiveObjectInspector: "
                + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    return (PrimitiveObjectInspector) oi;
}
 
Example 12
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asPrimitiveObjectInspector(
        @Nonnull final ObjectInspector[] argOIs, final int argIndex)
        throws UDFArgumentException {
    final ObjectInspector oi = getObjectInspector(argOIs, argIndex);
    if (oi.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentException("Expecting PrimitiveObjectInspector for argOIs["
                + argIndex + "] but got " + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    return (PrimitiveObjectInspector) oi;
}
 
Example 13
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ListObjectInspector asListOI(@Nonnull final ObjectInspector[] argOIs,
        final int argIndex) throws UDFArgumentException {
    final ObjectInspector oi = getObjectInspector(argOIs, argIndex);
    Category category = oi.getCategory();
    if (category != Category.LIST) {
        throw new UDFArgumentException("Expecting ListObjectInspector for argOIs[" + argIndex
                + "] but got " + TypeInfoUtils.getTypeInfoFromObjectInspector(oi));
    }
    return (ListObjectInspector) oi;
}
 
Example 14
Source File: OrcStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
private TypeInfo getTypeInfoFromLocation(String location, Job job) throws IOException {
    FileSystem fs = FileSystem.get(job.getConfiguration());
    Path path = getFirstFile(location, fs);
    if (path == null) {
        log.info("Cannot find any ORC files from " + location +
                ". Probably multiple load store in script.");
        return null;
    }
    Reader reader = OrcFile.createReader(fs, path);
    ObjectInspector oip = (ObjectInspector)reader.getObjectInspector();
    return TypeInfoUtils.getTypeInfoFromObjectInspector(oip);
}
 
Example 15
Source File: HiveUtils.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
static StructTypeInfo typeInfo(StructObjectInspector inspector) {
    return (StructTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(inspector);
}
 
Example 16
Source File: OrcTestTools.java    From incubator-gobblin with Apache License 2.0 2 votes vote down vote up
/**
 * Convert Avro schema into TypeInfo.
 * Current version of Hive used by Gobblin open-source(1.0.1) doesn't have {@link org.apache.orc.TypeDescription}
 * and utilities associated with it. So instead {@link TypeInfo} is being used to represent Orc schema.
 * Note that {@link TypeInfo} is not case preserving as it is actually the internal schema representation of Hive.
 */
public static TypeInfo convertAvroSchemaToOrcSchema(Schema avroSchema) throws SerDeException {
      return TypeInfoUtils.getTypeInfoFromObjectInspector(
          new AvroObjectInspectorGenerator(avroSchema).getObjectInspector());
}