Search in sources :

Example 1 with StandardFieldValue

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

the class Concat method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    Stream<FieldValue> concatenated = Stream.empty();
    for (final RecordPathSegment valuePath : valuePaths) {
        final Stream<FieldValue> stream = valuePath.evaluate(context);
        concatenated = Stream.concat(concatenated, stream);
    }
    final StringBuilder sb = new StringBuilder();
    concatenated.forEach(fv -> sb.append(DataTypeUtils.toString(fv.getValue(), (String) null)));
    final RecordField field = new RecordField("concat", RecordFieldType.STRING.getDataType());
    final FieldValue responseValue = new StandardFieldValue(sb.toString(), field, null);
    return Stream.of(responseValue);
}
Also used : RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 2 with StandardFieldValue

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

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

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

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

the class ChildFieldPath method getChild.

private FieldValue getChild(final FieldValue fieldValue) {
    if (!Filters.isRecord(fieldValue)) {
        return missingChild(fieldValue);
    }
    final Record record = (Record) fieldValue.getValue();
    if (record == null) {
        return missingChild(fieldValue);
    }
    final Object value = record.getValue(childName);
    if (value == null) {
        return missingChild(fieldValue);
    }
    final Optional<RecordField> field = record.getSchema().getField(childName);
    if (!field.isPresent()) {
        return missingChild(fieldValue);
    }
    return new StandardFieldValue(value, field.get(), fieldValue);
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) Record(org.apache.nifi.serialization.record.Record)

Aggregations

StandardFieldValue (org.apache.nifi.record.path.StandardFieldValue)10 FieldValue (org.apache.nifi.record.path.FieldValue)9 RecordField (org.apache.nifi.serialization.record.RecordField)6 RecordPathSegment (org.apache.nifi.record.path.paths.RecordPathSegment)5 Stream (java.util.stream.Stream)4 RecordPathEvaluationContext (org.apache.nifi.record.path.RecordPathEvaluationContext)4 DataTypeUtils (org.apache.nifi.serialization.record.util.DataTypeUtils)4 RecordPathUtils (org.apache.nifi.record.path.util.RecordPathUtils)3 Record (org.apache.nifi.serialization.record.Record)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Optional (java.util.Optional)2 OptionalInt (java.util.OptionalInt)1 Pattern (java.util.regex.Pattern)1 LiteralValuePath (org.apache.nifi.record.path.paths.LiteralValuePath)1