Search code examples
javaavro

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


I am using apache avro to and want get the logical type from my schema. I tried using the function getLogicalType() but it returns null. I don't understand what is wrong. My schema is as follows.

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

Following is the code where I am accessing the logicalType

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

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

Solution

  • Guys I think I found the answer. The way in which logical type was being declared was wrong.

    Instead of

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

    It should be

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

    Now when I use getlogicalType() function it gives me the expected result