'Scala Kafka exception: NoSuchMethodError: org.apache.avro.Schema.toString

I'm developing a kafka producer code in scala with those libs (I have to use version >6.X in kafka avro serializer to use TLS comunication):

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.confluent/kafka-avro-serializer -->
<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-avro-serializer</artifactId>
    <version>7.1.1</version>
</dependency>

In my code I'm creating a producer and sending the object like this:

logger.info("Preparing to send response through kafka")
  val conf = ConfigFactory.load()
  logger.info("Finished preparing ConfigFactory")
  val TOPIC: String = conf.getString(KAFKA_TOPIC)
  val KEY: String = conf.getString(KAFKA_KEY)
  logger.info("Send kafka event to topic: " + TOPIC)


  logger.info("Sending Response to petition " + statusResponse.getIdPetition + " with status: " + statusResponse.getPetitionStatus)
  //val data: GenericRecord = generateGenericRecord(statusResponse)

  logger.info("Generating Schema")
  val parser: Schema.Parser = new Schema.Parser()
  val avscStream = this.getClass.getResourceAsStream("/avro/*******.avsc")
  val schema: Schema = parser.parse(avscStream)

  logger.info("Generating Record")
  val data: GenericRecord = new GenericRecordBuilder(schema)
    .set("timestamp", statusResponse.getTimestamp)
    .set("idPetition", statusResponse.getIdPetition)
    .set("petitionStatus", statusResponse.getPetitionStatus)
    .set("responseCode", statusResponse.getResponseCode).build()

  val writer = new SpecificDatumWriter[GenericRecord](schema)
  val out = new ByteArrayOutputStream()
  val encoder: BinaryEncoder = EncoderFactory.get().binaryEncoder(out, null)
  writer.write(data, encoder)
  encoder.flush()
  out.close()

  var producer: Producer[String, Array[Byte]] = new KafkaProducer[String, Array[Byte]](KafkaConfiguration.getProperties)
  producer.send(new ProducerRecord[String, Array[Byte]](TOPIC, out.toByteArray))

The complete error trace:

User class threw exception: java.lang.NoSuchMethodError: org.apache.avro.Schema.toString(Ljava/util/Collection;Z)Ljava/lang/String;at io.confluent.kafka.schemaregistry.avro.AvroSchema.canonicalString(AvroSchema.java:157)at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.getIdFromRegistry(CachedSchemaRegistryClient.java:316)at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.getId(CachedSchemaRegistryClient.java:539)at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:127)at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:61)at org.apache.kafka.common.serialization.Serializer.serialize(Serializer.java:62)at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:894)at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:856)at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:743)at com.ing.cgpjrobotic.repository.KafkaSender.send(KafkaSender.scala:65)at com.ing.cgpjrobotic.service.ResponseService.generatePetitionStatusResponse(ResponseService.scala:54)at com.ing.cgpjrobotic.service.ResponseService.generateSuccessfulPetitionStatusResponse(ResponseService.scala:64)at com.ing.cgpjrobotic.App.run(App.scala:55)at com.ing.lambda.LauncherMain$.launchBatchExecutions(LauncherMain.scala:24)at com.ing.lambda.LauncherMain$.main(LauncherMain.scala:53)at com.ing.lambda.LauncherMain.main(LauncherMain.scala)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.apache.spark.deploy.yarn.ApplicationMaster$$anon$4.run(ApplicationMaster.scala:721)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source