How do you define Java constant fields in Apache Avro?
Example
public static final String FIELD = "field";
How do you define Java constant fields in Apache Avro?
Example
public static final String FIELD = "field";
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.