簡體   English   中英

有沒有一種方法可以強制等待Java進程結束在Scala或Spark中結束?

[英]Is there a way to force waiting for the end of a java process to end in Scala or 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功能重新調用它為止。

您可以使用sys.process._您將通過腳本路徑將shell腳本傳遞給以下流程函數。 另外,您需要處理shell腳本以獲取返回碼。 例如, If 0 success else failedIf 0 success else failed 請保重! 在該行的末尾。 您也可以從本教程中查看更多詳細信息以運行快速命令行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM