Avro字段默认值 [英] Avro field default values

查看:1262
本文介绍了Avro字段默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些设置Avro字段默认值的问题。我有一个简单的架构,如下所示:



data.avsc:

  {
namespace:test,
type:record,
name:Data,
fields:[
{name:id,type:[long,null]},
{name:value,type: [string,null]},
{name:raw,type:[bytes,null]}
]
}

我使用 avro-maven-plugin v1.7.6 生成Java当我使用以下方法创建模型实例时:
Data data = Data.newBuilder()。build(); ,它失败并出现异常:


org.apache.avro.AvroRuntimeException:
org。 apache.avro.AvroRuntimeException:字段ID类型:UNION pos:0而不是
设置且没有默认值。


但是如果我指定默认属性,

  {name:id,type:[long ,null],默认:nu ll},

我没有收到此错误。我在文档中读到联合中的第一个模式成为默认模式。所以我的问题是,为什么我还需要指定默认属性?我怎么做一个字段是可选的?



如果我确实需要指定默认值,那对于一个联合如何工作;我是否需要为联合中的每个模式指定默认值,以及它在顺序/语法方面如何工作?



谢谢。

解决方案

联合的默认值对应于联合的第一个模式(来源)。您的联合被定义为 [long,null] 因此默认值必须是长号。 null 不是一个很长的数字,这就是你收到错误的原因。



如果你还想定义 null 作为默认值然后首先放置空架构,即将联合更改为 [null,long] 相反。


I am running into some issues setting up default values for Avro fields. I have a simple schema as given below:

data.avsc:

{
 "namespace":"test",
 "type":"record",
 "name":"Data",
 "fields":[
    { "name": "id", "type": [ "long", "null" ] },
    { "name": "value", "type": [ "string", "null" ] },
    { "name": "raw", "type": [ "bytes", "null" ] }
 ]
}

I am using the avro-maven-plugin v1.7.6 to generate the Java model.

When I create an instance of the model using: Data data = Data.newBuilder().build();, it fails with an exception:

org.apache.avro.AvroRuntimeException: org.apache.avro.AvroRuntimeException: Field id type:UNION pos:0 not set and has no default value.

But if I specify the "default" property,

{ "name": "id", "type": [ "long", "null" ], "default": "null" },

I do not get this error. I read in the documentation that first schema in the union becomes the default schema. So my question is, why do I still need to specify the "default" property? How else do I make a field optional?

And if I do need to specify the default values, how does that work for a union; do I need to specify default values for each schema in the union and how does that work in terms of order/syntax?

Thanks.

解决方案

The default value of a union corresponds to the first schema of the union (Source). Your union is defined as ["long", "null"] therefor the default value must be a long number. null is not a long number that is why you are getting an error.

If you still want to define null as a default value then put null schema first, i.e. change the union to ["null", "long"] instead.

这篇关于Avro字段默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