Search in sources :

Example 1 with IllegalTypeConversionException

use of org.apache.nifi.serialization.record.util.IllegalTypeConversionException 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 2 with IllegalTypeConversionException

use of org.apache.nifi.serialization.record.util.IllegalTypeConversionException in project nifi by apache.

the class PutHBaseRecord method createPut.

protected PutFlowFile createPut(ProcessContext context, Record record, RecordSchema schema, FlowFile flowFile, String rowFieldName, String columnFamily, String timestampFieldName, String fieldEncodingStrategy, String rowEncodingStrategy, String complexFieldStrategy) throws PutCreationFailedInvokedException {
    PutFlowFile retVal = null;
    final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue();
    final String nullStrategy = context.getProperty(NULL_FIELD_STRATEGY).getValue();
    boolean asString = STRING_ENCODING_VALUE.equals(fieldEncodingStrategy);
    final byte[] fam = clientService.toBytes(columnFamily);
    if (record != null) {
        final Long timestamp;
        if (!StringUtils.isBlank(timestampFieldName)) {
            try {
                timestamp = record.getAsLong(timestampFieldName);
            } catch (IllegalTypeConversionException e) {
                throw new PutCreationFailedInvokedException("Could not convert " + timestampFieldName + " to a long", e);
            }
            if (timestamp == null) {
                getLogger().warn("The value of timestamp field " + timestampFieldName + " was null, record will be inserted with latest timestamp");
            }
        } else {
            timestamp = null;
        }
        List<PutColumn> columns = new ArrayList<>();
        for (String name : schema.getFieldNames()) {
            if (name.equals(rowFieldName) || name.equals(timestampFieldName)) {
                continue;
            }
            Object val = record.getValue(name);
            final byte[] fieldValueBytes;
            if (val == null && nullStrategy.equals(NULL_FIELD_SKIP.getValue())) {
                continue;
            } else if (val == null && nullStrategy.equals(NULL_FIELD_EMPTY.getValue())) {
                fieldValueBytes = EMPTY;
            } else {
                fieldValueBytes = asBytes(name, schema.getField(name).get().getDataType().getFieldType(), record, asString, complexFieldStrategy);
            }
            if (fieldValueBytes != null) {
                columns.add(new PutColumn(fam, clientService.toBytes(name), fieldValueBytes, timestamp));
            }
        }
        String rowIdValue = record.getAsString(rowFieldName);
        if (rowIdValue == null) {
            throw new PutCreationFailedInvokedException(String.format("Row ID was null for flowfile with ID %s", flowFile.getAttribute("uuid")));
        }
        byte[] rowId = getRow(rowIdValue, rowEncodingStrategy);
        retVal = new PutFlowFile(tableName, rowId, columns, flowFile);
    }
    return retVal;
}
Also used : IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) PutColumn(org.apache.nifi.hbase.put.PutColumn) ArrayList(java.util.ArrayList) PutFlowFile(org.apache.nifi.hbase.put.PutFlowFile)

Example 3 with IllegalTypeConversionException

use of org.apache.nifi.serialization.record.util.IllegalTypeConversionException in project nifi by apache.

the class AvroTypeUtil method convertToAvroObject.

@SuppressWarnings("unchecked")
private static Object convertToAvroObject(final Object rawValue, final Schema fieldSchema, final String fieldName) {
    if (rawValue == null) {
        return null;
    }
    switch(fieldSchema.getType()) {
        case INT:
            {
                final LogicalType logicalType = fieldSchema.getLogicalType();
                if (logicalType == null) {
                    return DataTypeUtils.toInteger(rawValue, fieldName);
                }
                if (LOGICAL_TYPE_DATE.equals(logicalType.getName())) {
                    final String format = AvroTypeUtil.determineDataType(fieldSchema).getFormat();
                    final Date date = DataTypeUtils.toDate(rawValue, () -> DataTypeUtils.getDateFormat(format), fieldName);
                    final Duration duration = Duration.between(new Date(0L).toInstant(), new Date(date.getTime()).toInstant());
                    final long days = duration.toDays();
                    return (int) days;
                } else if (LOGICAL_TYPE_TIME_MILLIS.equals(logicalType.getName())) {
                    final String format = AvroTypeUtil.determineDataType(fieldSchema).getFormat();
                    final Time time = DataTypeUtils.toTime(rawValue, () -> DataTypeUtils.getDateFormat(format), fieldName);
                    final Date date = new Date(time.getTime());
                    final Duration duration = Duration.between(date.toInstant().truncatedTo(ChronoUnit.DAYS), date.toInstant());
                    final long millisSinceMidnight = duration.toMillis();
                    return (int) millisSinceMidnight;
                }
                return DataTypeUtils.toInteger(rawValue, fieldName);
            }
        case LONG:
            {
                final LogicalType logicalType = fieldSchema.getLogicalType();
                if (logicalType == null) {
                    return DataTypeUtils.toLong(rawValue, fieldName);
                }
                if (LOGICAL_TYPE_TIME_MICROS.equals(logicalType.getName())) {
                    final long longValue = getLongFromTimestamp(rawValue, fieldSchema, fieldName);
                    final Date date = new Date(longValue);
                    final Duration duration = Duration.between(date.toInstant().truncatedTo(ChronoUnit.DAYS), date.toInstant());
                    return duration.toMillis() * 1000L;
                } else if (LOGICAL_TYPE_TIMESTAMP_MILLIS.equals(logicalType.getName())) {
                    final String format = AvroTypeUtil.determineDataType(fieldSchema).getFormat();
                    Timestamp t = DataTypeUtils.toTimestamp(rawValue, () -> DataTypeUtils.getDateFormat(format), fieldName);
                    return getLongFromTimestamp(rawValue, fieldSchema, fieldName);
                } else if (LOGICAL_TYPE_TIMESTAMP_MICROS.equals(logicalType.getName())) {
                    return getLongFromTimestamp(rawValue, fieldSchema, fieldName) * 1000L;
                }
                return DataTypeUtils.toLong(rawValue, fieldName);
            }
        case BYTES:
        case FIXED:
            final LogicalType logicalType = fieldSchema.getLogicalType();
            if (logicalType != null && LOGICAL_TYPE_DECIMAL.equals(logicalType.getName())) {
                final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) logicalType;
                final BigDecimal rawDecimal;
                if (rawValue instanceof BigDecimal) {
                    rawDecimal = (BigDecimal) rawValue;
                } else if (rawValue instanceof Double) {
                    rawDecimal = BigDecimal.valueOf((Double) rawValue);
                } else if (rawValue instanceof String) {
                    rawDecimal = new BigDecimal((String) rawValue);
                } else if (rawValue instanceof Integer) {
                    rawDecimal = new BigDecimal((Integer) rawValue);
                } else if (rawValue instanceof Long) {
                    rawDecimal = new BigDecimal((Long) rawValue);
                } else {
                    throw new IllegalTypeConversionException("Cannot convert value " + rawValue + " of type " + rawValue.getClass() + " to a logical decimal");
                }
                // If the desired scale is different than this value's coerce scale.
                final int desiredScale = decimalType.getScale();
                final BigDecimal decimal = rawDecimal.scale() == desiredScale ? rawDecimal : rawDecimal.setScale(desiredScale, BigDecimal.ROUND_HALF_UP);
                return new Conversions.DecimalConversion().toBytes(decimal, fieldSchema, logicalType);
            }
            if (rawValue instanceof byte[]) {
                return ByteBuffer.wrap((byte[]) rawValue);
            }
            if (rawValue instanceof Object[]) {
                return AvroTypeUtil.convertByteArray((Object[]) rawValue);
            } else {
                throw new IllegalTypeConversionException("Cannot convert value " + rawValue + " of type " + rawValue.getClass() + " to a ByteBuffer");
            }
        case MAP:
            if (rawValue instanceof Record) {
                final Record recordValue = (Record) rawValue;
                final Map<String, Object> map = new HashMap<>();
                for (final RecordField recordField : recordValue.getSchema().getFields()) {
                    final Object v = recordValue.getValue(recordField);
                    if (v != null) {
                        map.put(recordField.getFieldName(), v);
                    }
                }
                return map;
            } else if (rawValue instanceof Map) {
                final Map<String, Object> objectMap = (Map<String, Object>) rawValue;
                final Map<String, Object> map = new HashMap<>(objectMap.size());
                for (final String s : objectMap.keySet()) {
                    final Object converted = convertToAvroObject(objectMap.get(s), fieldSchema.getValueType(), fieldName + "[" + s + "]");
                    map.put(s, converted);
                }
                return map;
            } else {
                throw new IllegalTypeConversionException("Cannot convert value " + rawValue + " of type " + rawValue.getClass() + " to a Map");
            }
        case RECORD:
            final GenericData.Record avroRecord = new GenericData.Record(fieldSchema);
            final Record record = (Record) rawValue;
            for (final RecordField recordField : record.getSchema().getFields()) {
                final Object recordFieldValue = record.getValue(recordField);
                final String recordFieldName = recordField.getFieldName();
                final Field field = fieldSchema.getField(recordFieldName);
                if (field == null) {
                    continue;
                }
                final Object converted = convertToAvroObject(recordFieldValue, field.schema(), fieldName + "/" + recordFieldName);
                avroRecord.put(recordFieldName, converted);
            }
            return avroRecord;
        case UNION:
            return convertUnionFieldValue(rawValue, fieldSchema, schema -> convertToAvroObject(rawValue, schema, fieldName), fieldName);
        case ARRAY:
            final Object[] objectArray = (Object[]) rawValue;
            final List<Object> list = new ArrayList<>(objectArray.length);
            int i = 0;
            for (final Object o : objectArray) {
                final Object converted = convertToAvroObject(o, fieldSchema.getElementType(), fieldName + "[" + i + "]");
                list.add(converted);
                i++;
            }
            return list;
        case BOOLEAN:
            return DataTypeUtils.toBoolean(rawValue, fieldName);
        case DOUBLE:
            return DataTypeUtils.toDouble(rawValue, fieldName);
        case FLOAT:
            return DataTypeUtils.toFloat(rawValue, fieldName);
        case NULL:
            return null;
        case ENUM:
            return new GenericData.EnumSymbol(fieldSchema, rawValue);
        case STRING:
            return DataTypeUtils.toString(rawValue, (String) null);
    }
    return rawValue;
}
Also used : RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LogicalType(org.apache.avro.LogicalType) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Field(org.apache.avro.Schema.Field) RecordField(org.apache.nifi.serialization.record.RecordField) BigDecimal(java.math.BigDecimal) IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) SpecificRecord(org.apache.avro.specific.SpecificRecord) Record(org.apache.nifi.serialization.record.Record) MapRecord(org.apache.nifi.serialization.record.MapRecord) GenericRecord(org.apache.avro.generic.GenericRecord) LogicalTypes(org.apache.avro.LogicalTypes) Duration(java.time.Duration) GenericData(org.apache.avro.generic.GenericData) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Conversions(org.apache.avro.Conversions) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with IllegalTypeConversionException

