use of org.apache.nifi.record.path.FieldValue 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));
});
}
use of org.apache.nifi.record.path.FieldValue 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));
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class Substring method getIndex.
private OptionalInt getIndex(final RecordPathSegment indexSegment, final RecordPathEvaluationContext context) {
final Optional<FieldValue> firstFieldValueOption = indexSegment.evaluate(context).findFirst();
if (!firstFieldValueOption.isPresent()) {
return OptionalInt.empty();
}
final FieldValue fieldValue = firstFieldValueOption.get();
final Object indexObject = fieldValue.getValue();
if (!DataTypeUtils.isIntegerTypeCompatible(indexObject)) {
return OptionalInt.empty();
}
final String fieldName;
final RecordField field = fieldValue.getField();
fieldName = field == null ? "<Unknown Field>" : field.getFieldName();
return OptionalInt.of(DataTypeUtils.toInteger(indexObject, fieldName));
}
use of org.apache.nifi.record.path.FieldValue 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));
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class ArrayIndexPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)).filter(fieldValue -> fieldValue.getValue() != null && ((Object[]) fieldValue.getValue()).length > getArrayIndex(((Object[]) fieldValue.getValue()).length)).map(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();
final int arrayIndex = getArrayIndex(values.length);
final RecordField elementField = new RecordField(arrayField.getFieldName(), elementDataType);
final FieldValue result = new ArrayIndexFieldValue(values[arrayIndex], elementField, fieldValue, arrayIndex);
return result;
});
}
Aggregations