0

I have a topic with the following schema. Could someone help me out on how to add data to the different fields.

{
  "name": "Project",
  "type": "record",
  "namespace": "abcdefg",
  "fields": [   
    {
      "name": "Object",
      "type": {
        "name": "Object",
        "type": "record",
        "fields": [
          {
            "name": "Number_ID",
            "type": "int"
          },
          {
            "name": "Accept",
            "type": "boolean"
          }
        ]
      }
    },
    {
      "name": "DataStructureType",
      "type": "string"
    },
    {
      "name": "ProjectID",
      "type": "string"
    }
  ]
}

I tried the following code. I get list is not iterable or list is out of range.

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer


AvroProducerConf = {'bootstrap.servers': 'localhost:9092','schema.registry.url': 'http://localhost:8081'}
value_schema = avro.load('project.avsc')

avroProducer = AvroProducer(AvroProducerConf, default_value_schema = value_schema)

while True:
    avroProducer.produce(topic = 'my_topic', value = {['Object'][0] : "value", ['Object'] [1] : "true", ['DataStructureType'] : "testvalue", ['ProjectID'] : "123"})

    avroProducer.flush()
Aerluft
  • 41
  • 1
  • 7

1 Answers1

1

It's not clear what you're expecting something like this to do... ['Object'][0] and keys of a dict cannot be lists.

Try sending this, which matches your Avro schema

value = {
    'Object': {
       "Number_ID", 1, 
       "Accept": true
    }, 
    'DataStructureType' : "testvalue", 
    'ProjectID' : "123"
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks. This works. But when I use a schema with float fields in it, I get an error `TypeError: argument of type 'float' is not iterable` – Aerluft Apr 02 '20 at 14:58
  • Lets say you changed `NumberId` type to `double`. The above value still works. – OneCricketeer Apr 02 '20 at 19:52