如何使用scala在spark中并行执行多个函数?

如何使用scala在spark中并行执行多个函数?,scala,apache-spark,apache-spark-sql,Scala,Apache Spark,Apache Spark Sql,如何使用scala在spark batch中并行执行多个函数 def main(args: Array[String]) { def func1() { // dataframe 1 write to oracle database table 1 } def func2() { // dataframe 2 write to oracle database table 2 } def func3() { // dataframe 3 write to oracle data

如何使用scala在spark batch中并行执行多个函数

 def main(args: Array[String]) {
 def func1() {
 // dataframe 1 write to oracle database table 1
 }
 def func2() {
 // dataframe 2 write to oracle database table 2
 }
 def func3() { 
 // dataframe 3 write to oracle database table 3
 }
}

一般来说,可以通过使用Futures。。。按照下面的示例,您可以自己尝试

然后

scala> sc.parallelize( 1 to 10).map(fastFoo).map(x => ConcurrentContext.executeAsync(slowFoo(x))).collect
fastFoo(1)
fastFoo(2)
fastFoo(3)
fastFoo(4)
slowFoo start (2)
slowFoo start (1)
fastFoo(5)
slowFoo start (3)
  ...
res6: Array[scala.concurrent.Future[Int]] = Array(List(), List(), List(), List(), List(), List(), List(), List(), List(), List())

scala>  // Our request returns
//Then 5 seconds later
slowFoo end(1)
slowFoo end(7)
slowFoo end(8)
slowFoo end(4)
slowFoo start (10)
slowFoo end(5)

你的问题不清楚,但简单地使用多线程应该可以解决这个问题。你可能只需要使用Future。使用spark scala,我正在使用将3个不同的转换数据帧写入rdbms中的不同表,这应该并行执行。
scala> sc.parallelize( 1 to 10).map(fastFoo).map(x => ConcurrentContext.executeAsync(slowFoo(x))).collect
fastFoo(1)
fastFoo(2)
fastFoo(3)
fastFoo(4)
slowFoo start (2)
slowFoo start (1)
fastFoo(5)
slowFoo start (3)
  ...
res6: Array[scala.concurrent.Future[Int]] = Array(List(), List(), List(), List(), List(), List(), List(), List(), List(), List())

scala>  // Our request returns
//Then 5 seconds later
slowFoo end(1)
slowFoo end(7)
slowFoo end(8)
slowFoo end(4)
slowFoo start (10)
slowFoo end(5)