avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Turner <rob...@bigfoot.com>
Subject Re: Clarification of getSchema()#getIndexNamed(fieldName) in Avro 1.7.5 Java API
Date Mon, 27 Jan 2014 20:12:12 GMT
Hi Lewis,

The method *getIndexNamed* is only used for *Union* schemas and gives the
index of the union's type, so 0 for "null" and 1 for "string" in this case.
The *persistent.getSchema()* returns the *Record Schema* in this case and
the get *getField* method on that gets the field. *Fields* however are not
*Schemas* so you have to call the *schema() *method to get that. If you
wanted the index of the "string" union you would call the following:

     int unionIndex = persistent.getSchema().getField(fieldName).*schema()*.

But this is not what you want, I think, so the following is the best way as
you have discovered:

    int fieldIndex = persistent.getSchema().getField(fieldName).pos();

   Rob Turner.

On 27 January 2014 19:39, Lewis John Mcgibbney <lewis.mcgibbney@gmail.com>wrote:

> Hi Folks,
> I have the following UNION field
> {"name": "url", "type": ["null","string"], "default":null},
> Once I've generated the Persistent class, I set a vlue for this field as
> follows
> page.setUrl(new Utf8("example.org"));
> When however I attempt to get the field position of the URL UNION with
> int fieldIndex = persistent.getSchema().getIndexNamed(fieldName);
> I am thrown an org.apache.avro.AvroRuntimeException: Not a union
> {"type":"record","name":"WebPage",... blah blahblah ... "default":null}]}
> However if I do
> int fieldIndex = persistent.getSchema().getField(fieldName).pos();
> This works perfectly fine...
> I've looked at this in my debugger for a bit and can't find a solution
> right now. Any ideas please?
> Thank you in advance
> Lewis
> --
> *Lewis*


View raw message