use of org.apache.nifi.record.path.FieldValue 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));
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class MultiArrayIndexPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).flatMap(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();
return indices.stream().filter(range -> values.length > Math.abs(range.getMin())).flatMap(range -> {
final int min = range.getMin() < 0 ? values.length + range.getMin() : range.getMin();
final int max = range.getMax() < 0 ? values.length + range.getMax() : range.getMax();
final List<FieldValue> indexFieldValues = new ArrayList<>(Math.max(0, max - min));
for (int i = min; i <= max; i++) {
if (values.length > i) {
final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
final FieldValue arrayIndexFieldValue = new ArrayIndexFieldValue(values[i], elementField, fieldValue, i);
indexFieldValues.add(arrayIndexFieldValue);
}
}
return indexFieldValues.stream();
});
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class MultiMapKeyPath method evaluate.
@Override
@SuppressWarnings("unchecked")
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.MAP)).flatMap(fieldValue -> {
final Map<String, ?> map = (Map<String, ?>) fieldValue.getValue();
return mapKeys.stream().map(key -> {
final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
return new MapEntryFieldValue(map.get(key), elementField, fieldValue, key);
});
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class PredicatePath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> valueStream = getParentPath().evaluate(context);
return valueStream.flatMap(fieldVal -> {
// For the duration of this Predicate, we want to consider the 'context node' to be
// whatever value is given to us in the field value. We then want to return the 'context node'
// back to what it was before this Predicate.
final FieldValue previousContextNode = context.getContextNode();
context.setContextNode(fieldVal);
try {
// times, we will limit the stream to 1 element.
return filter.filter(context, false).limit(1).map(ignore -> fieldVal);
} finally {
context.setContextNode(previousContextNode);
}
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class RecordPathSegment method evaluate.
@Override
public final RecordPathResult evaluate(final Record record, final FieldValue contextNode) {
final RecordPathEvaluationContext context = new StandardRecordPathEvaluationContext(record);
context.setContextNode(contextNode);
final Stream<FieldValue> selectedFields = evaluate(context);
return new RecordPathResult() {
@Override
public String getPath() {
return RecordPathSegment.this.getPath();
}
@Override
public Stream<FieldValue> getSelectedFields() {
return selectedFields;
}
};
}
Aggregations