use of org.apache.nifi.record.path.ArrayIndexFieldValue 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.record.path.ArrayIndexFieldValue 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.ArrayIndexFieldValue 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);
});
}
});
}
Aggregations