Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodException for init method in Scala case class

I am writing an Apache Flink streaming application that deserializes data (Avro format) read off a Kafka bus (more details on here). The data is being deserialized into a Scala case class. I am getting an exception when i run the program and it received the first message from Kafka

Exception in thread "main" org.apache.flink.runtime.client.JobExecutionException: java.lang.RuntimeException: java.lang.NoSuchMethodException: org.myorg.quickstart.DeviceData.<init>()
    at org.apache.flink.runtime.minicluster.MiniCluster.executeJobBlocking(MiniCluster.java:625)
    at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.execute(LocalStreamEnvironment.java:121)
    at org.apache.flink.streaming.api.scala.StreamExecutionEnvironment.execute(StreamExecutionEnvironment.scala:654)
    at org.myorg.quickstart.StreamingKafkaClient$.main(StreamingKafkaClient.scala:26)
    at org.myorg.quickstart.StreamingKafkaClient.main(StreamingKafkaClient.scala)
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodException: org.myorg.quickstart.DeviceData.<init>()
    at org.apache.avro.specific.SpecificData.newInstance(SpecificData.java:353)
    at org.apache.avro.specific.SpecificData.newRecord(SpecificData.java:369)
    at org.apache.avro.reflect.ReflectData.newRecord(ReflectData.java:901)
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:212)
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145)
    at org.myorg.quickstart.AvroDeserializationSchema.deserialize(AvroDeserializationSchema.scala:20)
    at org.apache.flink.streaming.util.serialization.KeyedDeserializationSchemaWrapper.deserialize(KeyedDeserializationSchemaWrapper.java:44)
    at org.apache.flink.streaming.connectors.kafka.internal.Kafka09Fetcher.runFetchLoop(Kafka09Fetcher.java:142)
    at org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase.run(FlinkKafkaConsumerBase.java:738)
    at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:87)
    at org.apache.flink.streaming.api.operators.StreamSource.run(StreamSource.java:56)
    at org.apache.flink.streaming.runtime.tasks.SourceStreamTask.run(SourceStreamTask.java:99)
    at org.apache.flink.streaming.runtime.tasks.StreamTask.invoke(StreamTask.java:306)
    at org.apache.flink.runtime.taskmanager.Task.run(Task.java:703)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: org.myorg.quickstart.DeviceData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.apache.avro.specific.SpecificData.newInstance(SpecificData.java:347)
    ... 16 more

Process finished with exit code 1

The Scala case class is very simple:

package org.myorg.quickstart


/** Case class to hold the Device data. */
case class DeviceData(deviceId: String,
                    sw_version: String,
                    timestamp: String,
                    reading: Double
                   ) 

Not sure why an "init" method is needed for the case class. An examples of how to do this? Should i be using a different data structure other than the case class?

like image 777
Amit Arora Avatar asked Jul 02 '18 05:07

Amit Arora


People also ask

What is a Java nosuchmethodexception?

a $2 donation will help keep this site running! Java exception FAQ: What is a Java NoSuchMethodException? Answer: Using Java, you can get a NoSuchMethodException when you're using reflection and try to dynamically use a method on a class, and the method does not actually exist.

Why do I get a nosuchmethodexception when using reflection?

Answer: Using Java, you can get a NoSuchMethodException when you're using reflection and try to dynamically use a method on a class, and the method does not actually exist. The following example Java class shows how this NoSuchMethodException can be generated:

What is “nosuchmethoderror”?

This method can either be an instance method or a static method. The java.lang.NoSuchMethodError occurs when an application does not find a method at runtime. In most cases, we’re able to catch this error at compile-time. Hence, it’s not a big issue.


1 Answers

The Avro serializer or more specifically the SpecificData requires the target type to have a default constructor (constructor with no arguments). Otherwise Avro cannot instantiate an object of the target type.

Try to add a default constructor via

case class DeviceData(
    deviceId: String,
    sw_version: String,
    timestamp: String,
    reading: Double) {
  def this() = this("default", "default", "default", 0)
} 
like image 148
Till Rohrmann Avatar answered Sep 23 '22 12:09

Till Rohrmann