简体   繁体   中英

What is the default value for Union in Java for Apache Avro?

I'm trying to set the default value for a field (using Java) and I've got a nasty exception:

Exception in thread "main" org.apache.avro.AvroTypeException: Invalid default for field first: "Andy" not a ["null","string"]

at org.apache.avro.Schema.validateDefault(Schema.java:1542)

Documentation of Apache Avro: Unions says :

Unions, as mentioned above, are represented using JSON arrays. For example, ["null", "string"] declares a schema which may be either a null or string.

(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.)

Would anyone please tell me why the type of the default value must match the first element of the union ?

似乎您需要更改为 ["string","null"] 作为默认值将是第一个,您的默认值是字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM