Search code examples
pythonscalaapache-kafkaspark-avro

Avro Kafka conversion issues between scala and Python


Our project has both scala and python code and we need to send/consume avro encoded messages to kafka.

I am sending avro encodes messages to kafka using python and scala. I have producer in scala code which send avro encoded messages using Twitter bijection library as following:

val resourcesPath = getClass.getResource("/avro/url_info_schema.avsc")
val schemaFile = scala.io.Source.fromURL(resourcesPath).mkString
val schema = parser.parse(schemaFile)
val recordInjection = GenericAvroCodecs[GenericRecord](schema)
val avroRecord = new GenericData.Record(schema)
avroRecord.put("url_sha256", row._1)
avroRecord.put("url", row._2._1)
avroRecord.put("timestamp", row._2._2)
val recordBytes = recordInjection.apply(avroRecord)
kafkaProducer.value.send("topic", recordBytes)

Avro schema looks like

{
  "namespace": "com.rm.avro",
  "type": "record",
  "name": "url_info",
  "fields":[
     {
        "name": "url_sha256", "type": "string"
     },
     {
        "name": "url",  "type": "string"
     },
     {
        "name": "timestamp", "type": ["long"]
     }
 ]

}

I am able to decode it successfully in KafkaConsumer in scala

val resourcesPath = getClass.getResource("/avro/url_info_schema.avsc")
val schemaFile = scala.io.Source.fromURL(resourcesPath).mkString


kafkaInputStream.foreachRDD(kafkaRDD => {
  kafkaRDD.foreach(

    avroRecord => {
      val parser = new Schema.Parser()
      val schema = parser.parse(schemaFile)
      val recordInjection = GenericAvroCodecs[GenericRecord](schema)
      val record = recordInjection.invert(avroRecord.value()).get
      println(record)
    }
  )

}

However, I am unable to decode messages in python I get following exception

'utf8' codec can't decode byte 0xe4 in position 16: invalid continuation byte

python codes looks like below: schema_path="avro/url_info_schema.avsc" schema = avro.schema.parse(open(schema_path).read())

for msg in consumer:
   bytes_reader = io.BytesIO(msg.value)
    decoder = avro.io.BinaryDecoder(bytes_reader)
    reader = avro.io.DatumReader(schema)
    decoded_msg = reader.read(decoder)
    print(decoded_msg)

Also python avro producer messages is not understood by scala avro consumer. I get an exception there. Python Avro producer looks like following:

datum_writer = DatumWriter(schema)
bytes_writer = io.BytesIO()

datum_writer = avro.io.DatumWriter(schema)
encoder = avro.io.BinaryEncoder(bytes_writer)
datum_writer.write(data, encoder) 
raw_bytes = bytes_writer.getvalue()
producer.send(topic, raw_bytes)

How do I stay consistent across python and scala? Any pointers will be great


Solution

  • I was using Binary encoder in python and and nothing in Scala. Just had to change one line from

    val recordInjection = GenericAvroCodecs[GenericRecord](schema)
    

    to

    val recordInjection = GenericAvroCodecs.toBinary[GenericRecord](schema)
    

    I hope others find it useful. No changes needed in python code