我正在使用 ReflectData 从 Java 类生成模式。其中一个领域是
private LocalDate localDate;
并且 reflectData 生成等效代码作为记录
{
"name":"localDate",
"type":[
"null",
{
"type":"record",
"name":"LocalDate",
"namespace":"java.time",
"fields":[
]
}
],
"default":null
}
我想使用下面架构上的一些操作来覆盖类的行为,以便
final Schema schema = ReflectData.AllowNull.get().getSchema(cls). ?;
生成的架构正在引用如下的逻辑类型日期
{
"name" : "localDate",
"type" : [ "null", {
"type" : "int",
"logicalType" : "date"
} ],
"default" : null
}
...
增加了一些尝试
// 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<JavaPojo> avroParquetWriter =
AvroParquetWriter.<JavaPojo> 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