Search in sources :

Example 6 with FieldValue

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

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

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

the class Substring method getIndex.

private OptionalInt getIndex(final RecordPathSegment indexSegment, final RecordPathEvaluationContext context) {
    final Optional<FieldValue> firstFieldValueOption = indexSegment.evaluate(context).findFirst();
    if (!firstFieldValueOption.isPresent()) {
        return OptionalInt.empty();
    }
    final FieldValue fieldValue = firstFieldValueOption.get();
    final Object indexObject = fieldValue.getValue();
    if (!DataTypeUtils.isIntegerTypeCompatible(indexObject)) {
        return OptionalInt.empty();
    }
    final String fieldName;
    final RecordField field = fieldValue.getField();
    fieldName = field == null ? "<Unknown Field>" : field.getFieldName();
    return OptionalInt.of(DataTypeUtils.toInteger(indexObject, fieldName));
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 9 with FieldValue

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

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

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