Search in sources :

Example 1 with MapDataType

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

the class PutElasticsearchHttpRecord method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) throws IOException {
    if (value == null) {
        if (nullSuppression.equals(NEVER_SUPPRESS.getValue()) || ((nullSuppression.equals(SUPPRESS_MISSING.getValue())) && fieldName != null && !fieldName.equals(""))) {
            generator.writeNullField(fieldName);
        }
        return;
    }
    final DataType chosenDataType = dataType.getFieldType() == RecordFieldType.CHOICE ? DataTypeUtils.chooseDataType(value, (ChoiceDataType) dataType) : dataType;
    final Object coercedValue = DataTypeUtils.convertType(value, chosenDataType, fieldName);
    if (coercedValue == null) {
        generator.writeNull();
        return;
    }
    switch(chosenDataType.getFieldType()) {
        case DATE:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, () -> DataTypeUtils.getDateFormat(RecordFieldType.DATE.getDefaultFormat()));
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case TIME:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, () -> DataTypeUtils.getDateFormat(RecordFieldType.TIME.getDefaultFormat()));
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case TIMESTAMP:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, () -> DataTypeUtils.getDateFormat(RecordFieldType.TIMESTAMP.getDefaultFormat()));
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case DOUBLE:
            generator.writeNumber(DataTypeUtils.toDouble(coercedValue, fieldName));
            break;
        case FLOAT:
            generator.writeNumber(DataTypeUtils.toFloat(coercedValue, fieldName));
            break;
        case LONG:
            generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
            break;
        case INT:
        case BYTE:
        case SHORT:
            generator.writeNumber(DataTypeUtils.toInteger(coercedValue, fieldName));
            break;
        case CHAR:
        case STRING:
            generator.writeString(coercedValue.toString());
            break;
        case BIGINT:
            if (coercedValue instanceof Long) {
                generator.writeNumber((Long) coercedValue);
            } else {
                generator.writeNumber((BigInteger) coercedValue);
            }
            break;
        case BOOLEAN:
            final String stringValue = coercedValue.toString();
            if ("true".equalsIgnoreCase(stringValue)) {
                generator.writeBoolean(true);
            } else if ("false".equalsIgnoreCase(stringValue)) {
                generator.writeBoolean(false);
            } else {
                generator.writeString(stringValue);
            }
            break;
        case RECORD:
            {
                final Record record = (Record) coercedValue;
                final RecordDataType recordDataType = (RecordDataType) chosenDataType;
                final RecordSchema childSchema = recordDataType.getChildSchema();
                writeRecord(record, childSchema, generator);
                break;
            }
        case MAP:
            {
                final MapDataType mapDataType = (MapDataType) chosenDataType;
                final DataType valueDataType = mapDataType.getValueType();
                final Map<String, ?> map = (Map<String, ?>) coercedValue;
                generator.writeStartObject();
                for (final Map.Entry<String, ?> entry : map.entrySet()) {
                    final String mapKey = entry.getKey();
                    final Object mapValue = entry.getValue();
                    generator.writeFieldName(mapKey);
                    writeValue(generator, mapValue, fieldName + "." + mapKey, valueDataType);
                }
                generator.writeEndObject();
                break;
            }
        case ARRAY:
        default:
            if (coercedValue instanceof Object[]) {
                final Object[] values = (Object[]) coercedValue;
                final ArrayDataType arrayDataType = (ArrayDataType) dataType;
                final DataType elementType = arrayDataType.getElementType();
                writeArray(values, fieldName, generator, elementType);
            } else {
                generator.writeString(coercedValue.toString());
            }
            break;
    }
}
Also used : 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) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) Record(org.apache.nifi.serialization.record.Record) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Map(java.util.Map)

Example 2 with MapDataType

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

the class WriteJsonResult method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) throws JsonGenerationException, IOException {
    if (value == null) {
        generator.writeNull();
        return;
    }
    final DataType chosenDataType = dataType.getFieldType() == RecordFieldType.CHOICE ? DataTypeUtils.chooseDataType(value, (ChoiceDataType) dataType) : dataType;
    final Object coercedValue = DataTypeUtils.convertType(value, chosenDataType, LAZY_DATE_FORMAT, LAZY_TIME_FORMAT, LAZY_TIMESTAMP_FORMAT, fieldName);
    if (coercedValue == null) {
        generator.writeNull();
        return;
    }
    switch(chosenDataType.getFieldType()) {
        case DATE:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, LAZY_DATE_FORMAT);
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case TIME:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, LAZY_TIME_FORMAT);
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case TIMESTAMP:
            {
                final String stringValue = DataTypeUtils.toString(coercedValue, LAZY_TIMESTAMP_FORMAT);
                if (DataTypeUtils.isLongTypeCompatible(stringValue)) {
                    generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
                } else {
                    generator.writeString(stringValue);
                }
                break;
            }
        case DOUBLE:
            generator.writeNumber(DataTypeUtils.toDouble(coercedValue, fieldName));
            break;
        case FLOAT:
            generator.writeNumber(DataTypeUtils.toFloat(coercedValue, fieldName));
            break;
        case LONG:
            generator.writeNumber(DataTypeUtils.toLong(coercedValue, fieldName));
            break;
        case INT:
        case BYTE:
        case SHORT:
            generator.writeNumber(DataTypeUtils.toInteger(coercedValue, fieldName));
            break;
        case CHAR:
        case STRING:
            generator.writeString(coercedValue.toString());
            break;
        case BIGINT:
            if (coercedValue instanceof Long) {
                generator.writeNumber(((Long) coercedValue).longValue());
            } else {
                generator.writeNumber((BigInteger) coercedValue);
            }
            break;
        case BOOLEAN:
            final String stringValue = coercedValue.toString();
            if ("true".equalsIgnoreCase(stringValue)) {
                generator.writeBoolean(true);
            } else if ("false".equalsIgnoreCase(stringValue)) {
                generator.writeBoolean(false);
            } else {
                generator.writeString(stringValue);
            }
            break;
        case RECORD:
            {
                final Record record = (Record) coercedValue;
                final RecordDataType recordDataType = (RecordDataType) chosenDataType;
                final RecordSchema childSchema = recordDataType.getChildSchema();
                writeRecord(record, childSchema, generator, gen -> gen.writeStartObject(), gen -> gen.writeEndObject(), true);
                break;
            }
        case MAP:
            {
                final MapDataType mapDataType = (MapDataType) chosenDataType;
                final DataType valueDataType = mapDataType.getValueType();
                final Map<String, ?> map = (Map<String, ?>) coercedValue;
                generator.writeStartObject();
                for (final Map.Entry<String, ?> entry : map.entrySet()) {
                    final String mapKey = entry.getKey();
                    final Object mapValue = entry.getValue();
                    generator.writeFieldName(mapKey);
                    writeValue(generator, mapValue, fieldName + "." + mapKey, valueDataType);
                }
                generator.writeEndObject();
                break;
            }
        case ARRAY:
        default:
            if (coercedValue instanceof Object[]) {
                final Object[] values = (Object[]) coercedValue;
                final ArrayDataType arrayDataType = (ArrayDataType) dataType;
                final DataType elementType = arrayDataType.getElementType();
                writeArray(values, fieldName, generator, elementType);
            } else {
                generator.writeString(coercedValue.toString());
            }
            break;
    }
}
Also used : AbstractRecordSetWriter(org.apache.nifi.serialization.AbstractRecordSetWriter) SchemaAccessWriter(org.apache.nifi.schema.access.SchemaAccessWriter) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) Supplier(java.util.function.Supplier) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) RawRecordWriter(org.apache.nifi.serialization.record.RawRecordWriter) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) JsonGenerator(org.codehaus.jackson.JsonGenerator) Map(java.util.Map) BigInteger(java.math.BigInteger) DateFormat(java.text.DateFormat) Record(org.apache.nifi.serialization.record.Record) OutputStream(java.io.OutputStream) RecordField(org.apache.nifi.serialization.record.RecordField) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) WriteResult(org.apache.nifi.serialization.WriteResult) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) Set(java.util.Set) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) JsonFactory(org.codehaus.jackson.JsonFactory) SerializedForm(org.apache.nifi.serialization.record.SerializedForm) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) Optional(java.util.Optional) RecordSetWriter(org.apache.nifi.serialization.RecordSetWriter) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) 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) ChoiceDataType(org.apache.nifi.serialization.record.type.ChoiceDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Map(java.util.Map)

Example 3 with MapDataType

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

the class MapRecord method setMapValue.

@Override
@SuppressWarnings("unchecked")
public void setMapValue(final String fieldName, final String mapKey, final Object value) {
    final Optional<RecordField> field = getSchema().getField(fieldName);
    if (!field.isPresent()) {
        return;
    }
    final RecordField recordField = field.get();
    final DataType dataType = recordField.getDataType();
    if (dataType.getFieldType() != RecordFieldType.MAP) {
        throw new IllegalTypeConversionException("Cannot set the value of map entry on Record because the field '" + fieldName + "' is of type '" + dataType + "' and cannot be coerced into an MAP type");
    }
    Object mapObject = values.get(recordField.getFieldName());
    if (mapObject == null) {
        mapObject = new HashMap<String, Object>();
    }
    if (!(mapObject instanceof Map)) {
        return;
    }
    final Map<String, Object> map = (Map<String, Object>) mapObject;
    final MapDataType mapDataType = (MapDataType) dataType;
    final DataType valueDataType = mapDataType.getValueType();
    final Object coerced = DataTypeUtils.convertType(value, valueDataType, fieldName);
    final Object replaced = map.put(mapKey, coerced);
    if (replaced == null || !replaced.equals(coerced)) {
        serializedForm = Optional.empty();
    }
}
Also used : IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) MapDataType(org.apache.nifi.serialization.record.type.MapDataType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MapDataType

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

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

MapDataType (org.apache.nifi.serialization.record.type.MapDataType)9 DataType (org.apache.nifi.serialization.record.DataType)8 Map (java.util.Map)7 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)7 RecordField (org.apache.nifi.serialization.record.RecordField)6 ChoiceDataType (org.apache.nifi.serialization.record.type.ChoiceDataType)5 RecordDataType (org.apache.nifi.serialization.record.type.RecordDataType)5 Record (org.apache.nifi.serialization.record.Record)4 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)4 FieldValue (org.apache.nifi.record.path.FieldValue)3 MapEntryFieldValue (org.apache.nifi.record.path.MapEntryFieldValue)3 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1