如何判断我是在 Ammonite repl 下运行还是在脚本中运行?

Posted

技术标签:

【中文标题】如何判断我是在 Ammonite repl 下运行还是在脚本中运行?【英文标题】:How to tell whether i am running under Ammonite repl or in a script? 【发布时间】:2020-06-18 19:37:05 【问题描述】:

我试图使用不同的构建器来构建 SparkSession,而AmmoniteSparkSession 需要 repl。

这行不通,我认为是因为 repl 是编译时错误。

val sparkSessionBuilder =
  try 
    repl
    AmmoniteSparkSession.builder
   catch 
    case e: Exception => SparkSession.builder
  

【问题讨论】:

【参考方案1】:

由于是编译时,我们必须使用Multi-stageScripts根据上下文加载不同的脚本。它应该像here

val sparkSessionModule = sys.env.get("AMMONITE_REPL") match 
  case None => pwd / RelPath("libs/_SparkSession.sc")
  case Some(_) => pwd / RelPath("libs/_AmmoniteSparkSession.sc")

interp.load.module(sparkSessionModule)
@

【讨论】:

以上是关于如何判断我是在 Ammonite repl 下运行还是在脚本中运行?的主要内容,如果未能解决你的问题,请参考以下文章

在 Ammonite (scala) 中重新导入脚本

Ammonite: Script ... 不接受参数(在 OS X 下)

如何使用来自 Ammonite 脚本的资源?

linux打开终端如何启动scala,如何在终端下运行Scala代码片段?

如何在控制台中与我的播放应用程序交互?

如何在node中运行下面的IPFS示例?