use of org.apache.nifi.serialization.record.RecordField 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.serialization.record.RecordField 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;
});
}
use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.
the class ChildFieldPath method getChild.
private FieldValue getChild(final FieldValue fieldValue) {
if (!Filters.isRecord(fieldValue)) {
return missingChild(fieldValue);
}
final Record record = (Record) fieldValue.getValue();
if (record == null) {
return missingChild(fieldValue);
}
final Object value = record.getValue(childName);
if (value == null) {
return missingChild(fieldValue);
}
final Optional<RecordField> field = record.getSchema().getField(childName);
if (!field.isPresent()) {
return missingChild(fieldValue);
}
return new StandardFieldValue(value, field.get(), fieldValue);
}
use of org.apache.nifi.serialization.record.RecordField 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.serialization.record.RecordField in project nifi by apache.
the class SingularMapKeyPath method evaluate.
@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
final Stream<FieldValue> parentResult = getParentPath().evaluate(context);
return parentResult.filter(Filters.fieldTypeFilter(RecordFieldType.MAP)).map(fieldValue -> {
final DataType valueType = ((MapDataType) fieldValue.getField().getDataType()).getValueType();
final RecordField elementField = new RecordField(fieldValue.getField().getFieldName(), valueType);
return new MapEntryFieldValue(getMapValue(fieldValue), elementField, fieldValue, mapKey);
});
}
Aggregations