Search in sources :

Example 21 with FieldValue

use of org.apache.nifi.record.path.FieldValue in project nifi by apache.

the class Substring method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> fieldValues = recordPath.evaluate(context);
    return fieldValues.filter(fv -> fv.getValue() != null).map(fv -> {
        final OptionalInt startIndex = getIndex(startIndexPath, context);
        if (!startIndex.isPresent()) {
            return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
        }
        final OptionalInt endIndex = getIndex(endIndexPath, context);
        if (!endIndex.isPresent()) {
            return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
        }
        final int start = startIndex.getAsInt();
        final int end = endIndex.getAsInt();
        final String value = DataTypeUtils.toString(fv.getValue(), (String) null);
        // Allow for negative indices to be used to reference offset from string length. We add 1 here because we want -1 to refer
        // to the actual length of the string.
        final int evaluatedEndIndex = end < 0 ? value.length() + 1 + end : end;
        final int evaluatedStartIndex = start < 0 ? value.length() + 1 + start : start;
        if (evaluatedEndIndex <= evaluatedStartIndex || evaluatedStartIndex < 0 || evaluatedStartIndex > value.length()) {
            return new StandardFieldValue("", fv.getField(), fv.getParent().orElse(null));
        }
        final String substring = value.substring(evaluatedStartIndex, Math.min(evaluatedEndIndex, value.length()));
        return new StandardFieldValue(substring, fv.getField(), fv.getParent().orElse(null));
    });
}
Also used : Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) RecordField(org.apache.nifi.serialization.record.RecordField) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) Optional(java.util.Optional) FieldValue(org.apache.nifi.record.path.FieldValue) OptionalInt(java.util.OptionalInt) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) OptionalInt(java.util.OptionalInt)

Example 22 with FieldValue

use of org.apache.nifi.record.path.FieldValue 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 23 with FieldValue

use of org.apache.nifi.record.path.FieldValue 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 24 with FieldValue

use of org.apache.nifi.record.path.FieldValue in project nifi by apache.

the class PredicatePath method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> valueStream = getParentPath().evaluate(context);
    return valueStream.flatMap(fieldVal -> {
        // For the duration of this Predicate, we want to consider the 'context node' to be
        // whatever value is given to us in the field value. We then want to return the 'context node'
        // back to what it was before this Predicate.
        final FieldValue previousContextNode = context.getContextNode();
        context.setContextNode(fieldVal);
        try {
            // times, we will limit the stream to 1 element.
            return filter.filter(context, false).limit(1).map(ignore -> fieldVal);
        } finally {
            context.setContextNode(previousContextNode);
        }
    });
}
Also used : FieldValue(org.apache.nifi.record.path.FieldValue)

Example 25 with FieldValue

use of org.apache.nifi.record.path.FieldValue in project nifi by apache.

the class RecordPathSegment method evaluate.

@Override
public final RecordPathResult evaluate(final Record record, final FieldValue contextNode) {
    final RecordPathEvaluationContext context = new StandardRecordPathEvaluationContext(record);
    context.setContextNode(contextNode);
    final Stream<FieldValue> selectedFields = evaluate(context);
    return new RecordPathResult() {

        @Override
        public String getPath() {
            return RecordPathSegment.this.getPath();
        }

        @Override
        public Stream<FieldValue> getSelectedFields() {
            return selectedFields;
        }
    };
}
Also used : RecordPathResult(org.apache.nifi.record.path.RecordPathResult) FieldValue(org.apache.nifi.record.path.FieldValue) StandardRecordPathEvaluationContext(org.apache.nifi.record.path.StandardRecordPathEvaluationContext) StandardRecordPathEvaluationContext(org.apache.nifi.record.path.StandardRecordPathEvaluationContext) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext)

Aggregations

FieldValue (org.apache.nifi.record.path.FieldValue)29 RecordField (org.apache.nifi.serialization.record.RecordField)13 StandardFieldValue (org.apache.nifi.record.path.StandardFieldValue)10 RecordPathEvaluationContext (org.apache.nifi.record.path.RecordPathEvaluationContext)9 Stream (java.util.stream.Stream)8 Record (org.apache.nifi.serialization.record.Record)7 DataTypeUtils (org.apache.nifi.serialization.record.util.DataTypeUtils)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 DataType (org.apache.nifi.serialization.record.DataType)5 Optional (java.util.Optional)4 RecordPathSegment (org.apache.nifi.record.path.paths.RecordPathSegment)4 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)4 List (java.util.List)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)3 FlowFile (org.apache.nifi.flowfile.FlowFile)3 ProcessException (org.apache.nifi.processor.exception.ProcessException)3 RecordPath (org.apache.nifi.record.path.RecordPath)3 RecordPathResult (org.apache.nifi.record.path.RecordPathResult)3 RecordPathUtils (org.apache.nifi.record.path.util.RecordPathUtils)3