[英]Not able to call function from class (Scala)
我只是想从我在 Scala 中创建的 class(在 databricks 笔记本中)调用一个方法。 不要担心中间的所有代码。 只需关注最底部的SecondExplode
class 和registerUDF
function。 class如下:
case class SecondExplodeTimes(start_dt: String, end_dt: String, counter: Int)
class SecondExplode extends Serializable {
def expandDatetimeRangeToStartAndEndSeconds(start: String, end: String, ceilingLimit: Int): Seq[SecondExplodeTimes] =
{
val start_time: DateTime = DateTime.parse(start, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
val end_time: DateTime = DateTime.parse(end, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
val secBetween = end_time.getSecondOfMinute - start_time.getSecondOfMinute
(1 to secBetween).zipWithIndex.map{case (p, counter) => {
val strt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(start_time.plusSeconds(p-1))
val endt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(start_time.plusSeconds(p))
SecondExplodeTimes(strt, endt, counter)
}}.toSeq
}
def registerUDF: UserDefinedFunction = {
val spark = SparkSession.builder().getOrCreate()
spark.udf.register("second_explode", expandDatetimeRangeToStartAndEndSeconds _)
}
}
val secondexplode = new SecondExplode.registerUDF
我收到此错误:
error: not found: value SecondExplode. val secondexplode = new SecondExplode.registerUDF
我相信我使用的是 Scala 2 版本,所以语法是正确的。 简单地说,我试图从SecondExplode
class 中调用registerUDF
function 来测试它是否真的有效。 先感谢您。
我相信你想要的是(new SecondExplode).registerUDF
。
问题在于解析优先级。 .
优先级高于new
。 编译器假定您正在尝试创建SecondExplode.registerUDF
的new
实例,而不是创建new SecondExplode
然后在实例上调用registerUDF
方法。
这可能是一种误解的原因是没有什么可以阻止您这样做:
object SecondExplode {
class registerUDF {}
}
在这种情况下, new SecondExplode.registerUDF
会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.