有没有办法强制等待Java进程结束以Scala或Spark结尾?

时间:2019-05-13 08:16:00

标签: java scala apache-spark

在通过Spark部署的Scala应用中,我有一条代码行,该行调用通过JNI执行本机C ++代码的Java函数。此调用需要时间,并且如果不是唯一的运行方法,则会出现资源使用冲突,并带有*** stack smashing detected ***: <unknown> terminated

这里是电话,它的范围是:

[spark RDD].mapPartitionsWithIndex(f = (index: Int, it: Iterator[Row]) => {
  val sourceData: String = it.mkString()

  val result: List[List[String]] = new WrapperClass(sourceData, [misc parameters).getResult

  [wrinting result to a file]
}).take(1)

我的WrapperClass.getResult非常简单,看起来像这样:

[java call related variables initialization]

UnitexJni.execUnitexTool("UnitexTool {InstallLingResourcePackage -p " + appliedPkg + " -x " + resDir + "} " + "{" + runScriptCmd + "} " + "{InstallLingResourcePackage -p " + appliedPkg + " -x " + resDir + " --uninstall}")

[retrieving, formatting and returning result]

UnitexJni.execUnitexTool()是Java调用。

所以我想知道是否有一种方法可以强制使用此过程,直到使用Scala,Java或Spark功能重新调用它。

1 个答案:

答案 0 :(得分:1)

您可以使用sys.process._,您将通过脚本路径将shell脚本传递给以下流程函数。另外,您需要处理shell脚本以获取返回码。例如,If 0 success else failed。请注意行末的!。您还可以查看更多详细信息,以从此tutorial

运行快速命令行
import scala.sys.process.Process
val externalShellScript = Process("sh", Seq(scriptPath)).!    
  if (externalShellScript != 0) {
    throw new Exception(
      "Error in executing external shell script from " + scriptPath)
  }

除非完成此过程,否则Spark作业将不会继续。下面是简单的shell脚本和输出。

touch test.txt
echo "any massage"
控制台中的

输出将为

any massage