Search in sources :

Example 6 with RecordPathEvaluationContext

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

the class BinaryOperatorFilter method filter.

@Override
public Stream<FieldValue> filter(final RecordPathEvaluationContext context, final boolean invert) {
    final Stream<FieldValue> rhsStream = rhs.evaluate(context);
    final Optional<FieldValue> firstMatch = rhsStream.filter(fieldVal -> fieldVal.getValue() != null).findFirst();
    if (!firstMatch.isPresent()) {
        return Stream.empty();
    }
    final FieldValue fieldValue = firstMatch.get();
    final Object value = fieldValue.getValue();
    final Stream<FieldValue> lhsStream = lhs.evaluate(context);
    return lhsStream.filter(fieldVal -> {
        final boolean result = test(fieldVal, value);
        return invert ? !result : result;
    });
}
Also used : Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) Optional(java.util.Optional) FieldValue(org.apache.nifi.record.path.FieldValue) FieldValue(org.apache.nifi.record.path.FieldValue)

Example 7 with RecordPathEvaluationContext

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

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

use of org.apache.nifi.record.path.RecordPathEvaluationContext 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)9 RecordPathEvaluationContext (org.apache.nifi.record.path.RecordPathEvaluationContext)9 Stream (java.util.stream.Stream)7 RecordPathSegment (org.apache.nifi.record.path.paths.RecordPathSegment)5 StandardFieldValue (org.apache.nifi.record.path.StandardFieldValue)4 DataTypeUtils (org.apache.nifi.serialization.record.util.DataTypeUtils)4 Optional (java.util.Optional)3 RecordPathUtils (org.apache.nifi.record.path.util.RecordPathUtils)3 RecordField (org.apache.nifi.serialization.record.RecordField)3 Date (java.util.Date)2 ArrayIndexFieldValue (org.apache.nifi.record.path.ArrayIndexFieldValue)2 RecordPathResult (org.apache.nifi.record.path.RecordPathResult)2 StandardRecordPathEvaluationContext (org.apache.nifi.record.path.StandardRecordPathEvaluationContext)2 Filters (org.apache.nifi.record.path.util.Filters)2 DataType (org.apache.nifi.serialization.record.DataType)2 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)2 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 OptionalInt (java.util.OptionalInt)1