简体   繁体   English

如何使用 scala 在 Apache Spark 中运行 shell 文件

[英]How to run a shell file in Apache Spark using scala

I need to execute a shell file at the end of my code in spark using scala.我需要在我的代码末尾使用 scala 在 spark 中执行一个 shell 文件。 I used count and groupby functions in my code.我在代码中使用了 count 和 groupby 函数。 I should mention that, my code works perfectly without the last line of code.我应该提到的是,我的代码在没有最后一行代码的情况下也能完美运行。

(import sys.process._
/////////////////////////linux commands
val procresult="./Users/saeedtkh/Desktop/seprator.sh".!!)

could you please help me how to fix it.你能帮我解决这个问题吗?

You must use sys.process._ package from Scala SDK and use DSL with !您必须使用 Scala SDK 中的sys.process._包并使用 DSL 和! :

import sys.process._
val result = "ls -al".!

Or make same with scala.sys.process.Process :或与scala.sys.process.Process相同:

import scala.sys.process._
Process("cat data.txt")!

You almost there.你快到了。 You need to add .!!你需要添加.!! to the end of the command.For example:到命令的末尾。例如:

import sys.process._
val a = Seq("sh", "-c", " cal ").!!
println(a)

The reason i used Seq, is that you have more control about the parameters that you pass.我使用 Seq 的原因是您可以更好地控制传递的参数。 see this post: How to execute shell builtin from Scala请参阅这篇文章: 如何从 Scala 执行内置的 shell

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

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