Search in sources :

Example 1 with RecordPathEvaluationContext

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

the class Format 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 java.text.DateFormat dateFormat = getDateFormat(this.dateFormat, context);
        if (dateFormat == null) {
            return fv;
        }
        if (!(fv.getValue() instanceof Date) && !(fv.getValue() instanceof Number)) {
            return fv;
        }
        final Date dateValue = DataTypeUtils.toDate(fv.getValue(), null, fv.getField().getFieldName());
        final String formatted = dateFormat.format(dateValue);
        return new StandardFieldValue(formatted, fv.getField(), fv.getParent().orElse(null));
    });
}
Also used : RecordPathUtils(org.apache.nifi.record.path.util.RecordPathUtils) Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) Date(java.util.Date) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) FieldValue(org.apache.nifi.record.path.FieldValue) 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) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) Date(java.util.Date)

Example 2 with RecordPathEvaluationContext

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

the class ReplaceRegex 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 String value = DataTypeUtils.toString(fv.getValue(), (String) null);
        // Determine the Replacement Value
        final String replacementValue = RecordPathUtils.getFirstStringValue(replacementValuePath, context);
        if (replacementValue == null) {
            return fv;
        }
        final Pattern pattern;
        if (compiledPattern == null) {
            final Optional<FieldValue> fieldValueOption = searchValuePath.evaluate(context).findFirst();
            if (!fieldValueOption.isPresent()) {
                return fv;
            }
            final Object fieldValue = fieldValueOption.get().getValue();
            if (value == null) {
                return fv;
            }
            final String regex = DataTypeUtils.toString(fieldValue, (String) null);
            pattern = Pattern.compile(regex);
        } else {
            pattern = compiledPattern;
        }
        final String replaced = pattern.matcher(value).replaceAll(replacementValue);
        return new StandardFieldValue(replaced, fv.getField(), fv.getParent().orElse(null));
    });
}
Also used : Stream(java.util.stream.Stream) RecordPathUtils(org.apache.nifi.record.path.util.RecordPathUtils) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) LiteralValuePath(org.apache.nifi.record.path.paths.LiteralValuePath) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) Pattern(java.util.regex.Pattern) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 3 with RecordPathEvaluationContext

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

the class ToDate method evaluate.

@Override
public Stream<FieldValue> evaluate(RecordPathEvaluationContext context) {
    final Stream<FieldValue> fieldValues = recordPath.evaluate(context);
    return fieldValues.filter(fv -> fv.getValue() != null).map(fv -> {
        if (!(fv.getValue() instanceof String)) {
            return fv;
        }
        final java.text.DateFormat dateFormat = getDateFormat(this.dateFormat, context);
        final Date dateValue;
        try {
            dateValue = DataTypeUtils.toDate(fv.getValue(), () -> dateFormat, fv.getField().getFieldName());
        } catch (final Exception e) {
            return fv;
        }
        if (dateValue == null) {
            return fv;
        }
        return new StandardFieldValue(dateValue, fv.getField(), fv.getParent().orElse(null));
    });
}
Also used : RecordPathUtils(org.apache.nifi.record.path.util.RecordPathUtils) Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) Date(java.util.Date) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) FieldValue(org.apache.nifi.record.path.FieldValue) 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) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) Date(java.util.Date)

Example 4 with RecordPathEvaluationContext

use of org.apache.nifi.record.path.RecordPathEvaluationContext 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 5 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 RecordPathEvaluationContext context = new StandardRecordPathEvaluationContext(record);
    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