Search in sources :

Example 66 with RecordField

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));
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 67 with RecordField

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;
    });
}
Also used : Stream(java.util.stream.Stream) RecordPathEvaluationContext(org.apache.nifi.record.path.RecordPathEvaluationContext) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) Filters(org.apache.nifi.record.path.util.Filters) DataType(org.apache.nifi.serialization.record.DataType) FieldValue(org.apache.nifi.record.path.FieldValue) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordField(org.apache.nifi.serialization.record.RecordField) DataType(org.apache.nifi.serialization.record.DataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayIndexFieldValue(org.apache.nifi.record.path.ArrayIndexFieldValue)

Example 68 with RecordField

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);
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) Record(org.apache.nifi.serialization.record.Record)

Example 69 with RecordField

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;
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

Example 70 with RecordField

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);
    });
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) DataType(org.apache.nifi.serialization.record.DataType) FieldValue(org.apache.nifi.record.path.FieldValue) MapEntryFieldValue(org.apache.nifi.record.path.MapEntryFieldValue) MapDataType(org.apache.nifi.serialization.record.type.MapDataType)

Aggregations

RecordField (org.apache.nifi.serialization.record.RecordField)173 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)133 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)130 ArrayList (java.util.ArrayList)116 Test (org.junit.Test)108 Record (org.apache.nifi.serialization.record.Record)97 MapRecord (org.apache.nifi.serialization.record.MapRecord)73 HashMap (java.util.HashMap)52 InputStream (java.io.InputStream)48 FileInputStream (java.io.FileInputStream)44 ByteArrayInputStream (java.io.ByteArrayInputStream)43 ComponentLog (org.apache.nifi.logging.ComponentLog)39 DataType (org.apache.nifi.serialization.record.DataType)37 LinkedHashMap (java.util.LinkedHashMap)36 File (java.io.File)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)17 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)17 Schema (org.apache.avro.Schema)16 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)16