开始搭建的jdk这些自不必说,本文只是简单的介绍安装scala/spark
1.下载scala安装包
去官网下载tgz包,解压在/opt/scala/下,设置环境变量:
export SCALA_HOME=/opt/scala/scala-2.10.3 export PATH=$SCALA_HOME/bin:$PATH
设置完成后,就可以了,在命令行里测试安装是否正确:#scala 会进入类似于Mysql的命令输入模式,就说明已经安装成功了。(我之前下载的是rpm包,但是通过rpm命令安装后,使用的是默认安装,都不知道安装在哪里了,如果不熟的同学建议还是通过解压的方式,这样我们可以很好的设置环境变量什么的)
rpm卸载已安装的包:rpm -e test app_name 先看有没有依赖等错误提示,如果没有的话,可以放心的使用:rpm -e app_name删除了。
scala下载地址:http://www.scala-lang.org/download/2.10.3.html
2.下载spark安装包
依然是下载tgz包到:/opt/spark/ 下,然后进行配置。配置文件:/conf/spark-env.sh(这个文件本来没有,需要把spark-env.sh.template名字改成这个)。
目前spark环境不依赖Hadoop,也就不需要Mesos,所以配置的东西很少,配置信息详见:http://spark.incubator.apache.org/docs/latest/configuration.html 这个页面的最下解释区。
我的配置信息:
export SCALA_HOME=/opt/scala-2.10.3 export JAVA_HOME=/usr/java/jdk1.7.0_17
配置好了之后,好像也就可以了。根据官网的“Quick Start”,我们就快速体验下吧!
-------------------------------------------------------------------------------------------
1.built Spark
sbt/sbt assembly #使用此命令需要在工程目录的home下
命令完成后,就会下载插件或jar包,效果如下:
SBT是Simple Build Tool的简称,如果读者使用过Maven,那么可以简单将SBT看做是Scala世界的Maven,虽然二者各有优劣,但完成的工作基本是类似的。
上面的命令:sbt assembly 愚下认为是使用的sbt-assembly插件,这个插件的目的是:
经过此命令编译后的结果是:
[info]部分说得挺清楚,就是编译后的jar文件在:/opt/spark/spark-0.9.0-incubating/assembly/target/scala-2.10/spark-assembly-0.9.0-incubating-hadoop1.0.4.jar
将这个文件添加到CLASSPATH(位置应该是在conf/spark-env.sh中加入,参考本文最下面的一张参考配置图),就可以创建Spark应用(当然通过>[bin]#./spark-shell命令进入的是Scala解释器环境,所以需要编译。)
在解释器环境下测试Spark:(Spark交互模式)
scala> var data=Array(1,2,3,4,5,6) data: Array[Int] = Array(1, 2, 3, 4, 5, 6) scala> val distData = sc.parallelize(data) distData: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:14 scala> distData.reduce(_+_) 14/02/28 18:15:54 INFO SparkContext: Starting job: reduce at <console>:17 14/02/28 18:15:54 INFO DAGScheduler: Got job 0 (reduce at <console>:17) with 1 output partitions (allowLocal=false) 14/02/28 18:15:54 INFO DAGScheduler: Final stage: Stage 0 (reduce at <console>:17) 14/02/28 18:15:54 INFO DAGScheduler: Parents of final stage: List() 14/02/28 18:15:54 INFO DAGScheduler: Missing parents: List() 14/02/28 18:15:54 INFO DAGScheduler: Submitting Stage 0 (ParallelCollectionRDD[0] at parallelize at <console>:14), which has no missing parents 14/02/28 18:15:55 INFO DAGScheduler: Submitting 1 missing tasks from Stage 0 (ParallelCollectionRDD[0] at parallelize at <console>:14) 14/02/28 18:15:55 INFO TaskSchedulerImpl: Adding task set 0.0 with 1 tasks 14/02/28 18:16:00 INFO TaskSetManager: Starting task 0.0:0 as TID 0 on executor localhost: localhost (PROCESS_LOCAL) 14/02/28 18:16:00 INFO TaskSetManager: Serialized task 0.0:0 as 1077 bytes in 88 ms 14/02/28 18:16:01 INFO Executor: Running task ID 0 14/02/28 18:16:02 INFO Executor: Serialized size of result for 0 is 641 14/02/28 18:16:02 INFO Executor: Sending result for 0 directly to driver 14/02/28 18:16:02 INFO Executor: Finished task ID 0 14/02/28 18:16:02 INFO TaskSetManager: Finished TID 0 in 6049 ms on localhost (progress: 0/1) 14/02/28 18:16:02 INFO DAGScheduler: Completed ResultTask(0, 0) 14/02/28 18:16:02 INFO DAGScheduler: Stage 0 (reduce at <console>:17) finished in 6.167 s 14/02/28 18:16:02 INFO TaskSchedulerImpl: Remove TaskSet 0.0 from pool 14/02/28 18:16:02 INFO SparkContext: Job finished: reduce at <console>:17, took 7.928379191 s res0: Int = 21
在Eclipse下开发Spark:
将通过sbt/sbt assembly编译生成的/opt/spark/spark-0.9.0-incubating/assembly/target/scala-2.10/spark-assembly-0.9.0-incubating-hadoop1.0.4.jar 导出,作为创建Scala工程项目时需要的jar引入,就行了(我编译后的jar大小为:83.8 MB (87,878,749 字节))
示例:
(1)工程
(2)代码[代码不报错就说明没问题了~]
当我们在Eclipse上写完代码后,通过Eclipse导出为jar文件,然后编写个shell脚本,就可以在Spark中执行了。
其他Spark环境设置参考:
说明:最后的方法还是去看官网教程!
quick start : http://spark.incubator.apache.org/docs/latest/quick-start.html
configuration : http://spark.incubator.apache.org/docs/latest/configuration.html
推荐一篇针对以前版本的博客介绍,对新的也有一定的参考价值:
http://www.cnblogs.com/jerrylead/archive/2012/08/13/2636115.html
Spark 开发API:
http://spark.incubator.apache.org/docs/latest/api/core/index.html#org.apache.spark.rdd.RDD