Search in sources :

Example 6 with StandardFieldValue

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

the class DescendantFieldPath method findDescendants.

private List<FieldValue> findDescendants(final FieldValue fieldValue) {
    if (fieldValue == null || fieldValue.getValue() == null) {
        return Collections.emptyList();
    }
    if (!Filters.isRecord(fieldValue)) {
        return Collections.emptyList();
    }
    final Record record = (Record) fieldValue.getValue();
    final List<FieldValue> matchingValues = new ArrayList<>();
    for (final RecordField childField : record.getSchema().getFields()) {
        if (childField.getFieldName().equals(descendantName) || childField.getAliases().contains(descendantName)) {
            final Object value = record.getValue(descendantName);
            if (value != null) {
                final FieldValue descendantFieldValue = new StandardFieldValue(value, childField, fieldValue);
                matchingValues.add(descendantFieldValue);
            }
        }
        final Object recordValue = record.getValue(childField.getFieldName());
        if (recordValue == null) {
            continue;
        }
        if (Filters.isRecord(childField.getDataType(), recordValue)) {
            final FieldValue childFieldValue = new StandardFieldValue(recordValue, childField, fieldValue);
            matchingValues.addAll(findDescendants(childFieldValue));
        }
    }
    return matchingValues;
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 7 with StandardFieldValue

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

the class ReplaceNull method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final Stream<FieldValue> fieldValues = recordPath.evaluate(context);
    return fieldValues.map(fv -> {
        if (fv.getValue() != null) {
            return fv;
        }
        final Optional<FieldValue> replacementOption = replacementValuePath.evaluate(context).findFirst();
        if (!replacementOption.isPresent()) {
            return fv;
        }
        final FieldValue replacementFieldValue = replacementOption.get();
        final Object replacementValue = replacementFieldValue.getValue();
        if (replacementValue == null) {
            return fv;
        }
        return new StandardFieldValue(replacementValue, fv.getField(), fv.getParent().orElse(null));
    });
}
Also used : StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 8 with StandardFieldValue

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

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

the class RootPath method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    final RecordField field = new RecordField("root", RecordFieldType.RECORD.getRecordDataType(context.getRecord().getSchema()));
    final FieldValue fieldValue = new StandardFieldValue(context.getRecord(), field, null);
    return Stream.of(fieldValue);
}
Also used : 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 10 with StandardFieldValue

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

the class WildcardDescendantPath method findDescendants.

private List<FieldValue> findDescendants(final FieldValue fieldValue) {
    if (fieldValue == null || fieldValue.getValue() == null) {
        return Collections.emptyList();
    }
    if (!Filters.isRecord(fieldValue)) {
        return Collections.emptyList();
    }
    final Record record = (Record) fieldValue.getValue();
    final List<FieldValue> matchingValues = new ArrayList<>();
    for (final RecordField childField : record.getSchema().getFields()) {
        final Object value = record.getValue(childField);
        if (value == null) {
            continue;
        }
        final FieldValue descendantFieldValue = new StandardFieldValue(value, childField, fieldValue);
        matchingValues.add(descendantFieldValue);
        if (Filters.isRecord(childField.getDataType(), value)) {
            final FieldValue childFieldValue = new StandardFieldValue(value, childField, fieldValue);
            matchingValues.addAll(findDescendants(childFieldValue));
        }
    }
    return matchingValues;
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

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