use of org.apache.nifi.serialization.record.util.IllegalTypeConversionException in project nifi by apache.

the class AvroTypeUtil method convertByteArray.

public static ByteBuffer convertByteArray(final Object[] bytes) {
    final ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    for (final Object o : bytes) {
        if (o instanceof Byte) {
            bb.put(((Byte) o).byteValue());
        } else {
            throw new IllegalTypeConversionException("Cannot convert value " + bytes + " of type " + bytes.getClass() + " to ByteBuffer");
        }
    }
    bb.flip();
    return bb;
}
Also used : IllegalTypeConversionException(org.apache.nifi.serialization.record.util.IllegalTypeConversionException) ByteBuffer(java.nio.ByteBuffer)

Example 5 with IllegalTypeConversionException

use of org.apache.nifi.serialization.record.util.IllegalTypeConversionException in project nifi by apache.

the class MapRecord method setArrayValue.

@Override
public void setArrayValue(final String fieldName, final int arrayIndex, 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.ARRAY) {
        throw new IllegalTypeConversionException("Cannot set the value of an array index on Record because the field '" + fieldName + "' is of type '" + dataType + "' and cannot be coerced into an ARRAY type");
    }
    final Object arrayObject = values.get(recordField.getFieldName());
    if (arrayObject == null) {
        return;
    }
    if (!(arrayObject instanceof Object[])) {
        return;
    }
    final Object[] array = (Object[]) arrayObject;
    if (arrayIndex >= array.length) {
        return;
    }
    final ArrayDataType arrayDataType = (ArrayDataType) dataType;
    final DataType elementType = arrayDataType.getElementType();
    final Object coerced = DataTypeUtils.convertType(value, elementType, fieldName);
    final boolean update = !Objects.equals(coerced, array[arrayIndex]);
    if (update) {
        array[arrayIndex] = 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) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType)

Aggregations

IllegalTypeConversionException (org.apache.nifi.serialization.record.util.IllegalTypeConversionException)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ArrayDataType (org.apache.nifi.serialization.record.type.ArrayDataType)3 ArrayList (java.util.ArrayList)2 MapRecord (org.apache.nifi.serialization.record.MapRecord)2 RecordField (org.apache.nifi.serialization.record.RecordField)2 MapDataType (org.apache.nifi.serialization.record.type.MapDataType)2 BigDecimal (java.math.BigDecimal)1 ByteBuffer (java.nio.ByteBuffer)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 Duration (java.time.Duration)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Conversions (org.apache.avro.Conversions)1 LogicalType (org.apache.avro.LogicalType)1 LogicalTypes (org.apache.avro.LogicalTypes)1 Field (org.apache.avro.Schema.Field)1