參考:html
英文:https://spark.apache.org/docs/latest/programming-guide.htmljava
中文:http://www.cnblogs.com/lujinhong2/p/4651025.html 1.2.1版本的apache
(一)快速入門app
老規矩,先看一個簡單示例,有個認識。這個示例來自官方example的SparkPi:dom
package org.lujinhong.demo.spark /* * 官方的sparkPi示例 */ import scala.math.random import org.apache.spark._ /** Computes an approximation to pi */ object SparkPi { def main(args: Array[String]) { val conf = new SparkConf().setAppName("Spark Pi").setMaster("local") val spark = new SparkContext(conf) val slices = if (args.length > 0) args(0).toInt else 2 val n = math.min(100000L * slices, Int.MaxValue).toInt // avoid overflow val count = spark.parallelize(1 until n, slices).map { i => val x = random * 2 - 1 val y = random * 2 - 1 if (x*x + y*y < 1) 1 else 0 }.reduce(_ + _) println("Pi is roughly " + 4.0 * count / n) spark.stop() } }
注意以上的setMaster(「local」)是本身加上去的,方便直接在本地運行。若是在集羣上運行,則經過spark-submit的—master參數指定。eclipse
寫好代碼後,就能夠直接在eclipse中右鍵—>運行了。ide