首页 > 解决方案 > IntelliJ 在 udf 函数上构建了一个有 2 个错误的项目 - 没有可用于 Option[Seq[Testclass]] 的 TypeTag

问题描述

我正在通过 IntelliJ 为 scala 程序构建产品。我构建了另一个产品,没有任何错误。

我没有任何其他尝试,因为这是我第一次使用 class/udf 构建那种代码,我只是得到了一些代码的参考

    case class Testclass(
                      col1:Option[java.sql.Timestamp],
                      col2:Option[String],
                      col3:Option[Int],
                      col4:Option[Long],
                      col5:Option[Double],
                    )

    val function = udf((l: Seq[Row], id: String, ts: Timestamp) => scala.util.Try {
      l
        .filter(c => id.isEmpty || c.getAs[String]("order_transaction_id").equalsIgnoreCase(id))
        .filter(c => if (ts != null) c.getAs[Timestamp]("jump_timestamp").before(ts) else true)
        .map { c =>
          Testclass(
            if (!c.isNullAt(0)) Some(c.getAs[Timestamp](0)) else None,
            if (!c.isNullAt(1)) Some(c.getString(1)) else None,
            if (!c.isNullAt(2)) Some(c.getInt(2)) else None,
            if (!c.isNullAt(3)) Some(c.getLong(3)) else None,
            if (!c.isNullAt(4)) Some(c.getDouble(4)) else None
          )
        }
        .sortWith((c1, c2) => c1.his_ts.get.before(c2.his_ts.get))
    }.toOption)

    val dataFrame2 = dataFrame1
      .withColumn("column_ABC", function(col("event_lists"), col("event_string"), col("event_timestamp")))
    //source is a parquet file, but a little similar with json format
    //event_lists is an array for this parquet, all the elements are same as the definition of Testclass
    //event_string is a string field
    //event_timestamp is a timestamp field

预期结果:构建应该成功,没有任何错误

标签: scalauser-defined-functions

解决方案


对不起,我想通了

只需将代码与大师班放在同一级别

case class Testclass(
                  col1:Option[java.sql.Timestamp],
                  col2:Option[String],
                  col3:Option[Int],
                  col4:Option[Long],
                  col5:Option[Double],
                )

推荐阅读