No my class was a POJO. I was trying not to use the generated versions. I
was able to make it work with the following hack.
final Class<User> clazz = User.class;
DatumReader<User> datumReader = new
ReflectDatumReader<User>(new ReflectData(clazz.getClassLoader())) {
@Override
public void setSchema(Schema actual) {
String namespace;
if (clazz.getEnclosingClass() == null) {
namespace =
clazz.getPackage().getName();
} else {
namespace =
clazz.getEnclosingClass().getName() + "$";
}
Schema newSchema =
Schema.createRecord(clazz.getSimpleName(), actual.getDoc(), namespace,
actual.isError());
List<Field> fields = new ArrayList<>();
for (Field field : actual.getFields()) {
fields.add(new Field(field.name(),
field.schema(), field.doc(), field.defaultVal()));
}
newSchema.setFields(fields);
super.setSchema(newSchema);
}
};
I use the actual schema from the file but I force in the appropriate class name
and namespace so that the ReflectDatumReader can appropriately construct the
object.
Thanks for the help,
gray
On Dec 19, 2018, at 01:37, Mika Ristimäki <[email protected]> wrote:
Just to double checkâ¦. Your User class is autogenerated from the Schema by
avro tools, right?
java -jar /path/to/avro-tools-1.8.2.jar compile schema user.avsc .
It is not your own POJO class?
-Mika
On 19 Dec 2018, 0.01 +0200, [email protected], wrote:
For union types (e.g bar in your schema) the first type is the default value.
In case of a missing value (new User(null)) the reader expects to read a
variable of default value type. Which in your example is a String.
I don't think that was a the problem. As far as I can see, if I vary the order
of the "null" and "string", the binary output does not change (aside from the
schema header).
This looks to be a problem between the "expected" versus the "actual" schema.
The actual schema is read correctly but unfortunately the "expected" schema is
what is calculated from my User class without the "null" field. The schema from
file is ignored? Grumble.
So I guess I need to do something like:
ReflectDataumReder<User> datumReader = new ReflectDatumReader<>(User.class);
datumReader.setExpected(schema);
DataFileReader<Schema> reader = ...
Is that right? That just seems crazy. What am I doing wrong?
Thanks,
gray
----------
This message was sent from a MailNull anti-spam account. You can get
your free account and take control over your email by visiting the
following URL.
http://mailnull.com/