Search in sources :

Example 1 with FieldValue

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

the class UpdateRecord method processRelativePath.

private Record processRelativePath(final RecordPath replacementRecordPath, final Stream<FieldValue> destinationFields, Record record) {
    final List<FieldValue> destinationFieldValues = destinationFields.collect(Collectors.toList());
    for (final FieldValue fieldVal : destinationFieldValues) {
        final RecordPathResult replacementResult = replacementRecordPath.evaluate(record, fieldVal);
        final List<FieldValue> selectedFields = replacementResult.getSelectedFields().collect(Collectors.toList());
        final Object replacementObject = getReplacementObject(selectedFields);
        fieldVal.updateValue(replacementObject);
        record = updateRecord(destinationFieldValues, selectedFields, record);
    }
    return record;
}
Also used : RecordPathResult(org.apache.nifi.record.path.RecordPathResult) FieldValue(org.apache.nifi.record.path.FieldValue)

Example 2 with FieldValue

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

the class UpdateRecord method processAbsolutePath.

private Record processAbsolutePath(final RecordPath replacementRecordPath, final Stream<FieldValue> destinationFields, final Record record) {
    final RecordPathResult replacementResult = replacementRecordPath.evaluate(record);
    final List<FieldValue> selectedFields = replacementResult.getSelectedFields().collect(Collectors.toList());
    final List<FieldValue> destinationFieldValues = destinationFields.collect(Collectors.toList());
    return updateRecord(destinationFieldValues, selectedFields, record);
}
Also used : RecordPathResult(org.apache.nifi.record.path.RecordPathResult) FieldValue(org.apache.nifi.record.path.FieldValue)

Example 3 with FieldValue

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

the class UpdateRecord method updateRecord.

private Record updateRecord(final List<FieldValue> destinationFields, final List<FieldValue> selectedFields, final Record record) {
    if (destinationFields.size() == 1 && !destinationFields.get(0).getParentRecord().isPresent()) {
        final Object replacement = getReplacementObject(selectedFields);
        if (replacement == null) {
            return record;
        }
        if (replacement instanceof Record) {
            return (Record) replacement;
        }
        final List<RecordField> fields = selectedFields.stream().map(FieldValue::getField).collect(Collectors.toList());
        final RecordSchema schema = new SimpleRecordSchema(fields);
        final Record mapRecord = new MapRecord(schema, new HashMap<>());
        for (final FieldValue selectedField : selectedFields) {
            mapRecord.setValue(selectedField.getField().getFieldName(), selectedField.getValue());
        }
        return mapRecord;
    } else {
        for (final FieldValue fieldVal : destinationFields) {
            fieldVal.updateValue(getReplacementObject(selectedFields));
        }
        return record;
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) FieldValue(org.apache.nifi.record.path.FieldValue) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema)

Example 4 with FieldValue

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

the class ContainsRegex method test.

@Override
protected boolean test(final FieldValue fieldValue, final RecordPathEvaluationContext context) {
    final Pattern pattern;
    if (compiledPattern == null) {
        final Optional<FieldValue> fieldValueOption = regexPath.evaluate(context).findFirst();
        if (!fieldValueOption.isPresent()) {
            return false;
        }
        final Object value = fieldValueOption.get().getValue();
        if (value == null) {
            return false;
        }
        final String regex = DataTypeUtils.toString(value, (String) null);
        pattern = Pattern.compile(regex);
    } else {
        pattern = compiledPattern;
    }
    final String searchString = DataTypeUtils.toString(fieldValue.getValue(), (String) null);
    if (searchString == null) {
        return false;
    }
    return pattern.matcher(searchString).find();
}
Also used : Pattern(java.util.regex.Pattern) FieldValue(org.apache.nifi.record.path.FieldValue)

Example 5 with FieldValue

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

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