duolanierduone
duolanierduone

Reputation: 157

How to write a JSON into a Avro Schema in NiFi

I want to write a Avro schema. The JSON is like this.

{
"manager": {
    "employeeId": "ref-456",
    "name": "John Doe"
  }
}

This is the schema I written. But it's wrong. How can I change it to the right thing?

{
    "namespace":"nifi",
    "name":"store_event",
    "type":"record",
    "fields":[ {
        "name" : "manager",
        "type" : [
            {"name":"employeeId", "type":"string"},
            {"name":"name", "type":"string"}
        ]   
    }       
        
    ]
}

Upvotes: 0

Views: 1745

Answers (1)

Up_One
Up_One

Reputation: 5271

Here it is :

    {
    "type": "record",
    "name": "nifiRecord",
    "namespace": "org.apache.nifi",
    "fields": [
        {
            "name": "manager",
            "type": [
                "null",
                {
                    "type": "record",
                    "name": "managerType",
                    "fields": [
                        {
                            "name": "employeeId",
                            "type": [
                                "null",
                                "string"
                            ]
                        },
                        {
                            "name": "name",
                            "type": [
                                "null",
                                "string"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

You can actually infer the schema quite easy(i did this using your json payload)

Use ConvertRecord with a JsonTreeReader(Infer Schema) + JsonTreeSetWritter (Set Avro.Schema Attribute - this will tell you the schema)

enter image description here

enter image description here

Upvotes: 2

Related Questions