在 zeppelin 中定义 case class 失败

Defining a case class in zeppelin fails

我正在使用嵌入到另一个应用程序中的 zeppelin。 zeppelin 版本是 0.6.1-SNAPSHOT。定义以下情况 class 我得到一个错误

case class GenRandInt(lb : Int, ub : Int)
{
 val rnd = new scala.util.Random
 def next() : Int = { lb + rnd.nextInt(ub) }
}

defined class GenRandInt 
<console>:40: error: not found: value lb
               def next() : Int = { lb + rnd.nextInt(ub) }

代码在 IDE 中成功执行。可能出了什么问题?

问题是由于缩进造成的。

详情

scala> :粘贴 // 进入粘贴模式(ctrl-D 完成)

case class GenRandInt(lb : Int, ub : Int)
{
 val rnd = new scala.util.Random
 def next(): Int = { lb + rnd.nextInt(ub) }
}

// Exiting paste mode, now interpreting.

defined class GenRandInt

scala>

在 Scala 中,我们可以使用该缩进定义 case 类。所以我认为这是 zepplin 中 spark 解释器的问题,或者可能是 spark 本身。 (在 scala 2.10.6 上测试,用 scala 2.10.6 构建的 zeppelin 0.7.0-SNAPSHOT)

问题是 Zeppelin 没有 :paste 模式所以第一行:

case class GenRandInt(lb : Int, ub : Int)

... 发送给解释器,然后是下一个块: { val rnd = new scala.util.Random def next() : Int = { lb + rnd.nextInt(ub) } } ...被解释为一个独立的代码块。

当您将 { 与案例 class 放在同一行时,Zeppelin 会理解下一行是前一个代码块的一部分(直到您关闭 }).

我看到与此问题相关的一个技巧是将两个句子放在同一行上,用 ; 分隔。 (例如对于伴生对象),类似于: case class XXX () { ... }; object XXX () { ... }