tzatziki
tzatziki copied to clipboard
Impossible de parse date type from avro schema
Hello,
Currently in the KafkaSteps class, when we try to map a date like 2024-09-16, we encounter some troubles because of the avro type.
In fact, when we have a date, we have this kind of Avro schema:
{ "default": null, "name": "my_date", "type": [ "null", { "logicalType": "date", "type": "int" } ] }
But because, the type is considered as an UNION, the parseAvro method doesn't transform the value in an Integer.
What I suggest is to modify the method like this to cover this situation:
@Nullable
private Object parseAvro(String value, Schema valueSchema) {
return switch (valueSchema.getType()) {
case INT -> {
if("date".equals(valueSchema.getProp("logicalType"))) {
yield (int) LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE).toEpochDay();
} else {
yield Integer.parseInt(value);
}
}
case LONG -> Long.parseLong(value);
case FLOAT -> Float.parseFloat(value);
case DOUBLE -> Double.parseDouble(value);
case BOOLEAN -> Boolean.parseBoolean(value);
default -> value;
};
}