apache avro 的 getLogicalType() 函数 api returns null 即使它存在

getLogicalType() function of apache avro api returns null even if it is present

我正在使用 apache avro 并希望从我的架构中获取逻辑类型。我尝试使用函数 getLogicalType() 但它 returns 为空。我不明白哪里出了问题。我的架构如下。

{
   "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"]}
   ] 
 }

以下是我访问逻辑类型的代码

    for(Schema.Field currField : schema.getFields()) {

       field = createFieldList(currField.name(), currField.schema().getType().toString(), currField.schema().getLogicalType());
       fields.add(field);
   }

您需要使用 parse 方法来设置 logicalType here or setLogicalType which will initialize it here。我没有使用过这个库,但基于代码我看不到其他方式。

然后这个变量将通过您正在使用的 getLogicalType 方法返回。

伙计们,我想我找到了答案。声明逻辑类型的方式是错误的。

而不是

{
  "name": "favorite_number",
  "type": "int",
  "logicalType": "decimal", 
  "precision": 2,
  "scale": "2"
},

应该是

{
  "name": "favorite_number",
  "type": {
    "type": "int",
    "logicalType": "decimal",
    "precision": 2,
    "scale": "2"
  }
},

现在,当我使用 getlogicalType() 函数时,它会给我预期的结果