Search code examples
pythonapache-kafkaavroconfluent-platformconfluent-schema-registry

How can I produce without schema as avro type with python?


I use below code and send message to kafka. It works.

But I want to send message without schema because I have schema on kafka topic. I register it first. I do not want to send schema everytime.

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer


value_schema_str = """
{
   "type":"record",
   "name":"myrecord",
   "fields":[
      {
         "name":"id",
         "type":[
            "null",
            "int"
         ],
         "default":null
      },
      {
         "name":"product",
         "type":[
            "null",
            "string"
         ],
         "default":null
      },
      {
         "name":"quantity",
         "type":[
            "null",
            "int"
         ],
         "default":null
      },
      {
         "name":"price",
         "type":[
            "null",
            "int"
         ],
         "default":null
      }
   ]
}
"""

key_schema_str = """
{
   "type":"record",
   "name":"key_schema",
   "fields":[
      {
         "name":"id",
         "type":"int"
      }
   ]
}
"""


def delivery_report(err, msg):
    """ Called once for each message produced to indicate delivery result.
        Triggered by poll() or flush(). """
    if err is not None:
        print('Message delivery failed: {}'.format(err))
    else:
        print('Message delivered to {} [{}]'.format(msg.topic(), msg.partition()))


if __name__ == '__main__':
    value_schema = avro.loads(value_schema_str)
    key_schema = avro.loads(key_schema_str)
    #value = {"id": 1, "product": "myProduct", "quantity": 10, "price": 100}
    key = {"id": 1}


    avroProducer = AvroProducer({
        'bootstrap.servers': '10.0.0.0:9092',
        'on_delivery': delivery_report,
        'schema.registry.url': 'http://10.0.0.0:8081'
    }, default_key_schema=key_schema, default_value_schema=value_schema)

    avroProducer.produce(topic='orders', key=key)
    avroProducer.flush()t

thanks in advance


Solution

  • I do not want to send schema everytime.

    Avro requires a Schema. Full stop

    I have schema on kafka topic

    Kafka topics do not have schemas. I assume you mean you have a schema in the registry? Then you must fetch that before you use it in your producer

    from confluent_kafka.avro import CachedSchemaRegistryClient
    sr_client = CachedSchemaRegistryClient({'url': "http://10.0.0.0:8081"})
    

    Then use the client to do a get_schema call