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

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector#getCategory() . 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 Project: presto   File: SerDeUtils.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static Block serializeObject(Type type, BlockBuilder builder, Object object, ObjectInspector inspector, boolean filterNullMapKeys)
{
    switch (inspector.getCategory()) {
        case PRIMITIVE:
            serializePrimitive(type, builder, object, (PrimitiveObjectInspector) inspector);
            return null;
        case LIST:
            return serializeList(type, builder, object, (ListObjectInspector) inspector);
        case MAP:
            return serializeMap(type, builder, object, (MapObjectInspector) inspector, filterNullMapKeys);
        case STRUCT:
            return serializeStruct(type, builder, object, (StructObjectInspector) inspector);
        case UNION:
            return serializeUnion(type, builder, object, (UnionObjectInspector) inspector);
    }
    throw new RuntimeException("Unknown object inspector category: " + inspector.getCategory());
}
 
Example 2
Source Project: incubator-hivemall   File: HiveUtils.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PrimitiveObjectInspector asLongCompatibleOI(@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 LONG:
        case INT:
        case SHORT:
        case BYTE:
        case BOOLEAN:
        case FLOAT:
        case DOUBLE:
        case DECIMAL:
        case STRING:
        case TIMESTAMP:
            break;
        default:
            throw new UDFArgumentTypeException(0,
                "Unexpected type '" + argOI.getTypeName() + "' is passed.");
    }
    return oi;
}
 
Example 3
/**
 * @see org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector#getStructFieldData(java.lang.Object,
 *      org.apache.hadoop.hive.serde2.objectinspector.StructField)
 */
@SuppressWarnings("unchecked")
@Override
public Object getStructFieldData(Object data, StructField structField) {
    if ((data instanceof List) && !(data instanceof SerDeArray)) {
        MyField f = (MyField) structField;
        int fieldID = f.getFieldID();
        return ((List<Object>) data).get(fieldID);
    } else {
        ObjectInspector fieldObjectInspector = structField.getFieldObjectInspector();
        Category category = fieldObjectInspector.getCategory();
        Object fieldData = this.xmlProcessor.getObjectValue(data, structField.getFieldName());
        switch (category) {
            case PRIMITIVE: {
                PrimitiveObjectInspector primitiveObjectInspector = (PrimitiveObjectInspector) fieldObjectInspector;
                PrimitiveCategory primitiveCategory = primitiveObjectInspector.getPrimitiveCategory();
                return this.xmlProcessor.getPrimitiveObjectValue(fieldData, primitiveCategory);
            }
            default:
                return fieldData;
        }
    }
}
 
Example 4
Source Project: incubator-hivemall   File: ArgrankUDF.java    License: 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 5
Source Project: presto   File: TestSerDeUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static Block toBinaryBlock(io.prestosql.spi.type.Type type, Object object, ObjectInspector inspector)
{
    if (inspector.getCategory() == Category.PRIMITIVE) {
        return getPrimitiveBlock(type, object, inspector);
    }
    return getBlockObject(type, object, inspector);
}
 
Example 6
@Override
public Object getHiveData(AttributeValue data, ObjectInspector objectInspector) {
  Map<String, AttributeValue> dataMap = data.getM();
  if (dataMap == null) {
    return null;
  }
  switch (objectInspector.getCategory()) {
    case MAP:
      return DynamoDBDataParser.getMapObject(dataMap, objectInspector);
    case STRUCT:
      return DynamoDBDataParser.getStructObject(dataMap, objectInspector);
    default:
      throw new IllegalArgumentException("Unsupported Hive type: " + objectInspector.getTypeName());
  }
}
 
Example 7
static void validateCategoryPrimitive(final ObjectInspector inspector, final int index)
    throws UDFArgumentTypeException {
  if (inspector.getCategory() != ObjectInspector.Category.PRIMITIVE) {
    throw new UDFArgumentTypeException(index, "Primitive parameter expected, but "
        + inspector.getCategory().name() + " was recieved as parameter " + (index + 1));
  }
}
 
Example 8
Source Project: incubator-hivemall   File: HiveUtils.java    License: 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 9
private Object parseDispatcher(JsonParser parser, ObjectInspector oi)
        throws JsonParseException, IOException, SerDeException {
    switch (oi.getCategory()) {
        case PRIMITIVE:
            return parsePrimitive(parser, (PrimitiveObjectInspector) oi);
        case LIST:
            return parseList(parser, (ListObjectInspector) oi);
        case STRUCT:
            return parseStruct(parser, (StructObjectInspector) oi);
        case MAP:
            return parseMap(parser, (MapObjectInspector) oi);
        default:
            throw new SerDeException("parsing of: " + oi.getCategory() + " is not handled");
    }
}
 
Example 10
Source Project: pxf   File: HiveORCVectorizedResolver.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<List<OneField>> getFieldsForBatch(OneRow batch) {

    VectorizedRowBatch vectorizedBatch = (VectorizedRowBatch) batch.getData();

    /* Allocate empty result set */
    int columnsNumber = context.getColumns();
    resolvedBatch = new ArrayList<>(vectorizedBatch.size);

    /* Create empty template row */
    ArrayList<OneField> templateRow = new ArrayList<OneField>(columnsNumber);
    ArrayList<OneField> currentRow;
    for (int j = 0; j < context.getColumns(); j++) {
        templateRow.add(null);
    }
    /* Replicate template row*/
    for (int i = 0; i < vectorizedBatch.size; i++) {
        currentRow = new ArrayList<>(templateRow);
        resolvedBatch.add(currentRow);
    }

    /* process all columns*/
    List<? extends StructField> allStructFieldRefs = soi.getAllStructFieldRefs();
    for (int columnIndex = 0; columnIndex < vectorizedBatch.numCols; columnIndex++) {
        ObjectInspector oi = allStructFieldRefs.get(columnIndex).getFieldObjectInspector();
        if (oi.getCategory() == Category.PRIMITIVE) {
            resolvePrimitiveColumn(columnIndex, oi, vectorizedBatch);
        } else {
            throw new UnsupportedTypeException("Unable to resolve column index:" + columnIndex
                    + ". Only primitive types are supported.");
        }
    }

    return resolvedBatch;
}
 
Example 11
static void validateCategoryPrimitive(final ObjectInspector inspector, final int index)
    throws UDFArgumentTypeException {
  if (inspector.getCategory() != ObjectInspector.Category.PRIMITIVE) {
    throw new UDFArgumentTypeException(index, "Primitive parameter expected, but "
        + inspector.getCategory().name() + " was recieved as parameter " + (index + 1));
  }
}
 
Example 12
public static HiveGeometryOIHelper create(ObjectInspector oi, int argIndex) throws UDFArgumentException {
	if (oi.getCategory() != Category.PRIMITIVE) {
		throw new UDFArgumentException("Geometry argument must be a primitive type");
	}
	
	return new HiveGeometryOIHelper(oi, argIndex);
}
 
Example 13
Source Project: incubator-hivemall   File: HiveUtils.java    License: 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 14
static void validateCategoryPrimitive(final ObjectInspector inspector, final int index)
    throws UDFArgumentTypeException {
  if (inspector.getCategory() != ObjectInspector.Category.PRIMITIVE) {
    throw new UDFArgumentTypeException(index, "Primitive parameter expected, but "
        + inspector.getCategory().name() + " was recieved as parameter " + (index + 1));
  }
}
 
Example 15
static void validateCategoryPrimitive(
    final ObjectInspector inspector, final int index) throws SemanticException {
  if (inspector.getCategory() != ObjectInspector.Category.PRIMITIVE) {
    throw new UDFArgumentTypeException(index, "Primitive argument expected, but "
        + inspector.getCategory().name() + " was recieved");
  }
}
 
Example 16
Source Project: dremio-oss   File: HiveUtilities.java    License: Apache License 2.0 5 votes vote down vote up
public static StructObjectInspector getStructOI(final AbstractSerDe serDe) throws Exception {
  ObjectInspector oi = serDe.getObjectInspector();
  if (oi.getCategory() != Category.STRUCT) {
    throw new UnsupportedOperationException(String.format("%s category not supported", oi.getCategory()));
  }
  return (StructObjectInspector) oi;
}
 
Example 17
Source Project: hive-dwrf   File: WriterImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static TreeWriter createTreeWriter(ObjectInspector inspector,
    StreamFactory streamFactory, boolean nullable, Configuration conf, boolean useVInts,
    boolean lowMemoryMode, MemoryEstimate memoryEstimate) throws IOException {
  switch (inspector.getCategory()) {
    case PRIMITIVE:
      switch (((PrimitiveObjectInspector) inspector).getPrimitiveCategory()) {
        case BOOLEAN:
          return new BooleanTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case BYTE:
          return new ByteTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case SHORT:
          return new IntegerTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts , SHORT_BYTE_SIZE,
              lowMemoryMode, memoryEstimate);
        case INT:
          return new IntegerTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts , INT_BYTE_SIZE,
              lowMemoryMode, memoryEstimate);
        case LONG:
          return new IntegerTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts , LONG_BYTE_SIZE,
              lowMemoryMode, memoryEstimate);
        case FLOAT:
          return new FloatTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case DOUBLE:
          return new DoubleTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case STRING:
          return new StringTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case BINARY:
          return new BinaryTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        case TIMESTAMP:
          return new TimestampTreeWriter(streamFactory.getNextColumnId(),
              inspector, streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
        default:
          throw new IllegalArgumentException("Bad primitive category " +
            ((PrimitiveObjectInspector) inspector).getPrimitiveCategory());
      }
    case STRUCT:
      return new StructTreeWriter(streamFactory.getNextColumnId(), inspector,
          streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
    case MAP:
      return new MapTreeWriter(streamFactory.getNextColumnId(), inspector,
          streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
    case LIST:
      return new ListTreeWriter(streamFactory.getNextColumnId(), inspector,
          streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
    case UNION:
      return new UnionTreeWriter(streamFactory.getNextColumnId(), inspector,
          streamFactory, nullable, conf, useVInts, lowMemoryMode, memoryEstimate);
    default:
      throw new IllegalArgumentException("Bad category: " +
        inspector.getCategory());
  }
}
 
Example 18
Source Project: nifi   File: OrcFlowFileWriter.java    License: Apache License 2.0 4 votes vote down vote up
private static TreeWriter createTreeWriter(ObjectInspector inspector,
                                           StreamFactory streamFactory,
                                           boolean nullable) throws IOException {
    switch (inspector.getCategory()) {
        case PRIMITIVE:
            switch (((PrimitiveObjectInspector) inspector).getPrimitiveCategory()) {
                case BOOLEAN:
                    return new BooleanTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case BYTE:
                    return new ByteTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case SHORT:
                case INT:
                case LONG:
                    return new IntegerTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case FLOAT:
                    return new FloatTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case DOUBLE:
                    return new DoubleTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case STRING:
                    return new StringTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case CHAR:
                    return new CharTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case VARCHAR:
                    return new VarcharTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case BINARY:
                    return new BinaryTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case TIMESTAMP:
                    return new TimestampTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case DATE:
                    return new DateTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                case DECIMAL:
                    return new DecimalTreeWriter(streamFactory.getNextColumnId(),
                            inspector, streamFactory, nullable);
                default:
                    throw new IllegalArgumentException("Bad primitive category " +
                            ((PrimitiveObjectInspector) inspector).getPrimitiveCategory());
            }
        case STRUCT:
            return new StructTreeWriter(streamFactory.getNextColumnId(), inspector,
                    streamFactory, nullable);
        case MAP:
            return new MapTreeWriter(streamFactory.getNextColumnId(), inspector,
                    streamFactory, nullable);
        case LIST:
            return new ListTreeWriter(streamFactory.getNextColumnId(), inspector,
                    streamFactory, nullable);
        case UNION:
            return new UnionTreeWriter(streamFactory.getNextColumnId(), inspector,
                    streamFactory, nullable);
        default:
            throw new IllegalArgumentException("Bad category: " +
                    inspector.getCategory());
    }
}
 
Example 19
Source Project: metacat   File: HiveTypeConverter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns the canonical type.
 *
 * @param fieldInspector inspector
 * @return type
 */
Type getCanonicalType(final ObjectInspector fieldInspector) {
    switch (fieldInspector.getCategory()) {
        case PRIMITIVE:
            return getPrimitiveType(fieldInspector);
        case MAP:
            final MapObjectInspector mapObjectInspector =
                TypeUtils.checkType(fieldInspector, MapObjectInspector.class,
                    "fieldInspector");
            final Type keyType = getCanonicalType(mapObjectInspector.getMapKeyObjectInspector());
            final Type valueType = getCanonicalType(mapObjectInspector.getMapValueObjectInspector());
            if (keyType == null || valueType == null) {
                return null;
            }
            return TypeRegistry.getTypeRegistry().getParameterizedType(TypeEnum.MAP,
                ImmutableList.of(keyType.getTypeSignature(), valueType.getTypeSignature()), ImmutableList.of());
        case LIST:
            final ListObjectInspector listObjectInspector =
                TypeUtils.checkType(fieldInspector, ListObjectInspector.class,
                    "fieldInspector");
            final Type elementType =
                getCanonicalType(listObjectInspector.getListElementObjectInspector());
            if (elementType == null) {
                return null;
            }
            return TypeRegistry.getTypeRegistry().getParameterizedType(TypeEnum.ARRAY,
                ImmutableList.of(elementType.getTypeSignature()), ImmutableList.of());
        case STRUCT:
            final StructObjectInspector structObjectInspector =
                TypeUtils.checkType(fieldInspector, StructObjectInspector.class, "fieldInspector");
            final List<TypeSignature> fieldTypes = new ArrayList<>();
            final List<Object> fieldNames = new ArrayList<>();
            for (StructField field : structObjectInspector.getAllStructFieldRefs()) {
                fieldNames.add(field.getFieldName());
                final Type fieldType = getCanonicalType(field.getFieldObjectInspector());
                if (fieldType == null) {
                    return null;
                }
                fieldTypes.add(fieldType.getTypeSignature());
            }
            return TypeRegistry.getTypeRegistry()
                .getParameterizedType(TypeEnum.ROW, fieldTypes, fieldNames);
        default:
            log.info("Currently unsupported type {}, returning Unknown type", fieldInspector.getTypeName());
            return BaseType.UNKNOWN;
    }
}
 
Example 20
Source Project: incubator-hivemall   File: HiveUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isPrimitiveOI(@Nonnull final ObjectInspector oi) {
    return oi.getCategory() == Category.PRIMITIVE;
}