Search in sources :

Example 6 with ChoiceDataType

use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.

the class StandardSchemaValidator method getCanonicalDataType.

private DataType getCanonicalDataType(final DataType dataType, final Object rawValue, final StandardSchemaValidationResult result, final String fieldPrefix, final RecordField field) {
    final RecordFieldType fieldType = dataType.getFieldType();
    final DataType canonicalDataType;
    if (fieldType == RecordFieldType.CHOICE) {
        canonicalDataType = DataTypeUtils.chooseDataType(rawValue, (ChoiceDataType) dataType);
        if (canonicalDataType == null) {
            result.addValidationError(new StandardValidationError(concat(fieldPrefix, field), rawValue, ValidationErrorType.INVALID_FIELD, "Value is of type " + rawValue.getClass().getName() + " but was expected to be of type " + dataType));
            return null;
        }
    } else {
        canonicalDataType = dataType;
    }
    return canonicalDataType;
}
Also used : RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType)

Example 7 with ChoiceDataType

use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.

the class StandardSchemaValidator method isTypeCorrect.

private boolean isTypeCorrect(final Object value, final DataType dataType) {
    switch(dataType.getFieldType()) {
        case ARRAY:
            if (!(value instanceof Object[])) {
                return false;
            }
            final ArrayDataType arrayDataType = (ArrayDataType) dataType;
            final DataType elementType = arrayDataType.getElementType();
            final Object[] array = (Object[]) value;
            for (final Object arrayVal : array) {
                if (!isTypeCorrect(arrayVal, elementType)) {
                    return false;
                }
            }
            return true;
        case MAP:
            if (!(value instanceof Map)) {
                return false;
            }
            final MapDataType mapDataType = (MapDataType) dataType;
            final DataType valueDataType = mapDataType.getValueType();
            final Map<?, ?> map = (Map<?, ?>) value;
            for (final Object mapValue : map.values()) {
                if (!isTypeCorrect(mapValue, valueDataType)) {
                    return false;
                }
            }
            return true;
        case RECORD:
            return value instanceof Record;
        case CHOICE:
            final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
            for (final DataType choice : choiceDataType.getPossibleSubTypes()) {
                if (isTypeCorrect(value, choice)) {
                    return true;
                }
            }
            return false;
        case BIGINT:
            return value instanceof BigInteger;
        case BOOLEAN:
            return value instanceof Boolean;
        case BYTE:
            return value instanceof Byte;
        case CHAR:
            return value instanceof Character;
        case DATE:
            return value instanceof java.sql.Date;
        case DOUBLE:
            return value instanceof Double;
        case FLOAT:
            // Same goes for Short/Integer
            return value instanceof Float;
        case INT:
            return value instanceof Integer;
        case LONG:
            return value instanceof Long;
        case SHORT:
            return value instanceof Short;
        case STRING:
            return value instanceof String;
        case TIME:
            return value instanceof java.sql.Time;
        case TIMESTAMP:
            return value instanceof java.sql.Timestamp;
    }
    return false;
}
Also used : MapDataType(org.apache.nifi.serialization.record.type.MapDataType) BigInteger(java.math.BigInteger) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) BigInteger(java.math.BigInteger) Record(org.apache.nifi.serialization.record.Record) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) Map(java.util.Map)

Example 8 with ChoiceDataType

use of org.apache.nifi.serialization.record.type.ChoiceDataType in project nifi by apache.

the class DataTypeUtils method convertType.

public static Object convertType(final Object value, final DataType dataType, final Supplier<DateFormat> dateFormat, final Supplier<DateFormat> timeFormat, final Supplier<DateFormat> timestampFormat, final String fieldName) {
    if (value == null) {
        return null;
    }
    switch(dataType.getFieldType()) {
        case BIGINT:
            return toBigInt(value, fieldName);
        case BOOLEAN:
            return toBoolean(value, fieldName);
        case BYTE:
            return toByte(value, fieldName);
        case CHAR:
            return toCharacter(value, fieldName);
        case DATE:
            return toDate(value, dateFormat, fieldName);
        case DOUBLE:
            return toDouble(value, fieldName);
        case FLOAT:
            return toFloat(value, fieldName);
        case INT:
            return toInteger(value, fieldName);
        case LONG:
            return toLong(value, fieldName);
        case SHORT:
            return toShort(value, fieldName);
        case STRING:
            return toString(value, () -> getDateFormat(dataType.getFieldType(), dateFormat, timeFormat, timestampFormat));
        case TIME:
            return toTime(value, timeFormat, fieldName);
        case TIMESTAMP:
            return toTimestamp(value, timestampFormat, fieldName);
        case ARRAY:
            return toArray(value, fieldName);
        case MAP:
            return toMap(value, fieldName);
        case RECORD:
            final RecordDataType recordType = (RecordDataType) dataType;
            final RecordSchema childSchema = recordType.getChildSchema();
            return toRecord(value, childSchema, fieldName);
        case CHOICE:
            {
                final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
                final DataType chosenDataType = chooseDataType(value, choiceDataType);
                if (chosenDataType == null) {
                    throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " for field " + fieldName + " to any of the following available Sub-Types for a Choice: " + choiceDataType.getPossibleSubTypes());
                }
                return convertType(value, chosenDataType, fieldName);
            }
    }
    return null;
}
Also used : RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Aggregations

DataType (org.apache.nifi.serialization.record.DataType)8 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)8 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)7 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)7 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)7 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)5 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)5 Map (java.util.Map)4 Record (org.apache.nifi.serialization.record.Record)4 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)3 RecordField (org.apache.nifi.serialization.record.RecordField)3 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 ComponentLog (org.apache.nifi.logging.ComponentLog)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1