sksamuel / avro4s

Avro schema generation and serialization / deserialization for Scala
Apache License 2.0
719 stars 238 forks source link

java.lang.ExceptionInInitializerError when using GenericSerde #810

Open ayoub-benali opened 1 year ago

ayoub-benali commented 1 year ago

Hello,

I generated case classes from an Avro schema file using sbt-avrohugger and I want to use avro4s for generating a kafka Serde because io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde doesn't work with classes that include Shapeless classes.

Initially I tried this:

import org.apache.kafka.common.serialization.Serde
import com.sksamuel.avro4s.kafka.GenericSerde
import com.sksamuel.avro4s.BinaryFormat
import scala.collection.JavaConverters._

implicit val inputSerde: Serde[MyClass] = new GenericSerde[MyClass](BinaryFormat)

val serdeConfig = Map("schema.registry.url" -> "SOME_URL").asJava
inputSerde.configure(serdeConfig, false)

MyClass has a lot of nested classes and I got this error during compilation:

[error] Error while emitting MyClass$
[error] Method too large: MyClass$.<init> ()V

Then I tried invoking SchemaFor like this:

import org.apache.kafka.common.serialization.Serde
import com.sksamuel.avro4s.kafka.GenericSerde
import com.sksamuel.avro4s.BinaryFormat
import com.sksamuel.avro4s.SchemaFor
import scala.collection.JavaConverters._

implicit val myClassSchemaFor: SchemaFor[MyClass] = SchemaFor[MyClass]
implicit val inputSerde: Serde[MyClass] = new GenericSerde[MyClass](BinaryFormat)

val serdeConfig = Map("schema.registry.url" -> "SOME_URL").asJava
inputSerde.configure(serdeConfig, false)

But then I got this error during runtime:

[error]   caused by java.lang.ExceptionInInitializerError
[error]   caused by java.lang.NullPointerException
[error]   sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error]   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[error]   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[error]   java.lang.reflect.Constructor.newInstance(Constructor.java:423)
....

[error]
[error]   CAUSED BY java.lang.ExceptionInInitializerError
[error]   com.sksamuel.avro4s.AvroSchema$.apply(AvroSchema.scala:20)
[error]   com.sksamuel.avro4s.kafka.GenericSerde.<init>(GenericSerde.scala:21)
...

Any idea how to solve or debug this problem ?

These are the main dependencies:

"org.apache.avro"          % "avro"                   % "1.10.2"
"com.sksamuel.avro4s" %% "avro4s-core"  % "4.1.1",
"com.sksamuel.avro4s" %% "avro4s-kafka" % "4.1.1"
"io.confluent"                   % "kafka-streams-avro-serde" % "5.4.5"
"com.chuusai"                  %% "shapeless"     % "2.3.3"

sbt-avrohugger version 2.8.0 scala version: 2.12.18 Thanks