apache avro api的getLogicalType()函数即使存在也会返回null
问题描述:
我正在使用apache avro并希望从我的模式中获取逻辑类型。我尝试使用函数getLogicalType()
,但它返回null。我不明白什么是错的。我的模式如下。apache avro api的getLogicalType()函数即使存在也会返回null
{
"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": "int", "logicalType": "decimal", "precision": 2, "scale": "2"},
{"name": "favorite_color", "type": ["string", "null"]}
]
}
以下是我在哪里访问logicalType
for(Schema.Field currField : schema.getFields()) {
field = createFieldList(currField.name(), currField.schema().getType().toString(), currField.schema().getLogicalType());
fields.add(field);
}
答
伙计们,我想我找到了答案。逻辑类型被声明的方式是错误的。取而代之的
{
"name": "favorite_number",
"type": "int",
"logicalType": "decimal",
"precision": 2,
"scale": "2"
},
应该
{
"name": "favorite_number",
"type": {
"type": "int",
"logicalType": "decimal",
"precision": 2,
"scale": "2"
}
},
现在,当我使用getlogicalType()函数,它给了我预期的结果
答
您需要使用解析方法将设置logicalType here或setLogicalType将初始化here的代码。我没有经历过这个图书馆,但基于代码我没有看到其他方式。
然后这个变量将由getLogicalType返回您正在使用的方法。
正如我在评论中写道,以我的回答你有为所有字段循环,并且只为第二个字段定义* logicalType *,这就是为什么getLogicalType()返回null,至少对于第一个和第三个字段。尝试仅为每个字段输出getLogicalType的结果,而不是在createFieldList()中使用它。 – LLL
不,它打印全部为空 – Hazzard