use of org.apache.nifi.record.path.FieldValue 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.FieldValue 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;
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class WildcardIndexPath 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, RecordFieldType.ARRAY)).flatMap(fieldValue -> {
final RecordFieldType fieldType = fieldValue.getField().getDataType().getFieldType();
final Object value = fieldValue.getValue();
if (value == null) {
return Stream.empty();
}
if (fieldType == RecordFieldType.MAP) {
final Map<String, ?> map = (Map<String, ?>) value;
return map.entrySet().stream().map(entry -> {
final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
return new MapEntryFieldValue(entry.getValue(), elementField, fieldValue, entry.getKey());
});
} else {
final Object[] array = (Object[]) value;
return IntStream.range(0, array.length).mapToObj(index -> {
final DataType elementDataType = ((ArrayDataType) fieldValue.getField().getDataType()).getElementType();
final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), elementDataType);
return new ArrayIndexFieldValue(array[index], elementField, fieldValue, index);
});
}
});
}
use of org.apache.nifi.record.path.FieldValue in project nifi by apache.
the class UpdateRecord method getReplacementObject.
private Object getReplacementObject(final List<FieldValue> selectedFields) {
if (selectedFields.size() > 1) {
final List<RecordField> fields = selectedFields.stream().map(FieldValue::getField).collect(Collectors.toList());
final RecordSchema schema = new SimpleRecordSchema(fields);
final Record record = new MapRecord(schema, new HashMap<>());
for (final FieldValue fieldVal : selectedFields) {
record.setValue(fieldVal.getField().getFieldName(), fieldVal.getValue());
}
return record;
}
if (selectedFields.isEmpty()) {
return null;
} else {
return selectedFields.get(0).getValue();
}
}
Aggregations