Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1330

java: allow custom Encodings with annotations

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Minor
    • Resolution: Duplicate
    • None
    • None
    • java
    • None

    Description

      I propose an annotation @AvroEncode that allows one to encode a java field or a class directly. The motivation behind this is that some classes are impossible to serialize due to transient fields or result in a verbose encoding. For example, java.util.Date cannot be serialized as of now.

      The annotation would specify an implementation of an abstract serializer/deserializer class, which reads and writes directly from the encoder/decoder.
      It would also insert the encodings schema into the surrounding schema.

      The annotation would look like this:

      @AvroEncode(using=DateToUtcMillisecondsSerDe)
      Date date;
      
      public class DateToUtcMillisecondsSerDe extends CustomSerDe<Date> {
        {
          schema = Schema.create(Schema.Type.LONG);
        }
        
        @Override
        void write(Date datum, Encoder out) throws IOException {
          out.writeLong(datum.getTime());
        }
      
        @Override
        void read(Date datum, Decoder in) throws IOException  {
          datum.setTime(in.readLong());
        }
      }
      

      As a proof of concept, i implemented such a serDe for java.util.Date.

      this would solve avro-739 by delivering one or several default formats for Date, which can be Overridden to any representation by implemeting a different SerDe.

      Attachments

        1. Avro-1330.patch
          8 kB
          Vincenz Priesnitz
        2. Avro-1330update.patch
          8 kB
          Vincenz Priesnitz

        Issue Links

          Activity

            People

              vince83 Vincenz Priesnitz
              vince83 Vincenz Priesnitz
              Votes:
              6 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: