Default values to null in an avro schema, should it be "null" or null?
Asked Answered
R

1

7

I am new to Avro and trying to define a schema. Wondering if I am trying to default a value to null, should it be:

a. "default": null

b. "default": "null"

Does it matter what the data type is? like for int then the default will be "default": null, but when it's for a string then the default should be "default":"null"? or it doesn't matter?

Rockey answered 16/8, 2019 at 7:41 Comment(1)
If the type is a string and wants to set the default value then use double quotes as default.Meerkat
E
10

To default a value to null, you must specify "default": null. See the table in the Complex Types section of the specification for examples values of each type.

Does it matter what the data type is?

Yes, the type matters. As you can see in the Primitive Types section of the specification, null is its own type. You may not set an int, string, or any other primitive type to null.

If you want a "nullable" field, use Unions. For example:

{ "type": ["null", "int"], "default": null }

This dictates that this data may either be an int or it may be a null. It defaults to null. When specifying defaults for unions, remember that the default must match the first type in the union.

From the specification:

(Note that when a default value is specified for a record field whose type is a union, the type of the default value must match the first element of the union. Thus, for unions containing "null", the "null" is usually listed first, since the default value of such unions is typically null.)

Endomorphic answered 16/8, 2019 at 23:15 Comment(2)
thanks for your response. Just to confirm - is it like what @Meerkat mentioned, if the type is a string then the default should be set to "default": "null" with quotes?Rockey
@kodeWithKT, yes. If the type is string, and you want the default value to be a string with the value "null". Note that this is not null, it is a string that says "null".Endomorphic

© 2022 - 2024 — McMap. All rights reserved.