Search in sources :

Example 11 with ArrayDataType

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

the class WildcardIndexPath method evaluate.

@Override
@SuppressWarnings("unchecked")
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
    return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.MAP, RecordFieldType.ARRAY)).flatMap(fieldValue -> {
        final RecordFieldType fieldType = fieldValue.getField().getDataType().getFieldType();
        final Object value = fieldValue.getValue();
        if (value == null) {
            return Stream.empty();
        }
        if (fieldType == RecordFieldType.MAP) {
            final Map<String, ?> map = (Map<String, ?>) value;
            return map.entrySet().stream().map(entry -> {
                final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
                final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
                return new MapEntryFieldValue(entry.getValue(), elementField, fieldValue, entry.getKey());
            });
        } else {
            final Object[] array = (Object[]) value;
            return IntStream.range(0, array.length).mapToObj(index -> {
                final DataType elementDataType = ((ArrayDataType) fieldValue.getField().getDataType()).getElementType();
                final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), elementDataType);
                return new ArrayIndexFieldValue(array[index], elementField, fieldValue, index);
            });
        }
    });
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) DataType(org.apache.nifi.serialization.record.DataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Map(java.util.Map)

Example 12 with ArrayDataType

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

the class MapRecord method setArrayValue.

@Override
public void setArrayValue(final String fieldName, final int arrayIndex, final Object value) {
    final Optional<RecordField> field = getSchema().getField(fieldName);
    if (!field.isPresent()) {
        return;
    }
    final RecordField recordField = field.get();
    final DataType dataType = recordField.getDataType();
    if (dataType.getFieldType() != RecordFieldType.ARRAY) {
        throw new IllegalTypeConversionException("Cannot set the value of an array index on Record because the field '" + fieldName + "' is of type '" + dataType + "' and cannot be coerced into an ARRAY type");
    }
    final Object arrayObject = values.get(recordField.getFieldName());
    if (arrayObject == null) {
        return;
    }
    if (!(arrayObject instanceof Object[])) {
        return;
    }
    final Object[] array = (Object[]) arrayObject;
    if (arrayIndex >= array.length) {
        return;
    }
    final ArrayDataType arrayDataType = (ArrayDataType) dataType;
    final DataType elementType = arrayDataType.getElementType();
    final Object coerced = DataTypeUtils.convertType(value, elementType, fieldName);
    final boolean update = !Objects.equals(coerced, array[arrayIndex]);
    if (update) {
        array[arrayIndex] = coerced;
        serializedForm = Optional.empty();
    }
}
Also used : IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType)

Example 13 with ArrayDataType

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

ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)13 DataType (org.apache.nifi.serialization.record.DataType)12 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)9 RecordField (org.apache.nifi.serialization.record.RecordField)8 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)8 Map (java.util.Map)7 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)7 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)6 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)5 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)5 HashMap (java.util.HashMap)4 MapRecord (org.apache.nifi.serialization.record.MapRecord)4 Record (org.apache.nifi.serialization.record.Record)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ArrayIndexFieldValue (org.apache.nifi.record.path.ArrayIndexFieldValue)3 FieldValue (org.apache.nifi.record.path.FieldValue)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Stream (java.util.stream.Stream)2