问题
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