It's not clear to me what you're trying to do.

"Persistent" is a record with a field named "url" whose value is a union.

Schema recordSchema = Persistent.class.getSchema();
List<Field> fields = recordSchema.getFields();
Field urlField = recordSchema.getField("url");

// a field's position is within the record that contains the field
assert(urlField == fields.get(urlField.pos());

Schema urlFieldSchema = urlField.schema();

// an index in a union schema is the position of a schema within the union
assert(urlFieldSchema.getIndexNamed("null")) == 0);
assert(urlFieldSchema.getIndexNamed("string")) == 1);

Does this help?

Doug


On Mon, Jan 27, 2014 at 11:39 AM, Lewis John Mcgibbney
<lewis.mcgibb...@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

Reply via email to