简体   繁体   English

静默模式下通过spark-shell执行scala脚本

[英]Execute the scala script through spark-shell in silent mode

Need to execute the scala script through spark-shell with silent mode.需要在静默模式下通过 spark-shell 执行 scala 脚本。 When I am using spark-shell -i "file.scala" , after the execution, I am getting into the scala interactive mode.当我使用spark-shell -i "file.scala" ,执行后,我进入了 scala 交互模式。 I don't want to get into there.我不想进入那里。

I have tried to execute the spark-shell -i "file.scala".我试图执行 spark-shell -i "file.scala"。 But I don't know how to execute the script in silent mode.但我不知道如何在静默模式下执行脚本。

spark-shell -i "file.scala"

after execution, I get into执行后,我进入

scala>

I don't want to get into the scala> mode我不想进入scala>模式

Updating (October 2019) for a script that terminates更新(2019 年 10 月)终止脚本

This question is also about running a script that terminates, that is, a "scala script" that run by spark-shell -i script.scala > output.txt that stopts by yourself (internal instruction System.exit(0) terminates the script).这个问题也是关于运行一个终止的脚本,即一个由spark-shell -i script.scala > output.txt运行的“scala 脚本”,它自己停止(内部指令System.exit(0)终止脚本)。
See this question with a good example .用一个很好的例子这个问题

It also needs a "silent mode" , it is expected to not pollute the output.txt .它还需要一个“静默模式”预计不会污染output.txt

Suppose Spark v2.2+ .假设Spark v2.2+


PS: there are a lot of cases (typically small tools and module/algorithm tests) where Spark interpreter can be better than compiler ... Please, "let's compile!" PS:在很多情况下(通常是小工具和模块/算法测试), Spark 解释器可以比编译器更好......请“让我们编译!” is not an answer here.不是这里的答案。

spark-shell -i file.scala keeps the interpreter open in the end, so System.exit(0) is required to be at the end of your script. spark-shell -i file.scala保持解释器打开,因此System.exit(0)需要位于脚本的末尾。 The most appropriate solution is to place your code in try {} and put System.exit(0) in finally {} section.最合适的解决方案是将您的代码放入try {}并将System.exit(0)放入finally {}部分。

If logging is requiered you can use something like this:如果需要记录日志,您可以使用以下内容:

spark-shell < file.scala > test.log 2>&1 &

If you have limitations on editing file and you can't add System.exit(0) , use:如果您对编辑文件有限制并且无法添加System.exit(0) ,请使用:

echo :quit | scala-shell -i file.scala

UPD UPD

If you want to suppress everything in output except printlns you have to turn off logging for spark-shell.如果你想抑制输出中除了 printlns 之外的所有内容,你必须关闭 spark-shell 的日志记录。 The sample of configs is here .配置示例在这里 Disabling any kind of logging in $SPARK-HOME/conf/log4j.properties should allow you to see only pritnlns.$SPARK-HOME/conf/log4j.properties禁用任何类型的日志记录应该只允许您看到 pritnlns。 But I would not follow this approach with printlns.但是我不会在 printlns 中遵循这种方法。 Using general Logging with log4j should be used instead of printlns.应该使用带有 log4j 的通用日志记录而不是 printlns。 You can configure it so obtain the same results as with printlns.您可以对其进行配置,以获得与 printlns 相同的结果。 It boils down to configuring a pattern.它归结为配置模式。 This answer provides an example of a pattern that solves your issue.答案提供了解决您的问题的模式示例。

最好的方法绝对是将您的 Scala 代码编译为 jar 并使用spark-submit但如果您只是在寻找快速迭代循环,您可以在解析您的 Scala 代码后简单地发出:quit

echo :quit | scala-shell -i yourfile.scala

Adding onto @rluta's answer.添加到@rluta 的答案中。 You can place the call to spark-shell command inside a shell script.您可以在 shell 脚本中调用spark-shell命令。 Say the below in a shell script:在 shell 脚本中说以下内容:

spark-shell < yourfile.scala

But this would require you to keep the lines of code within a line in case a statement is written on different lines.但这将要求您将代码行保留在一行中,以防将语句写在不同的行上。

OR或者

echo :quit | spark-shell -i yourfile.scala

This should这应该

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM