Search in sources :

Example 1 with DataException

use of org.apache.kafka.connect.errors.DataException in project kafka by apache.

the class ConnectSchema method validateValue.

public static void validateValue(String name, Schema schema, Object value) {
    if (value == null) {
        if (!schema.isOptional())
            throw new DataException("Invalid value: null used for required field: \"" + name + "\", schema type: " + schema.type());
        else
            return;
    }
    List<Class> expectedClasses = LOGICAL_TYPE_CLASSES.get(schema.name());
    if (expectedClasses == null)
        expectedClasses = SCHEMA_TYPE_CLASSES.get(schema.type());
    if (expectedClasses == null)
        throw new DataException("Invalid Java object for schema type " + schema.type() + ": " + value.getClass() + " for field: \"" + name + "\"");
    boolean foundMatch = false;
    for (Class<?> expectedClass : expectedClasses) {
        if (expectedClass.isInstance(value)) {
            foundMatch = true;
            break;
        }
    }
    if (!foundMatch)
        throw new DataException("Invalid Java object for schema type " + schema.type() + ": " + value.getClass() + " for field: \"" + name + "\"");
    switch(schema.type()) {
        case STRUCT:
            Struct struct = (Struct) value;
            if (!struct.schema().equals(schema))
                throw new DataException("Struct schemas do not match.");
            struct.validate();
            break;
        case ARRAY:
            List<?> array = (List<?>) value;
            for (Object entry : array) validateValue(schema.valueSchema(), entry);
            break;
        case MAP:
            Map<?, ?> map = (Map<?, ?>) value;
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                validateValue(schema.keySchema(), entry.getKey());
                validateValue(schema.valueSchema(), entry.getValue());
            }
            break;
    }
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with DataException

use of org.apache.kafka.connect.errors.DataException in project kafka by apache.

the class Date method fromLogical.

/**
     * Convert a value from its logical format (Date) to it's encoded format.
     * @param value the logical value
     * @return the encoded value
     */
public static int fromLogical(Schema schema, java.util.Date value) {
    if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME)))
        throw new DataException("Requested conversion of Date object but the schema does not match.");
    Calendar calendar = Calendar.getInstance(UTC);
    calendar.setTime(value);
    if (calendar.get(Calendar.HOUR_OF_DAY) != 0 || calendar.get(Calendar.MINUTE) != 0 || calendar.get(Calendar.SECOND) != 0 || calendar.get(Calendar.MILLISECOND) != 0) {
        throw new DataException("Kafka Connect Date type should not have any time fields set to non-zero values.");
    }
    long unixMillis = calendar.getTimeInMillis();
    return (int) (unixMillis / MILLIS_PER_DAY);
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) Calendar(java.util.Calendar)

Example 3 with DataException

use of org.apache.kafka.connect.errors.DataException in project kafka by apache.

the class SchemaBuilder method defaultValue.

/**
     * Set the default value for this schema. The value is validated against the schema type, throwing a
     * {@link SchemaBuilderException} if it does not match.
     * @param value the default value
     * @return the SchemaBuilder
     */
public SchemaBuilder defaultValue(Object value) {
    checkNull(DEFAULT_FIELD, defaultValue);
    checkNotNull(TYPE_FIELD, type, DEFAULT_FIELD);
    try {
        ConnectSchema.validateValue(this, value);
    } catch (DataException e) {
        throw new SchemaBuilderException("Invalid default value", e);
    }
    defaultValue = value;
    return this;
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) SchemaBuilderException(org.apache.kafka.connect.errors.SchemaBuilderException)

Example 4 with DataException

use of org.apache.kafka.connect.errors.DataException in project kafka by apache.

the class Time method fromLogical.

/**
     * Convert a value from its logical format (Time) to it's encoded format.
     * @param value the logical value
     * @return the encoded value
     */
public static int fromLogical(Schema schema, java.util.Date value) {
    if (schema.name() == null || !(schema.name().equals(LOGICAL_NAME)))
        throw new DataException("Requested conversion of Time object but the schema does not match.");
    Calendar calendar = Calendar.getInstance(UTC);
    calendar.setTime(value);
    long unixMillis = calendar.getTimeInMillis();
    if (unixMillis < 0 || unixMillis > MILLIS_PER_DAY) {
        throw new DataException("Kafka Connect Time type should not have any date fields set to non-zero values.");
    }
    return (int) unixMillis;
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) Calendar(java.util.Calendar)

Example 5 with DataException

use of org.apache.kafka.connect.errors.DataException in project kafka by apache.

the class TimestampRouter method apply.

@Override
public R apply(R record) {
    final Long timestamp = record.timestamp();
    if (timestamp == null) {
        throw new DataException("Timestamp missing on record: " + record);
    }
    final String formattedTimestamp = timestampFormat.get().format(new Date(timestamp));
    final String updatedTopic = topicFormat.replace("${topic}", record.topic()).replace("${timestamp}", formattedTimestamp);
    return record.newRecord(updatedTopic, record.kafkaPartition(), record.keySchema(), record.key(), record.valueSchema(), record.value(), record.timestamp());
}
Also used : DataException(org.apache.kafka.connect.errors.DataException) Date(java.util.Date)

Aggregations

DataException (org.apache.kafka.connect.errors.DataException)11 Map (java.util.Map)5 HashMap (java.util.HashMap)4 ConnectSchema (org.apache.kafka.connect.data.ConnectSchema)4 Schema (org.apache.kafka.connect.data.Schema)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Calendar (java.util.Calendar)2 Field (org.apache.kafka.connect.data.Field)2 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 SerializationException (org.apache.kafka.common.errors.SerializationException)1 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)1 Struct (org.apache.kafka.connect.data.Struct)1 SchemaBuilderException (org.apache.kafka.connect.errors.SchemaBuilderException)1