要使用Spark编写九九乘法表,可以按照以下步骤进行:
import org.apache.spark._
val conf = new SparkConf().setAppName("MultiplicationTable")
val sc = new SparkContext(conf)
val table = sc.parallelize(1 to 9)
val result = table.flatMap(i => (1 to i).map(j => (i, j, i*j)))
result.collect().foreach(println)
sc.stop()
完整的代码如下:
import org.apache.spark._
object MultiplicationTable {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("MultiplicationTable")
val sc = new SparkContext(conf)
val table = sc.parallelize(1 to 9)
val result = table.flatMap(i => (1 to i).map(j => (i, j, i*j)))
result.collect().foreach(println)
sc.stop()
}
}
通过运行这段代码,就可以生成并打印出九九乘法表的结果。