Java Code Examples for org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector#getTypeName()

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector#getTypeName() . 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
@Nonnull
public static PrimitiveObjectInspector asDoubleCompatibleOI(
        @Nonnull final ObjectInspector argOI) throws UDFArgumentTypeException {
    if (argOI.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but "
                + argOI.getTypeName() + " is passed.");
    }
    final PrimitiveObjectInspector oi = (PrimitiveObjectInspector) argOI;
    switch (oi.getPrimitiveCategory()) {
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case STRING:
        case TIMESTAMP:
            break;
        default:
            throw new UDFArgumentTypeException(0,
                "Only numeric or string type arguments are accepted but " + argOI.getTypeName()
                        + " is passed.");
    }
    return oi;
}
 
Example 2
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public MapConverter(ObjectInspector inputOI, SettableMapObjectInspector outputOI) {
  if (inputOI instanceof MapObjectInspector) {
    this.inputOI = (MapObjectInspector) inputOI;
    this.outputOI = outputOI;
    inputKeyOI = this.inputOI.getMapKeyObjectInspector();
    outputKeyOI = outputOI.getMapKeyObjectInspector();
    inputValueOI = this.inputOI.getMapValueObjectInspector();
    outputValueOI = outputOI.getMapValueObjectInspector();
    keyConverter = getConverter(inputKeyOI, outputKeyOI);
    valueConverter = getConverter(inputValueOI, outputValueOI);
  } else if (!(inputOI instanceof VoidObjectInspector)) {
    throw new UnsupportedOperationException(
        "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
            + "not supported yet.");
  }
}
 
Example 3
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asFloatingPointOI(@Nonnull final ObjectInspector argOI)
        throws UDFArgumentTypeException {
    if (argOI.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but "
                + argOI.getTypeName() + " is passed.");
    }
    final PrimitiveObjectInspector oi = (PrimitiveObjectInspector) argOI;
    switch (oi.getPrimitiveCategory()) {
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
            break;
        default:
            throw new UDFArgumentTypeException(0, "Only floating point number is accepted but "
                    + argOI.getTypeName() + " is passed.");
    }
    return oi;
}
 
Example 4
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asIntegerOI(@Nonnull final ObjectInspector argOI)
        throws UDFArgumentTypeException {
    if (argOI.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but "
                + argOI.getTypeName() + " is passed.");
    }
    final PrimitiveObjectInspector oi = (PrimitiveObjectInspector) argOI;
    switch (oi.getPrimitiveCategory()) {
        case INT:
        case SHORT:
        case LONG:
        case BYTE:
            break;
        default:
            throw new UDFArgumentTypeException(0,
                "Unexpected type '" + argOI.getTypeName() + "' is passed.");
    }
    return oi;
}
 
Example 5
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asIntCompatibleOI(@Nonnull final ObjectInspector argOI)
        throws UDFArgumentTypeException {
    if (argOI.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but "
                + argOI.getTypeName() + " is passed.");
    }
    final PrimitiveObjectInspector oi = (PrimitiveObjectInspector) argOI;
    switch (oi.getPrimitiveCategory()) {
        case INT:
        case SHORT:
        case LONG:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case BOOLEAN:
        case BYTE:
        case STRING:
            break;
        default:
            throw new UDFArgumentTypeException(0,
                "Unexpected type '" + argOI.getTypeName() + "' is passed.");
    }
    return oi;
}
 
Example 6
Source File: ArgrankUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    if (argOIs.length != 1) {
        throw new UDFArgumentLengthException(
            "argrank(array<ANY> a) takes exactly 1 argument: " + argOIs.length);
    }
    ObjectInspector argOI0 = argOIs[0];
    if (argOI0.getCategory() != Category.LIST) {
        throw new UDFArgumentException(
            "argrank(array<ANY> a) expects array<ANY> for the first argument: "
                    + argOI0.getTypeName());
    }

    this.listOI = HiveUtils.asListOI(argOI0);
    this.elemOI = listOI.getListElementObjectInspector();

    return ObjectInspectorFactory.getStandardListObjectInspector(
        PrimitiveObjectInspectorFactory.writableIntObjectInspector);
}
 
Example 7
Source File: DynamoDBDataParser.java    From emr-dynamodb-connector with Apache License 2.0 5 votes vote down vote up
public static Object getNumberObject(String data, ObjectInspector objectInspector) {
  String hiveType = objectInspector.getTypeName();
  if (hiveType.equals(serdeConstants.BIGINT_TYPE_NAME)) {
    return Long.parseLong(data);
  } else if (hiveType.equals(serdeConstants.DOUBLE_TYPE_NAME)) {
    return Double.parseDouble(data);
  }
  throw new IllegalArgumentException("Unsupported Hive type: " + hiveType);
}
 
Example 8
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static void validateFeatureOI(@Nonnull final ObjectInspector oi)
        throws UDFArgumentException {
    final String typeName = oi.getTypeName();
    if (!STRING_TYPE_NAME.equals(typeName) && !INT_TYPE_NAME.equals(typeName)
            && !BIGINT_TYPE_NAME.equals(typeName)) {
        throw new UDFArgumentException(
            "argument type for a feature must be List of key type [Int|BitInt|Text]: "
                    + typeName);
    }
}
 
