Я использую RefertData для генерации схемы из класса Java.
одна из полей - < /p>
private LocalDate localDate;
< /code>
и RefertData генерирует эквивалентный код как запись < /p>
{
"name":"localDate",
"type":[
"null",
{
"type":"record",
"name":"LocalDate",
"namespace":"java.time",
"fields":[
]
}
],
"default":null
}
< /code>
Я хотел бы переопределить поведение в классе, используя некоторые операции на схеме ниже, так что < /p>
final Schema schema = ReflectData.AllowNull.get().getSchema(cls). ?;
< /code>
Результирующая схема ссылается на дату логического типа, как ниже < /p>
{
"name" : "localDate",
"type" : [ "null", {
"type" : "int",
"logicalType" : "date"
} ],
"default" : null
}
< /code>
....
Добавил еще несколько попыток < /p>
// given a java object
@Data
public class JavaPojo {
@AvroEncode(using = DateAsLongEncoding.class)
private Date javaDate;
@AvroEncode(using = DateAsLongEncoding.class)
private LocalDateTime javaLocalDateTime;
}
...
// and an instance of it
JavaPojo javaPojo = new JavaPojo();
javaPojo.setJavaDate(new Date());
javaPojo.setJavaLocalDateTime(LocalDateTime.now())
...
// use relfection to generate the schema and eventually store it as parquet with a logicalType mapping
Schema schema = ReflectData.AllowNull.get().getSchema(JavaPojo.class);
// attempted to toggle on timeSupport as well
GenericData timeSupport = new GenericData();
timeSupport.addLogicalTypeConversion(new TimeConversions.DateConversion());
ParquetWriter avroParquetWriter =
AvroParquetWriter. builder(fsPath)
.withSchema(schema)
.withDataModel(ReflectData.get())
.withDataModel(timeSupport)
.withCompressionCodec(ompressionCodecName.SNAPPY)
.build();
...
// fails with the below when no timeSupport is set
java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.Number
or similarly with LocalDateTime
// fails with the below when timeSupport is added
java.lang.ClassCastException: JavaPojo cannot be cast to org.apache.avro.generic.IndexedRecord
Подробнее здесь: https://stackoverflow.com/questions/563 ... gical-type