Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Avro support required fields?

Tags:

avro

i.e. is it possible to make field required similar to ProtoBuf:

message SearchRequest { required string query = 1; }

like image 908
alex Avatar asked Aug 13 '15 17:08

alex


People also ask

How do I use Avro files in data flows?

When working with Avro files in data flows, you can read and write complex data types, but be sure to clear the physical schema from the dataset first. In data flows, you can set your logical projection and derive columns that are complex structures, then auto-map those fields to an Avro file.

What is Avro?

What is Avro? Avro is an open-source schema specification for data serialization that provides serialization and data exchange services for Apache Hadoop. Avro is a language-agnostic format that can be used for any language that facilitates the exchange of data between programs.

What Avro logical types does the Avro data source support?

The Avro data source supports reading the following Avro logical types: The Avro data source ignores docs, aliases, and other properties present in the Avro file. This library supports writing of all Spark SQL types into Avro.

Why is Avro data not tagged with type information in RPC?

Avro-based remote procedure call (RPC) systems must also guarantee that remote recipients of data have a copy of the schema used to write that data. Because the schema used to write data is always available when the data is read, Avro data itself is not tagged with type information. The schema is required to parse data.


1 Answers

All fields are required in Avro by default. As is mentioned in the official documentation, if you want to make something optional, you have to make it nullable by unioning its type with null, like this

{ "namespace": "example.avro",
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "favorite_number",  "type": ["int", "null"]},
    {"name": "favorite_color", "type": ["string", "null"]}
  ]
}

In this example, name is required, favorite_number and favorite_color are optional. I recommend spending some more time with the documentation.

like image 62
Keegan Avatar answered Dec 09 '22 17:12

Keegan