Search in sources :

Example 61 with RecordField

use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.

the class DataTypeUtils method merge.

public static RecordField merge(final RecordField thisField, final RecordField otherField) {
    final String fieldName = thisField.getFieldName();
    final Set<String> aliases = new HashSet<>();
    aliases.addAll(thisField.getAliases());
    aliases.addAll(otherField.getAliases());
    final Object defaultValue;
    if (thisField.getDefaultValue() == null && otherField.getDefaultValue() != null) {
        defaultValue = otherField.getDefaultValue();
    } else {
        defaultValue = thisField.getDefaultValue();
    }
    final DataType dataType;
    if (thisField.getDataType().equals(otherField.getDataType())) {
        dataType = thisField.getDataType();
    } else {
        dataType = RecordFieldType.CHOICE.getChoiceDataType(thisField.getDataType(), otherField.getDataType());
    }
    return new RecordField(fieldName, dataType, defaultValue, aliases, thisField.isNullable() || otherField.isNullable());
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) HashSet(java.util.HashSet)

Example 62 with RecordField

use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.

the class DataTypeUtils method convertRecordFieldtoObject.

/**
 * Creates a native Java object from a given object of a specified type. Non-scalar (complex, nested, etc.) data types are processed iteratively/recursively, such that all
 * included objects are native Java objects, rather than Record API objects or implementation-specific objects.
 * @param value The object to be converted
 * @param dataType The type of the provided object
 * @return An object representing a native Java conversion of the given input object
 */
public static Object convertRecordFieldtoObject(final Object value, final DataType dataType) {
    if (value == null) {
        return null;
    }
    if (value instanceof Record) {
        Record record = (Record) value;
        RecordSchema recordSchema = record.getSchema();
        if (recordSchema == null) {
            throw new IllegalTypeConversionException("Cannot convert value of type Record to Map because Record does not have an associated Schema");
        }
        final Map<String, Object> recordMap = new HashMap<>();
        for (RecordField field : recordSchema.getFields()) {
            final DataType fieldDataType = field.getDataType();
            final String fieldName = field.getFieldName();
            Object fieldValue = record.getValue(fieldName);
            if (fieldValue == null) {
                recordMap.put(fieldName, null);
            } else if (isScalarValue(fieldDataType, fieldValue)) {
                recordMap.put(fieldName, fieldValue);
            } else if (fieldDataType instanceof RecordDataType) {
                Record nestedRecord = (Record) fieldValue;
                recordMap.put(fieldName, convertRecordFieldtoObject(nestedRecord, fieldDataType));
            } else if (fieldDataType instanceof MapDataType) {
                recordMap.put(fieldName, convertRecordMapToJavaMap((Map) fieldValue, ((MapDataType) fieldDataType).getValueType()));
            } else if (fieldDataType instanceof ArrayDataType) {
                recordMap.put(fieldName, convertRecordArrayToJavaArray((Object[]) fieldValue, ((ArrayDataType) fieldDataType).getElementType()));
            } else {
                throw new IllegalTypeConversionException("Cannot convert value [" + fieldValue + "] of type " + fieldDataType.toString() + " to Map for field " + fieldName + " because the type is not supported");
            }
        }
        return recordMap;
    } else if (value instanceof Map) {
        return convertRecordMapToJavaMap((Map) value, ((MapDataType) dataType).getValueType());
    } else if (dataType != null && isScalarValue(dataType, value)) {
        return value;
    }
    throw new IllegalTypeConversionException("Cannot convert value of class " + value.getClass().getName() + " because the type is not supported");
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) DataType(org.apache.nifi.serialization.record.DataType) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 63 with RecordField

use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.

the class TestSimpleRecordSchema method testPreventsTwoFieldsWithConflictingNamesAliases.

@Test
public void testPreventsTwoFieldsWithConflictingNamesAliases() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar")));
    fields.add(new RecordField("bar", RecordFieldType.STRING.getDataType()));
    try {
        new SimpleRecordSchema(fields);
        Assert.fail("Was able to create two fields with conflicting names/aliases");
    } catch (final IllegalArgumentException expected) {
    }
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 64 with RecordField

use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.

the class TestSimpleRecordSchema method testPreventsTwoFieldsWithSameAlias.

@Test
public void testPreventsTwoFieldsWithSameAlias() {
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("hello", RecordFieldType.STRING.getDataType(), null, set("foo", "bar")));
    fields.add(new RecordField("goodbye", RecordFieldType.STRING.getDataType(), null, set("baz", "bar")));
    try {
        new SimpleRecordSchema(fields);
        Assert.fail("Was able to create two fields with same alias");
    } catch (final IllegalArgumentException expected) {
    }
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 65 with RecordField

use of org.apache.nifi.serialization.record.RecordField in project nifi by apache.

the class Concat method evaluate.

@Override
public Stream<FieldValue> evaluate(final RecordPathEvaluationContext context) {
    Stream<FieldValue> concatenated = Stream.empty();
    for (final RecordPathSegment valuePath : valuePaths) {
        final Stream<FieldValue> stream = valuePath.evaluate(context);
        concatenated = Stream.concat(concatenated, stream);
    }
    final StringBuilder sb = new StringBuilder();
    concatenated.forEach(fv -> sb.append(DataTypeUtils.toString(fv.getValue(), (String) null)));
    final RecordField field = new RecordField("concat", RecordFieldType.STRING.getDataType());
    final FieldValue responseValue = new StandardFieldValue(sb.toString(), field, null);
    return Stream.of(responseValue);
}
Also used : RecordPathSegment(org.apache.nifi.record.path.paths.RecordPathSegment) RecordField(org.apache.nifi.serialization.record.RecordField) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue) FieldValue(org.apache.nifi.record.path.FieldValue) StandardFieldValue(org.apache.nifi.record.path.StandardFieldValue)

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