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;
}
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));
});
}
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));
});
}
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);
}
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;
}
Aggregations