elcomendante
elcomendante

Reputation: 1161

Flink read custom types - implicit value error: `Caused by: java.lang.NoSuchMethodException: <init>()`

I am trying to read avro file with case class: UserItemIds that includes case class type: User , sbt and scala 2.11

case class User(id: Long, description: String)

case class UserItemIds(user: User, itemIds: List[Long])


val UserItemIdsInputStream = env.createInput(new AvroInputFormat[UserItemIds](user_item_ids_In, classOf[UserItemIds]))

UserItemIdsInputStream.print()

but receive: Error:

Caused by: java.lang.NoSuchMethodException: schema.User.<init>()

Can anyone guide me how to work with these types please? This example is with avro files, but this could be parquet or any custom DB input.

Do I need to use TypeInformation ? ex:, if yes how to do so?

 val tupleInfo: TypeInformation[(User, List[Long])] = createTypeInformation[(User, List[Long])]

I also saw env.registerType() , does it relate to the issue at all? Any help is greatly appreciated.

I found the solution to this java error as Adding a default constructor in this case I added factory method to scala case class by adding it to the companion object

object UserItemIds{
case class UserItemIds(
                      user: User,
                      itemIds: List[Long])
       def apply(user:User,itemIds:List[Long]) = new 
       UserItemIds(user,itemIds)}

but this has not resolved the issue

Upvotes: 0

Views: 312

Answers (1)

Till Rohrmann
Till Rohrmann

Reputation: 13346

You have to add a default constructor for the User and UserItemIds type. This could look the following way:

case class User(id: Long, description: String) {
  def this() = this(0L, "")
}

case class UserItemIds(user: User, itemIds: List[Long]) {
  def this() = this(new User(), List())
}

Upvotes: 1

Related Questions