Example 9
Source File: MulticlassOnlineClassifierUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected PrimitiveObjectInspector processFeaturesOI(@Nonnull ObjectInspector arg)
        throws UDFArgumentException {
    this.featureListOI = (ListObjectInspector) arg;
    ObjectInspector featureRawOI = featureListOI.getListElementObjectInspector();
    String keyTypeName = featureRawOI.getTypeName();
    if (!STRING_TYPE_NAME.equals(keyTypeName) && !INT_TYPE_NAME.equals(keyTypeName)
            && !BIGINT_TYPE_NAME.equals(keyTypeName)) {
        throw new UDFArgumentTypeException(0,
            "1st argument must be Map of key type [Int|BitInt|Text]: " + keyTypeName);
    }
    this.parseFeature = STRING_TYPE_NAME.equals(keyTypeName);
    return HiveUtils.asPrimitiveObjectInspector(featureRawOI);
}
 
Example 10
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asIntCompatibleOI(
        @Nonnull final ObjectInspector[] argOIs, final int argIndex)
        throws UDFArgumentException {
    ObjectInspector argOI = getObjectInspector(argOIs, argIndex);
    if (argOI.getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(argIndex,
            "Only primitive type arguments are accepted but " + argOI.getTypeName()
                    + " is passed.");
    }
    final PrimitiveObjectInspector oi = (PrimitiveObjectInspector) argOI;
    switch (oi.getPrimitiveCategory()) {
        case INT:
        case SHORT:
        case LONG:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case BOOLEAN:
        case BYTE:
        case STRING:
            break;
        default:
            throw new UDFArgumentTypeException(argIndex,
                "Unexpected type '" + argOI.getTypeName() + "' is passed.");
    }
    return oi;
}
 
Example 11
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static BooleanObjectInspector asBooleanOI(@Nonnull final ObjectInspector[] argOIs,
        final int argIndex) throws UDFArgumentException {
    ObjectInspector argOI = getObjectInspector(argOIs, argIndex);
    if (!BOOLEAN_TYPE_NAME.equals(argOI.getTypeName())) {
        throw new UDFArgumentTypeException(argIndex,
            "Argument type must be Boolean: " + argOI.getTypeName());
    }
    return (BooleanObjectInspector) argOI;
}
 
Example 12
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static BooleanObjectInspector asBooleanOI(@Nonnull final ObjectInspector argOI)
        throws UDFArgumentException {
    if (!BOOLEAN_TYPE_NAME.equals(argOI.getTypeName())) {
        throw new UDFArgumentException("Argument type must be Boolean: " + argOI.getTypeName());
    }
    return (BooleanObjectInspector) argOI;
}
 
Example 13
Source File: ArrayIntersectUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectInspector initialize(@Nonnull ObjectInspector[] argOIs)
        throws UDFArgumentException {
    final int argLength = argOIs.length;
    if (argLength < 2) {
        throw new UDFArgumentLengthException(
            "Expecting at least two arrays as arguments: " + argLength);
    }

    ListObjectInspector[] argListOIs = new ListObjectInspector[argLength];
    ListObjectInspector arg0ListOI = HiveUtils.asListOI(argOIs[0]);
    ObjectInspector arg0ElemOI = arg0ListOI.getListElementObjectInspector();
    argListOIs[0] = arg0ListOI;
    for (int i = 1; i < argLength; i++) {
        ListObjectInspector listOI = HiveUtils.asListOI(argOIs[i]);
        if (!ObjectInspectorUtils.compareTypes(listOI.getListElementObjectInspector(),
            arg0ElemOI)) {
            throw new UDFArgumentException(
                "Array types does not match: " + arg0ElemOI.getTypeName() + " != "
                        + listOI.getListElementObjectInspector().getTypeName());
        }
        argListOIs[i] = listOI;
    }

    this.argListOIs = argListOIs;
    this.result = new ArrayList<Object>();
    return ObjectInspectorUtils.getStandardObjectInspector(arg0ListOI);
}
 
Example 14
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns a converter that converts objects from one OI to another OI. The
 * returned (converted) object does not belong to the converter. Hence once convertor can be used
 * multiple times within one eval invocation.
 */
public Converter getConverter(ObjectInspector inputOI, ObjectInspector outputOI) {
  // If the inputOI is the same as the outputOI, just return an
  // IdentityConverter.
  if (inputOI.equals(outputOI)) {
    return new ObjectInspectorConverters.IdentityConverter();
  }
  Converter c = getConverterFromCache(inputOI, outputOI);
  if (c != null) {
    return c;
  }
  switch (outputOI.getCategory()) {
    case PRIMITIVE:
      return getConverter((PrimitiveObjectInspector) inputOI, (PrimitiveObjectInspector) outputOI);
    case STRUCT:
      c = new StructConverter(inputOI, (SettableStructObjectInspector) outputOI);
      break;
    case LIST:
      c = new ListConverter(inputOI, (SettableListObjectInspector) outputOI);
      break;
    case MAP:
      c = new MapConverter(inputOI, (SettableMapObjectInspector) outputOI);
      break;
    default:
      throw new UnsupportedOperationException(
          "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
              + " not supported yet.");
  }
  cacheConverter(inputOI, outputOI, c);
  return c;
}
 
Example 15
Source File: DynamoDBDataParser.java    From emr-dynamodb-connector with Apache License 2.0 5 votes vote down vote up
public static String getNumber(Object data, ObjectInspector objectInspector) {
  if (objectInspector.getTypeName().equals(serdeConstants.DOUBLE_TYPE_NAME)) {
    return Double.toString(((DoubleObjectInspector) objectInspector).get(data));
  } else if (objectInspector.getTypeName().equals(serdeConstants.BIGINT_TYPE_NAME)) {
    return Long.toString(((LongObjectInspector) objectInspector).get(data));
  }
  throw new IllegalArgumentException("Unknown object inspector type: " + objectInspector.getCategory()
          + " Type name: " + objectInspector.getTypeName());
}
 
Example 16
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static BinaryObjectInspector asBinaryOI(@Nonnull final ObjectInspector argOI)
        throws UDFArgumentException {
    if (!BINARY_TYPE_NAME.equals(argOI.getTypeName())) {
        throw new UDFArgumentException("Argument type must be Binary: " + argOI.getTypeName());
    }
    return (BinaryObjectInspector) argOI;
}
 
Example 17
Source File: MapRouletteUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    if (argOIs.length != 1 && argOIs.length != 2) {
        throw new UDFArgumentLengthException(
            "Expected exactly one argument for map_roulette: " + argOIs.length);
    }
    if (argOIs[0].getCategory() != ObjectInspector.Category.MAP) {
        throw new UDFArgumentTypeException(0,
            "Only map type argument is accepted but got " + argOIs[0].getTypeName());
    }

    this.mapOI = HiveUtils.asMapOI(argOIs[0]);
    this.valueOI = HiveUtils.asDoubleCompatibleOI(mapOI.getMapValueObjectInspector());

    if (argOIs.length == 2) {
        ObjectInspector argOI1 = argOIs[1];
        if (HiveUtils.isIntegerOI(argOI1) == false) {
            throw new UDFArgumentException(
                "The second argument of map_roulette must be integer type: "
                        + argOI1.getTypeName());
        }
        if (ObjectInspectorUtils.isConstantObjectInspector(argOI1)) {
            long seed = HiveUtils.getAsConstLong(argOI1);
            this._rand = new Random(seed); // fixed seed
        } else {
            this.seedOI = HiveUtils.asLongCompatibleOI(argOI1);
        }
    } else {
        this._rand = new Random(); // random seed
    }

    return mapOI.getMapKeyObjectInspector();
}
 
Example 18
Source File: MinHashUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    if (argOIs.length < 2) {
        throw new UDFArgumentException(
            "_FUNC_ takes more than 2 arguments: ANY item, Array<Int|BigInt|Text> features [, constant String options]");
    }
    this.itemOI = argOIs[0];

    this.featureListOI = (ListObjectInspector) argOIs[1];
    ObjectInspector featureRawOI = featureListOI.getListElementObjectInspector();
    String keyTypeName = featureRawOI.getTypeName();
    if (!STRING_TYPE_NAME.equals(keyTypeName) && !INT_TYPE_NAME.equals(keyTypeName)
            && !BIGINT_TYPE_NAME.equals(keyTypeName)) {
        throw new UDFArgumentTypeException(0,
            "1st argument must be Map of key type [Int|BitInt|Text]: " + keyTypeName);
    }
    this.parseFeature = STRING_TYPE_NAME.equals(keyTypeName);
    this.forwardObjs = new Object[2];

    processOptions(argOIs);

    ArrayList<String> fieldNames = new ArrayList<String>();
    ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();

    fieldNames.add("clusterid");
    fieldOIs.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
    fieldNames.add("item");
    fieldOIs.add(itemOI);

    return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}
 
Example 19
Source File: CacheableObjectInspectorConverters.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ListConverter(ObjectInspector inputOI, SettableListObjectInspector outputOI) {
  if (inputOI instanceof ListObjectInspector) {
    this.inputOI = (ListObjectInspector) inputOI;
    this.outputOI = outputOI;
    inputElementOI = this.inputOI.getListElementObjectInspector();
    outputElementOI = outputOI.getListElementObjectInspector();
    elementConverter = getConverter(inputElementOI, outputElementOI);
  } else if (!(inputOI instanceof VoidObjectInspector)) {
    throw new UnsupportedOperationException(
        "Hive internal error: conversion of " + inputOI.getTypeName() + " to " + outputOI.getTypeName()
            + "not supported yet.");
  }
}
 
Example 20
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
public static boolean isVoidOI(@Nonnull final ObjectInspector oi) {
    String typeName = oi.getTypeName();
    return VOID_TYPE_NAME.equals(typeName);
}