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

Allow controlling avro via java annotations when using reflection.

Log workAgile BoardRank to TopRank to BottomAttach filesAttach ScreenshotBulk Copy AttachmentsBulk Move AttachmentsVotersWatch issueWatchersCreate sub-taskConvert to sub-taskMoveLinkCloneLabelsUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments


    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.7.5
    • java
    • None
    • Hide
      Added the java annotations @AvroName, @AvroIgnore, @AvroMeta, @AvroAlias and @AvroEncode to control the behavior of avro when using reflection.

      Added the java annotations @AvroName, @AvroIgnore, @AvroMeta, @AvroAlias and @AvroEncode to control the behavior of avro when using reflection.


      It would be great if one could control avro with java annotations. As of now, it is already possible to mark fields as Nullable or classes being encoded as a String. I propose a bigger set of annotations to control the behavior of avro on fields and classes. Such annotations have proven useful with jacksons json serialization and morphias mongoDB serialization.

      I propose the following additional annotations:
      @AvroAlias(alias="alias", space="space")
      @AvroMeta(key="K", value="V")

      Java fields with the @AvroName("alternativeName") annotation will be renamed in the induced schema. When reading an avro file via reflection, the reflection reader will look for fields in the schema with "alternativeName".
      For example:

         int bar;  

      is serialized as

        { "name" : "foo", "type" : "int" } 

      The @AvroAlias annotation will add a new alias to the induced schema of a record, enum or field. The space parameter is optional and defaults to the namespace of the named schema the alias is added to.

      Fields with the @AvroIgnore annotation will be treated as if they had a transient modifier, i.e. they will not be written to or read from avro files.

      The @AvroMeta(key="K", value="V") annotation allows you to store an arbitrary key : value pair at every node in the schema.

         @AvroMeta(key="fieldKey", value="fieldValue")
         int foo;  

      will create the following schema

      {"name" : "foo", "type" : "int", "fieldKey" : "fieldValue" } 

      Fields can be custom encoded with the AvroEncode(using=CustomEncoding.class) annotation. This annotation is a generalization of the @Stringable annotation. The @Stringable annotation is limited to classes with string argument constructors. Some classes can be similarly reduced to a smaller class or even a single primitive, but dont fit the requirements for @Stringable. A prominent example is java.util.Date, which instances can essentially be described with a single long. Such classes can now be encoded with a CustomEncoding, which reads and writes directly from the encoder/decoder.

      One simply extends the abstract CustomEncodings class by implementing a schema, a read method and a write method. A java field can then be annotated like this:

      Date date;

      The custom encoding implementation would look like

      public class DateAsLongEncoding extends CustomEncoding<Date> {
          schema = Schema.create(Schema.Type.LONG);
          schema.addProp("CustomEncoding", "DateAsLongEncoding");
        public void write(Object datum, Encoder out) throws IOException {
        public Date read(Object reuse, Decoder in) throws IOException {
          if (reuse != null) {
            return (Date)reuse;
          else return new Date(in.readLong());

      I implemented said annotations and a custom encoding for java.util.Date as a proof of concept and also extended the @Stringable annotations to fields.

      This issue is a followup of AVRO-1328 and AVRO-1330.


        1. AVRO-1341.patch
          32 kB
          Vincenz Priesnitz
        2. AVRO-1341.patch
          32 kB
          Vincenz Priesnitz
        3. AVRO-1341.patch
          26 kB
          Vincenz Priesnitz
        4. AVRO-1341.patch
          38 kB
          Vincenz Priesnitz
        5. AVRO-1341.patch
          39 kB
          Doug Cutting
        6. AVRO-1341.patch
          39 kB
          Vincenz Priesnitz
        7. AVRO-1341.patch
          40 kB
          Doug Cutting

        Issue Links


          This comment will be Viewable by All Users Viewable by All Users


            vince83 Vincenz Priesnitz Assign to me
            vince83 Vincenz Priesnitz
            5 Vote for this issue
            9 Start watching this issue




                Issue deployment