如何使用AvroProducer向主题添加数据

问题描述 投票:0回答:1

我有一个具有以下架构的主题。有人可以帮我解决如何将数据添加到不同的字段。

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

我尝试了以下代码。我得到列表不可迭代或列表超出范围。

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()
python apache-kafka avro kafka-producer-api
1个回答
1
投票

目前尚不清楚您期望像这样做什么... ['Object'][0]并且字典的键不能为列表。

尝试发送,与您的Avro模式相匹配

value = {
    'Object': {
       "Number_ID", 1, 
       "Accept": true
    }, 
    'DataStructureType' : "testvalue", 
    'ProjectID' : "123"
}
© www.soinside.com 2019 - 2024. All rights reserved.