zeppelin imported classes not found when using

寵の児 提交于 2019-12-24 02:16:58

问题


I get a weird error when using spark on zeppelin. The imported classes are not found when I use them. The code sample is :

%spark
import java.io.Serializable
import java.text.{ParseException, SimpleDateFormat}
import java.util.{Calendar, SimpleTimeZone}

class Pos(val pos: String) extends Serializable {

if (pos.length != 12) {
  throw new IllegalArgumentException(s"[${pos}] seems not a valid pos 
 string")
}

private val cstFormat = new SimpleDateFormat("yyyyMMddHHmm")
private val utcFormat = new SimpleDateFormat("yyyyMMddHHmm")
}

I get the following errors:

import java.io.Serializable
import java.text.{ParseException, SimpleDateFormat}
import java.util.{Calendar, SimpleTimeZone}
<console>:17: error: not found: type SimpleDateFormat
     private val cstFormat = new SimpleDateFormat("yyyyMMddHHmm")
                                 ^
<console>:18: error: not found: type SimpleDateFormat
     private val utcFormat = new SimpleDateFormat("yyyyMMddHHmm")
                                 ^
<console>:25: error: not found: type ParseException
       case e: ParseException => throw newIllegalArgumentException(s"
               ^

Is there any method to solve the error? The zeppelin version is 0.7.3 and spark version is 2.1 Thanks in advance!


回答1:


It seems you have to write imports within class definition :

%spark
class Pos(val pos: String) extends Serializable {

    import java.io.Serializable
    import java.text.{ParseException, SimpleDateFormat}
    import java.util.{Calendar, SimpleTimeZone}

    if (pos.length != 12) {
      throw new IllegalArgumentException(s"[${pos}] seems not a valid pos string")
    }

    private val cstFormat = new SimpleDateFormat("yyyyMMddHHmm")
    private val utcFormat = new SimpleDateFormat("yyyyMMddHHmm")
}

If you need your imports for the arguments of your class constructor you can create your class within an object and then call YourObject.YourClass(args) in the Following paragraphs. See this question for another example.




回答2:


In Zeppelin, you have to import everything on the same line, separating with ; to make it work:

import java.io.Serializable; import java.text.{ParseException, SimpleDateFormat}; import java.util.{Calendar, SimpleTimeZone}; class Pos(val pos: String) extends Serializable {

if (pos.length != 12) {
  throw new IllegalArgumentException(s"[${pos}] seems not a valid pos 
 string")
}

private val cstFormat = new SimpleDateFormat("yyyyMMddHHmm")
private val utcFormat = new SimpleDateFormat("yyyyMMddHHmm")
}


来源:https://stackoverflow.com/questions/49936710/zeppelin-imported-classes-not-found-when-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!