Search in sources :

Example 31 with DataType

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

the class MultiArrayIndexPath method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
    return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).flatMap(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();
        return indices.stream().filter(range -> values.length > Math.abs(range.getMin())).flatMap(range -> {
            final int min = range.getMin() < 0 ? values.length + range.getMin() : range.getMin();
            final int max = range.getMax() < 0 ? values.length + range.getMax() : range.getMax();
            final List<FieldValue> indexFieldValues = new ArrayList<>(Math.max(0, max - min));
            for (int i = min; i <= max; i++) {
                if (values.length > i) {
                    final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
                    final FieldValue arrayIndexFieldValue = new ArrayIndexFieldValue(values[i], elementField, fieldValue, i);
                    indexFieldValues.add(arrayIndexFieldValue);
                }
            }
            return indexFieldValues.stream();
        });
    });
}
Also used : List(java.util.List) 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) NumericRange(org.apache.nifi.record.path.NumericRange) 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) ArrayList(java.util.ArrayList) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) 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 32 with DataType

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

the class MultiMapKeyPath 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)).flatMap(fieldValue -> {
        final Map<String, ?> map = (Map<String, ?>) fieldValue.getValue();
        return mapKeys.stream().map(key -> {
            final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
            final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
            return new MapEntryFieldValue(map.get(key), elementField, fieldValue, key);
        });
    });
}
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) Map(java.util.Map)

Example 33 with DataType

use of org.apache.nifi.serialization.record.DataType 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 34 with DataType

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

the class Filters method isRecord.

public static boolean isRecord(final FieldValue fieldValue) {
    final DataType dataType = fieldValue.getField().getDataType();
    final Object value = fieldValue.getValue();
    return isRecord(dataType, value);
}
Also used : DataType(org.apache.nifi.serialization.record.DataType)

Example 35 with DataType

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

the class TestRecordPath method getDefaultFields.

private List<RecordField> getDefaultFields() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("attributes", RecordFieldType.MAP.getMapDataType(RecordFieldType.STRING.getDataType())));
    fields.add(new RecordField("mainAccount", RecordFieldType.RECORD.getRecordDataType(getAccountSchema())));
    fields.add(new RecordField("numbers", RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.INT.getDataType())));
    final DataType accountDataType = RecordFieldType.RECORD.getRecordDataType(getAccountSchema());
    final DataType accountsType = RecordFieldType.ARRAY.getArrayDataType(accountDataType);
    final RecordField accountsField = new RecordField("accounts", accountsType);
    fields.add(accountsField);
    return fields;
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) DataType(org.apache.nifi.serialization.record.DataType)

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