【问题标题】:Spray-JSON in an Apache Zeppelin NotebookApache Zeppelin Notebook 中的 Spray-JSON
【发布时间】:2017-11-23 09:29:40
【问题描述】:

我正在使用Zeppelin notebooksSpark Streaming 应用程序编写原型。它通过事件总线接收小的JSON 消息,我需要以(最好)分布式方式解析这些消息。我选择spray-json 来反序列化各个消息,但我似乎无法让它工作。我认为这是因为Zeppelin notebooks 是通过某种 REPL 接口解释的。

我直接从docs复制了这个样本:

case class Color(name: String, red: Int, green: Int, blue: Int)
object Color

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val colorFormat = jsonFormat4(Color.apply)
}

但它给了我以下输出:

defined class Color
defined object Color
warning: previously defined class Color is not a companion to object Color.
Companions must be defined together; you may wish to use :paste mode for this.
<console>:75: error: value apply is not a member of object Color
 Note: implicit value colorFormat is not applicable here because it comes after the application point and it lacks an explicit result type
         implicit val colorFormat = jsonFormat4(Color.apply)

还有其他方法可以让我反序列化Zeppelin notebook 中的消息吗?我不受spray-json 的约束,但它看起来确实是一个不错的库。

【问题讨论】:

  • Scala REPL 有一个粘贴模式。不确定 Zeppelin 中是否可用。你可以试试case class Color(name: String, red: Int, green: Int, blue: Int); object Color;
  • issues.apache.org/jira/browse/ZEPPELIN-658 你也可以在你的解释器中试试:paste 吗?
  • 这也是 scala-repl 的问题。这个@philantrovert有一个骗局,我不知道它现在在哪里:/
  • :paste 不起作用,我应该提到我已经尝试过了。 @philantrovert 提供的解决方案效果很好,谢谢!

标签: scala apache-spark spray apache-zeppelin spray-json


【解决方案1】:

感谢@philantrovert 的评论,我能够让它工作。诀窍是将类和对象声明放在同一行,如下所示:

case class Color(name: String, red: Int, green: Int, blue: Int); object Color;

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 2017-10-31
    • 2017-11-04
    • 2016-09-09
    • 2017-06-01
    • 2015-10-25
    • 2017-06-22
    • 2017-03-04
    • 2018-12-01
    相关资源
    最近更新 更多