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

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

我正在尝试读取大小写 class 的 avro 文件: UserItemIds 包括大小写 class 类型: User , sbtscala 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()

但收到:错误:

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

谁能指导我如何使用这些类型?此示例使用 avro 个文件,但这可以是 parquet 或任何自定义 DB 输入。

我需要使用 TypeInformation 吗?例如:如果是,该怎么做?

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

我也看到了 env.registerType() ,它与问题完全相关吗?非常感谢任何帮助。

我找到了 java error 的解决方案 Adding a default constructor 在这种情况下,我将工厂方法添加到 scala case class,方法是将其添加到伴随对象

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

但这并没有解决问题

您必须为 UserUserItemIds 类型添加默认构造函数。这可能看起来像以下方式:

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())
}