Aerluft
Aerluft

Reputation: 41

How to add data to a topic using AvroProducer

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()

Upvotes: 0

Views: 167

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 192043

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

Upvotes: 1

Related Questions