[ 
https://issues.apache.org/jira/browse/AVRO-3520?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17539481#comment-17539481
 ] 

Christophe Le Saec commented on AVRO-3520:
------------------------------------------

For the existing encoded binaries (that are already exits), as it does not 
embed schema information, i suggest the reader try to get information for added 
field :
{code:java}
  @Override  
  protected MyCustomv2 read(Object object, Decoder in)  {    
    // ... fill in field of first version
    // For fields in new version, potentially non exists
    try {
       mc.setNewField(in.readString());
    }
    catch (IOException ex) {}
  }
{code}
(This imply that new fields are added at end of Record, and support no other 
kind of change)

For futur encoded binaries, you could put schema information on record itself,
{code:java}
final Field version = new Field("version", Schema.create(Schema.Type.INT), 
"schelma version", 0);
...
Schema.createRecord("record", "doc", "namespace", false, Arrays.asList(version, 
...));
{code}
Then, use this "metadata" field to store object version

For instance, with second version
{code:java}
    @Override
    protected void write(Object datum, Encoder out) throws IOException {
         ...     
         out.writeInt(2); // version 2
        ...
    }

    @Override
    protected MyCustom_versionX read(Object reuse, Decoder in) throws 
IOException {
       ....
       int version = in.readInt();
       // So, you can have an adapted code to the version
    }

    @Override
    protected Schema getSchema() {
        // return version 2
    }
{code}
(This allow more modification than simply add field, but also, change field 
type (from int to float for example ...))

Hope it will help.

> CustomEncoding doesn't expose the read schema
> ---------------------------------------------
>
>                 Key: AVRO-3520
>                 URL: https://issues.apache.org/jira/browse/AVRO-3520
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.11.0
>            Reporter: Colin
>            Priority: Major
>
> Currently it is not possible to detect a schema change when using 
> `CustomEncoding<T>`.



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to