Search in sources :

Example 11 with RecordDataType

use of org.apache.nifi.serialization.record.type.RecordDataType 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)

Example 12 with RecordDataType

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

the class JsonPathRowRecordReader method convert.

@SuppressWarnings("unchecked")
protected Object convert(final Object value, final DataType dataType, final String fieldName, final Object defaultValue) {
    if (value == null) {
        return defaultValue;
    }
    if (value instanceof List) {
        if (dataType.getFieldType() != RecordFieldType.ARRAY) {
            throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type Array to " + dataType);
        }
        final ArrayDataType arrayType = (ArrayDataType) dataType;
        final List<?> list = (List<?>) value;
        final Object[] coercedValues = new Object[list.size()];
        int i = 0;
        for (final Object rawValue : list) {
            coercedValues[i++] = convert(rawValue, arrayType.getElementType(), fieldName, null);
        }
        return coercedValues;
    }
    if (dataType.getFieldType() == RecordFieldType.RECORD && value instanceof Map) {
        final RecordDataType recordDataType = (RecordDataType) dataType;
        final RecordSchema childSchema = recordDataType.getChildSchema();
        final Map<String, Object> rawValues = (Map<String, Object>) value;
        final Map<String, Object> coercedValues = new HashMap<>();
        for (final Map.Entry<String, Object> entry : rawValues.entrySet()) {
            final String key = entry.getKey();
            final Optional<DataType> desiredTypeOption = childSchema.getDataType(key);
            if (desiredTypeOption.isPresent()) {
                final Optional<RecordField> field = childSchema.getField(key);
                final Object defaultFieldValue = field.isPresent() ? field.get().getDefaultValue() : null;
                final Object coercedValue = convert(entry.getValue(), desiredTypeOption.get(), fieldName + "." + key, defaultFieldValue);
                coercedValues.put(key, coercedValue);
            }
        }
        return new MapRecord(childSchema, coercedValues);
    } else {
        return DataTypeUtils.convertType(value, dataType, LAZY_DATE_FORMAT, LAZY_TIME_FORMAT, LAZY_TIMESTAMP_FORMAT, fieldName);
    }
}
Also used : MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) DataType(org.apache.nifi.serialization.record.DataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) List(java.util.List) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Aggregations

RecordSchema (org.apache.nifi.serialization.record.RecordSchema)12 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)12 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)9 DataType (org.apache.nifi.serialization.record.DataType)8 RecordField (org.apache.nifi.serialization.record.RecordField)8 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)8 Map (java.util.Map)5 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)5 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)5 HashMap (java.util.HashMap)4 Schema (org.apache.avro.Schema)4 MapRecord (org.apache.nifi.serialization.record.MapRecord)4 Record (org.apache.nifi.serialization.record.Record)4 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 List (java.util.List)2 GenericData (org.apache.avro.generic.GenericData)2 Record (org.apache.avro.generic.GenericData.Record)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)2