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;
}
}
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);
}
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;
}
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;
}
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());
}
Aggregations