Hi,
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.
How to define your schema depends on what you want. If you want the reader to
return null then define bar as
{ "name": "bar", "type": ["null", “string"] }
or (the exact same thing but being explicit about it)
{ "name": "bar", "type": ["null", “string”], "default": null }
If you want to the reader return some string (can be empty string “") define
the schema as
{ "name": "bar", "type": ["string", “null"], "default": “my default return
value” }
For more details see
https://avro.apache.org/docs/1.8.2/spec.html#Unions
https://avro.apache.org/docs/1.8.2/spec.html#schema_record
-Mika
On 17 Dec 2018, 23.50 +0200, [email protected], wrote:
> Howdy:
>
> The following test program is not working with the 1.8.2 version of
> apache-avro because of a null field. Am I doing something wrong? The error I
> get is: "Found null, expecting string" with the record with a null value for
> the bar field.
>
> I've search the Jira bug list but I don't see anything relevant.
>
> Thanks much,
> gray
>
> -----------------
>
> public class Foo {
>
> private static final String AVRO = "{ \"name\": \"User\", \"type\":
> \"record\","
> + "\"fields\": [ {\"name\": \"bar\", \"type\": [\"string\", \"null\"]} ]}";
>
> public static void main(String[] args) throws Exception {
> File file = new File("users.avro");
> DataFileWriter<User> writer = new DataFileWriter<User>(new
> ReflectDatumWriter<User>(User.class));
> writer.create(new Schema.Parser().parse(AVRO), file);
> writer.append(new User("bar1"));
> writer.append(new User(null));
> writer.close();
>
> DataFileReader<User> reader = new DataFileReader<User>(file, new
> ReflectDatumReader<>(User.class));
> reader.next(); // works
> reader.next(); // ENOWORK
> reader.close();
> }
>
> public static class User {
> private String bar;
> public User() {
> // needed by avro reader
> }
> public User(String bar) {
> this.bar = bar;
> }
> }
> }
>
>
> ----------
> 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/