Search in sources :

Example 16 with DataType

use of org.apache.nifi.serialization.record.DataType in project nifi by apache.

the class DataTypeUtils method isScalarValue.

public static boolean isScalarValue(final DataType dataType, final Object value) {
    final RecordFieldType fieldType = dataType.getFieldType();
    final RecordFieldType chosenType;
    if (fieldType == RecordFieldType.CHOICE) {
        final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
        final DataType chosenDataType = chooseDataType(value, choiceDataType);
        if (chosenDataType == null) {
            return false;
        }
        chosenType = chosenDataType.getFieldType();
    } else {
        chosenType = fieldType;
    }
    switch(chosenType) {
        case ARRAY:
        case MAP:
        case RECORD:
            return false;
    }
    return true;
}
Also used : 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) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType)

Example 17 with DataType

use of org.apache.nifi.serialization.record.DataType in project nifi by apache.

the class DataTypeUtils method merge.

public static RecordField merge(final RecordField thisField, final RecordField otherField) {
    final String fieldName = thisField.getFieldName();
    final Set<String> aliases = new HashSet<>();
    aliases.addAll(thisField.getAliases());
    aliases.addAll(otherField.getAliases());
    final Object defaultValue;
    if (thisField.getDefaultValue() == null && otherField.getDefaultValue() != null) {
        defaultValue = otherField.getDefaultValue();
    } else {
        defaultValue = thisField.getDefaultValue();
    }
    final DataType dataType;
    if (thisField.getDataType().equals(otherField.getDataType())) {
        dataType = thisField.getDataType();
    } else {
        dataType = RecordFieldType.CHOICE.getChoiceDataType(thisField.getDataType(), otherField.getDataType());
    }
    return new RecordField(fieldName, dataType, defaultValue, aliases, thisField.isNullable() || otherField.isNullable());
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) 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) HashSet(java.util.HashSet)

Example 18 with DataType

use of org.apache.nifi.serialization.record.DataType in project nifi by apache.

the class DataTypeUtils method convertRecordFieldtoObject.

/**
 * Creates a native Java object from a given object of a specified type. Non-scalar (complex, nested, etc.) data types are processed iteratively/recursively, such that all
 * included objects are native Java objects, rather than Record API objects or implementation-specific objects.
 * @param value The object to be converted
 * @param dataType The type of the provided object
 * @return An object representing a native Java conversion of the given input object
 */
public static Object convertRecordFieldtoObject(final Object value, final DataType dataType) {
    if (value == null) {
        return null;
    }
    if (value instanceof Record) {
        Record record = (Record) value;
        RecordSchema recordSchema = record.getSchema();
        if (recordSchema == null) {
            throw new IllegalTypeConversionException("Cannot convert value of type Record to Map because Record does not have an associated Schema");
        }
        final Map<String, Object> recordMap = new HashMap<>();
        for (RecordField field : recordSchema.getFields()) {
            final DataType fieldDataType = field.getDataType();
            final String fieldName = field.getFieldName();
            Object fieldValue = record.getValue(fieldName);
            if (fieldValue == null) {
                recordMap.put(fieldName, null);
            } else if (isScalarValue(fieldDataType, fieldValue)) {
                recordMap.put(fieldName, fieldValue);
            } else if (fieldDataType instanceof RecordDataType) {
                Record nestedRecord = (Record) fieldValue;
                recordMap.put(fieldName, convertRecordFieldtoObject(nestedRecord, fieldDataType));
            } else if (fieldDataType instanceof MapDataType) {
                recordMap.put(fieldName, convertRecordMapToJavaMap((Map) fieldValue, ((MapDataType) fieldDataType).getValueType()));
            } else if (fieldDataType instanceof ArrayDataType) {
                recordMap.put(fieldName, convertRecordArrayToJavaArray((Object[]) fieldValue, ((ArrayDataType) fieldDataType).getElementType()));
            } else {
                throw new IllegalTypeConversionException("Cannot convert value [" + fieldValue + "] of type " + fieldDataType.toString() + " to Map for field " + fieldName + " because the type is not supported");
            }
        }
        return recordMap;
    } else if (value instanceof Map) {
        return convertRecordMapToJavaMap((Map) value, ((MapDataType) dataType).getValueType());
    } else if (dataType != null && isScalarValue(dataType, value)) {
        return value;
    }
    throw new IllegalTypeConversionException("Cannot convert value of class " + value.getClass().getName() + " because the type is not supported");
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) 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) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 19 with DataType

use of org.apache.nifi.serialization.record.DataType in project nifi by apache.

the class ArrayIndexPath method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
    return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).filter(fieldValue -> fieldValue.getValue() != null && ((Object[]) fieldValue.getValue()).length > getArrayIndex(((Object[]) fieldValue.getValue()).length)).map(fieldValue -> {
        final ArrayDataType arrayDataType = (ArrayDataType) fieldValue.getField().getDataType();
        final DataType elementDataType = arrayDataType.getElementType();
        final RecordField arrayField = new RecordField(fieldValue.getField().getFieldName(), elementDataType);
        final Object[] values = (Object[]) fieldValue.getValue();
        final int arrayIndex = getArrayIndex(values.length);
        final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
        final FieldValue result = new ArrayIndexFieldValue(values[arrayIndex], elementField, fieldValue, arrayIndex);
        return result;
    });
}
Also used : Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) Filters(org.apache.nifi.record.path.util.Filters) DataType(org.apache.nifi.serialization.record.DataType) FieldValue(org.apache.nifi.record.path.FieldValue) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordField(org.apache.nifi.serialization.record.RecordField) DataType(org.apache.nifi.serialization.record.DataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue)

Example 20 with DataType

use of org.apache.nifi.serialization.record.DataType in project nifi by apache.

the class SingularMapKeyPath method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
    return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.MAP)).map(fieldValue -> {
        final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
        final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
        return new MapEntryFieldValue(getMapValue(fieldValue), elementField, fieldValue, mapKey);
    });
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) DataType(org.apache.nifi.serialization.record.DataType) FieldValue(org.apache.nifi.record.path.FieldValue) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) MapDataType(org.apache.nifi.serialization.record.type.MapDataType)

Aggregations

DataType (org.apache.nifi.serialization.record.DataType)45 RecordField (org.apache.nifi.serialization.record.RecordField)36 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)27 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)24 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)22 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)22 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)21 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)20 ArrayList (java.util.ArrayList)17 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)17 HashMap (java.util.HashMap)15 Record (org.apache.nifi.serialization.record.Record)14 Map (java.util.Map)13 MapRecord (org.apache.nifi.serialization.record.MapRecord)13 Test (org.junit.Test)13 LinkedHashMap (java.util.LinkedHashMap)11 List (java.util.List)11 ComponentLog (org.apache.nifi.logging.ComponentLog)10 File (java.io.File)9 IOException (java.io.IOException)9