1

How do you define Java constant fields in Apache Avro?

Example

public static final String FIELD = "field";
bdparrish
  • 3,022
  • 1
  • 33
  • 54
  • If you take a look at the [documentation](https://avro.apache.org/docs/1.7.7/api/java/constant-values.html), you'll see how to do it. Also take a look at this [documentation](https://avro.apache.org/docs/1.6.0/api/java/constant-values.html) – Skizo-ozᴉʞS Aug 31 '16 at 16:11
  • @Skizo-ozᴉʞS not sure what you are showing me. this is the Avro API constants, not how to define one in my schema. – bdparrish Aug 31 '16 at 16:16

1 Answers1

2

The short answer is: you don't. Avro is a serialization system; so to understand why static fields are not serialized, we can look beyond Avro, to serialization in general. Here are a couple of resources from SO.

To prove that Avro doesn't serialize static fields, we can use ReflectData to generate a schema.

import org.apache.avro.Schema;
import org.apache.avro.reflect.ReflectData;

public class Main {
    public static void main(String... args) {
        Schema schema = ReflectData.get().getSchema(Main.Foo.class);
        System.out.println(schema);
    }

    static class Foo {
        public static final String FIELD = "field";
    }
}

The output is: {"type":"record","name":"Foo","namespace":"thepackage.Main$","fields":[]}
Notice the empty array of fields.

Without a static modifier on the field, the schema becomes:

{"type":"record","name":"Foo","namespace":"thepackage.Main$","fields":[{"name":"FIELD","type":"string"}]}

The links above explain why serializing static fields is unnecessary and undesirable, but I suppose you can achieve the same effect with a single-element enum.

Community
  • 1
  • 1
jaco0646
  • 11,033
  • 7
  • 47
  • 64
  • Not questioning how Java deals with `static` values in serialization, but in reference to using Avro as a class defining language instead. Your answer makes sense from a design of Avro. – bdparrish Sep 01 '16 at 18:38
  • I'm not entirely sure what you mean by _a class defining language_, but I suspect the answer is that Avro is _not_ a class defining language. According to its [homepage](https://avro.apache.org/) _Apache Avro is a data serialization system._ That being said, it might be interesting to see how a data serialization system could be _used_ as a class defining language; but I suspect you would be travelling into uncharted territory. – jaco0646 Sep 01 '16 at 21:58
  • Class defining language, meaning I can define a class structure so if I had a public protocol, then a third party could utilize my provided '*.avsc' file for whatever the language was that they were using. I am surprised that Avro doesn't provide a constant definition like Thrift does. – bdparrish Sep 02 '16 at 01:32
  • "_The [Apache Thrift](https://thrift.apache.org/) software framework, for scalable cross-language services development, combines a software stack with a code generation engine..._" Comparing this to Avro sounds like apples and oranges to me, but there's a popular thread at http://stackoverflow.com/questions/4633611/ – jaco0646 Sep 02 '16 at 18:12