Search in sources :

Example 6 with ArrayDataType

use of org.apache.nifi.serialization.record.type.ArrayDataType 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 7 with ArrayDataType

use of org.apache.nifi.serialization.record.type.ArrayDataType in project nifi by apache.

the class AvroTypeUtil method buildAvroSchema.

private static Schema buildAvroSchema(final DataType dataType, final String fieldName, final boolean nullable) {
    final Schema schema;
    switch(dataType.getFieldType()) {
        case ARRAY:
            final ArrayDataType arrayDataType = (ArrayDataType) dataType;
            final DataType elementDataType = arrayDataType.getElementType();
            if (RecordFieldType.BYTE.equals(elementDataType.getFieldType())) {
                schema = Schema.create(Type.BYTES);
            } else {
                final Schema elementType = buildAvroSchema(elementDataType, fieldName, false);
                schema = Schema.createArray(elementType);
            }
            break;
        case BIGINT:
            schema = Schema.create(Type.STRING);
            break;
        case BOOLEAN:
            schema = Schema.create(Type.BOOLEAN);
            break;
        case BYTE:
            schema = Schema.create(Type.INT);
            break;
        case CHAR:
            schema = Schema.create(Type.STRING);
            break;
        case CHOICE:
            final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
            final List<DataType> options = choiceDataType.getPossibleSubTypes();
            // We need to keep track of which types have been added to the union, because if we have
            // two elements in the UNION with the same type, it will fail - even if the logical type is
            // different. So if we have an int and a logical type date (which also has a 'concrete type' of int)
            // then an Exception will be thrown when we try to create the union. To avoid this, we just keep track
            // of the Types and avoid adding it in such a case.
            final List<Schema> unionTypes = new ArrayList<>(options.size());
            final Set<Type> typesAdded = new HashSet<>();
            for (final DataType option : options) {
                final Schema optionSchema = buildAvroSchema(option, fieldName, false);
                if (!typesAdded.contains(optionSchema.getType())) {
                    unionTypes.add(optionSchema);
                    typesAdded.add(optionSchema.getType());
                }
            }
            schema = Schema.createUnion(unionTypes);
            break;
        case DATE:
            schema = Schema.create(Type.INT);
            LogicalTypes.date().addToSchema(schema);
            break;
        case DOUBLE:
            schema = Schema.create(Type.DOUBLE);
            break;
        case FLOAT:
            schema = Schema.create(Type.FLOAT);
            break;
        case INT:
            schema = Schema.create(Type.INT);
            break;
        case LONG:
            schema = Schema.create(Type.LONG);
            break;
        case MAP:
            schema = Schema.createMap(buildAvroSchema(((MapDataType) dataType).getValueType(), fieldName, false));
            break;
        case RECORD:
            final RecordDataType recordDataType = (RecordDataType) dataType;
            final RecordSchema childSchema = recordDataType.getChildSchema();
            final List<Field> childFields = new ArrayList<>(childSchema.getFieldCount());
            for (final RecordField field : childSchema.getFields()) {
                childFields.add(buildAvroField(field));
            }
            schema = Schema.createRecord(fieldName + "Type", null, "org.apache.nifi", false, childFields);
            break;
        case SHORT:
            schema = Schema.create(Type.INT);
            break;
        case STRING:
            schema = Schema.create(Type.STRING);
            break;
        case TIME:
            schema = Schema.create(Type.INT);
            LogicalTypes.timeMillis().addToSchema(schema);
            break;
        case TIMESTAMP:
            schema = Schema.create(Type.LONG);
            LogicalTypes.timestampMillis().addToSchema(schema);
            break;
        default:
            return null;
    }
    if (nullable) {
        return nullable(schema);
    } else {
        return schema;
    }
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) ArrayList(java.util.ArrayList) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) Field(org.apache.avro.Schema.Field) RecordField(org.apache.nifi.serialization.record.RecordField) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) Type(org.apache.avro.Schema.Type) LogicalType(org.apache.avro.LogicalType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashSet(java.util.HashSet)

Example 8 with ArrayDataType

use of org.apache.nifi.serialization.record.type.ArrayDataType in project nifi by apache.

the class StandardSchemaValidator method isTypeCorrect.

private boolean isTypeCorrect(final Object value, final DataType dataType) {
    switch(dataType.getFieldType()) {
        case ARRAY:
            if (!(value instanceof Object[])) {
                return false;
            }
            final ArrayDataType arrayDataType = (ArrayDataType) dataType;
            final DataType elementType = arrayDataType.getElementType();
            final Object[] array = (Object[]) value;
            for (final Object arrayVal : array) {
                if (!isTypeCorrect(arrayVal, elementType)) {
                    return false;
                }
            }
            return true;
        case MAP:
            if (!(value instanceof Map)) {
                return false;
            }
            final MapDataType mapDataType = (MapDataType) dataType;
            final DataType valueDataType = mapDataType.getValueType();
            final Map<?, ?> map = (Map<?, ?>) value;
            for (final Object mapValue : map.values()) {
                if (!isTypeCorrect(mapValue, valueDataType)) {
                    return false;
                }
            }
            return true;
        case RECORD:
            return value instanceof Record;
        case CHOICE:
            final ChoiceDataType choiceDataType = (ChoiceDataType) dataType;
            for (final DataType choice : choiceDataType.getPossibleSubTypes()) {
                if (isTypeCorrect(value, choice)) {
                    return true;
                }
            }
            return false;
        case BIGINT:
            return value instanceof BigInteger;
        case BOOLEAN:
            return value instanceof Boolean;
        case BYTE:
            return value instanceof Byte;
        case CHAR:
            return value instanceof Character;
        case DATE:
            return value instanceof java.sql.Date;
        case DOUBLE:
            return value instanceof Double;
        case FLOAT:
            // Same goes for Short/Integer
            return value instanceof Float;
        case INT:
            return value instanceof Integer;
        case LONG:
            return value instanceof Long;
        case SHORT:
            return value instanceof Short;
        case STRING:
            return value instanceof String;
        case TIME:
            return value instanceof java.sql.Time;
        case TIMESTAMP:
            return value instanceof java.sql.Timestamp;
    }
    return false;
}
Also used : MapDataType(org.apache.nifi.serialization.record.type.MapDataType) BigInteger(java.math.BigInteger) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) BigInteger(java.math.BigInteger) Record(org.apache.nifi.serialization.record.Record) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) Map(java.util.Map)

Example 9 with ArrayDataType

use of org.apache.nifi.serialization.record.type.ArrayDataType in project nifi by apache.

the class StandardSchemaValidator method verifyComplexType.

private void verifyComplexType(final DataType dataType, final Object rawValue, final StandardSchemaValidationResult result, final String fieldPrefix, final RecordField field) {
    // If the field type is RECORD, or if the field type is a CHOICE that allows for a RECORD and the value is a RECORD, then we
    // need to dig into each of the sub-fields. To do this, we first need to determine the 'canonical data type'.
    final DataType canonicalDataType = getCanonicalDataType(dataType, rawValue, result, fieldPrefix, field);
    if (canonicalDataType == null) {
        return;
    }
    // Now that we have the 'canonical data type', we check if it is a Record. If so, we need to validate each sub-field.
    if (canonicalDataType.getFieldType() == RecordFieldType.RECORD) {
        verifyChildRecord(canonicalDataType, rawValue, dataType, result, field, fieldPrefix);
    }
    if (canonicalDataType.getFieldType() == RecordFieldType.ARRAY) {
        final ArrayDataType arrayDataType = (ArrayDataType) canonicalDataType;
        final DataType elementType = arrayDataType.getElementType();
        final Object[] arrayObject = (Object[]) rawValue;
        int i = 0;
        for (final Object arrayValue : arrayObject) {
            verifyComplexType(elementType, arrayValue, result, fieldPrefix + "[" + i + "]", field);
            i++;
        }
    }
}
Also used : RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType)

Example 10 with ArrayDataType

use of org.apache.nifi.serialization.record.type.ArrayDataType 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();
        });
    });
}
Also used : List(java.util.List) 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) NumericRange(org.apache.nifi.record.path.NumericRange) 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) ArrayList(java.util.ArrayList) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) 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)

Aggregations

ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)13 DataType (org.apache.nifi.serialization.record.DataType)12 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)9 RecordField (org.apache.nifi.serialization.record.RecordField)8 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)8 Map (java.util.Map)7 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)7 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)6 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)5 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)5 HashMap (java.util.HashMap)4 MapRecord (org.apache.nifi.serialization.record.MapRecord)4 Record (org.apache.nifi.serialization.record.Record)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 ArrayIndexFieldValue (org.apache.nifi.record.path.ArrayIndexFieldValue)3 FieldValue (org.apache.nifi.record.path.FieldValue)3 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Stream (java.util.stream.Stream)2