You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Julien Phalip <jp...@gmail.com> on 2022/11/13 04:34:50 UTC

Modifying a field's schema property in Java

Hi,

I've got a schema with multiple levels of depths (sub-records) that I would
need to change slightly. In particular, I need to change the "precision"
and "scale" properties on a decimal field.

My problem is that apparently in Java a field's properties are immutable.
If you call the "add prop()" method for a property that already exists, you
get an exception.

Is there a way to make this type of modification on an existing schema, or
do you have to recreate the entire schema from scratch?

Thank you,

Julien

Re: Modifying a field's schema property in Java

Posted by Ryan Skraba <ry...@skraba.com>.
Thanks Oscar!

Julien (or anyone else) -- do you think it would be useful to have a
category of "Schema" objects that are mutable for the Java SDK?

Something like:

MutableSchema ms = originalSchema.unlock();
ms.getField("quantity").setProperty("precision", 5);
ms.getField("dept").setFieldName("department_id");
ms.getField("department_id").setType(Schema.Type.LONG);
Schema modifiedSchema = ms.lock();

This would be a major change to the Java SDK, but in the past, we've
used a lot of "ad hoc" or dynamic, transient schemas and making
changes has always been a pain point!

All my best, Ryan

On Sun, Nov 13, 2022 at 8:19 AM Oscar Westra van Holthe - Kind
<os...@westravanholthe.nl> wrote:
>
> On sun 13 nov. 2022 05:34, Julien Phalip <jp...@gmail.com> wrote:
>>
>> I've got a schema with multiple levels of depths (sub-records) that I would need to change slightly. [...]
>>
>> Is there a way to make this type of modification on an existing schema, or do you have to recreate the entire schema from scratch?
>
>
> After creation, Avro schemata are immutable. To make such modifications you can use a visitor. There already is some code available to help you along: you can find an example in the module avro-compiler, that replaces references to named schemata with the actual schema.
>
> IIRC, you're looking for the Schemas class. The interface you need to implement has the word 'visitor' in the name.
>
> Kind regards,
> Oscar
>
> --
> Oscar Westra van Holthe - Kind <os...@westravanholthe.nl>

Re: Modifying a field's schema property in Java

Posted by Oscar Westra van Holthe - Kind <os...@westravanholthe.nl>.
On sun 13 nov. 2022 05:34, Julien Phalip <jp...@gmail.com> wrote:

> I've got a schema with multiple levels of depths (sub-records) that I
> would need to change slightly. [...]
>
> Is there a way to make this type of modification on an existing schema, or
> do you have to recreate the entire schema from scratch?
>

After creation, Avro schemata are immutable. To make such modifications you
can use a visitor. There already is some code available to help you along:
you can find an example in the module avro-compiler, that replaces
references to named schemata with the actual schema.

IIRC, you're looking for the Schemas class. The interface you need to
implement has the word 'visitor' in the name.

Kind regards,
Oscar

-- 
Oscar Westra van Holthe - Kind <os...@westravanholthe.nl>

>