推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html
全部博文(595)
分类: HADOOP
2016-02-17 12:51:57
本文约定Hadoop 2.7.1安装在/data/hadoop/current,而Spark 1.6.0被安装在/data/hadoop/spark,其中/data/hadoop/spark为指向/data/hadoop/spark。
Spark官网为:(Shark官网为:,Shark已成为Spark的一个模块,不再需要单独安装)。
以cluster模式运行Spark,不介绍client模式。
联邦理工学院洛桑(EPFL)的Martin Odersky于2001年基于Funnel的工作开始设计Scala。
Scala是一种多范式的编程语言,设计初衷是要集成纯面向对象编程和函数式编程的各种特性。运行在Java虚拟机JVM之上,兼容现有的Java程序,并可调用Java类库。Scala包含编译器和类库,以BSD许可证发布。
Spark使用Scala开发的,在安装Spark之前,先在各个节上将Scala安装好。Scala的官网为:,下载网址为:,本文下载的是二进制安装包scala-2.11.7.tgz。
本文以root用户(实则也可以非root用户,建议事先规划好)将Scala安装在/data/scala,其中/data/scala是指向/data/scala-2.11.7的软链接。
安装方法非常简单,将scala-2.11.7.tgz上传到/data目录,然后在/data/目录下对scala-2.11.7.tgz进行解压。
接着,建立软链接:ln -s /data/scala-2.11.7 /data/scala。
Scala被安装完成后,需要将它添加到PATH环境变量中,可以直接修改/etc/profile文件,加入以下内容即可:
export SCALA_HOME=/data/scala export PATH=$SCALA_HOME/bin:$PATH |
Spark的安装以非root用户进行,本文以hadoop用户安装它。
本文下载的二进制安装包,推荐这种方式,否则编译还得折腾。下载网址为:,本文下载的是spark-1.6.0-bin-hadoop2.6.tgz,这个可以直接跑在YARN上。
1) 将spark-1.6.0-bin-hadoop2.6.tgz上传到目录/data/hadoop下
2) 解压:tar xzf spark-1.6.0-bin-hadoop2.6.tgz
3) 建立软链接:ln -s spark-1.6.0-bin-hadoop2.6 spark
在yarn上运行spark,不需要每台机器都安装spark,可以只安装在一台机器上。但是只能在被安装的机器上运行spark,原因很简单:需要调用spark的文件。
可以spark-env.sh.template复制一份,然后增加以下内容:
HADOOP_CONF_DIR=/data/hadoop/current/etc/hadoop YARN_CONF_DIR=/data/hadoop/current/etc/hadoop |
由于运行在Yarn上,所以没有启动Spark这一过程。而是在执行命令spark-submit时,由Yarn调度运行Spark。
./bin/spark-submit --class org.apache.spark.examples.SparkPi \ --master yarn --deploy-mode cluster \ --driver-memory 4g \ --executor-memory 2g \ --executor-cores 1 \ --queue default \ lib/spark-examples*.jar 10 |
运行输出:
16/02/03 16:08:33 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:34 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:35 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:36 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:37 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:38 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:39 INFO yarn.Client: Application report for application_1454466109748_0007 (state: RUNNING) 16/02/03 16:08:40 INFO yarn.Client: Application report for application_1454466109748_0007 (state: FINISHED) 16/02/03 16:08:40 INFO yarn.Client: client token: N/A diagnostics: N/A ApplicationMaster host: 10.225.168.251 ApplicationMaster RPC port: 0 queue: default start time: 1454486904755 final status: SUCCEEDED tracking URL: user: hadoop 16/02/03 16:08:40 INFO util.ShutdownHookManager: Shutdown hook called 16/02/03 16:08:40 INFO util.ShutdownHookManager: Deleting directory /tmp/spark-7fc8538c-8f4c-4d8d-8731-64f5c54c5eac |
通过运行即可进入SparkSQL Cli交互界面,但要在Yarn上以cluster运行,则需要指定参数--master值为yarn(注意不支持参数--deploy-mode的值为cluster,也就是只能以client模式运行在Yarn上):
./bin/spark-sql --master yarn |
为什么SparkSQL Cli只能以client模式运行?其实很好理解,既然是交互,需要看到输出,这个时候cluster模式就没法做到了。因为cluster模式,ApplicationMaster在哪机器上运行,是由Yarn动态确定的。
Spark集成Hive非常简单,只需以下几步:
1) 在spark-env.sh中加入HIVE_HOME,如:export HIVE_HOME=/data/hadoop/hive
2) 将Hive的hive-site.xml和hive-log4j.properties两个文件复制到Spark的conf目录下。
完成后,再次执行spark-sql进入Spark的SQL Cli,运行命令show tables即可看到在Hive中创建的表。
示例:
./spark-sql --master yarn --driver-class-path /data/hadoop/hive/lib/mysql-connector-java-5.1.38-bin.jar
Spark的Java编程示例:。
package testspark;
import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaSparkContext; import org.apache.spark.sql.Row; import org.apache.spark.sql.hive.HiveContext;
public class SparkSQLHiveOnYarn { public static void main(String[] args) throws Exception { System.out.println("start"); SparkConf sparkConf = new SparkConf().setAppName("SparkSQLHiveOnYarnTest"); JavaSparkContext ctx = new JavaSparkContext(sparkConf); HiveContext hc = new HiveContext(ctx.sc()); hc.sql("use default"); // 选择使用哪个DB Row[] result = hc.sql("select count(1) from test").collect(); System.out.println(result[0]); ctx.stop(); } } |
打包成jar后,运行(假设jar包放在/tmp目录下):
spark-submit --master yarn \ --class testspark.SparkSQLHiveOnYarn \ --driver-memory 4G \ --driver-java-options "-XX:MaxPermSize=4G" \ --verbose \ --jars $HIVE_HOME/lib/mysql-connector-java-5.1.38-bin.jar \ /tmp/testspark.jar |
运行:
./bin/spark-submit --class org.apache.spark.examples.SparkPi --master yarn --deploy-mode cluster --driver-memory 4g --executor-memory 2g --executor-cores 1 --queue thequeue lib/spark-examples*.jar 10
时报如下错误,只需要将“--queue thequeue”改成“--queue default”即可。
16/02/03 15:57:36 INFO yarn.Client: Application report for application_1454466109748_0004 (state: FAILED) 16/02/03 15:57:36 INFO yarn.Client: client token: N/A diagnostics: Application application_1454466109748_0004 submitted by user hadoop to unknown queue: thequeue ApplicationMaster host: N/A ApplicationMaster RPC port: -1 queue: thequeue start time: 1454486255907 final status: FAILED tracking URL: user: hadoop 16/02/03 15:57:36 INFO yarn.Client: Deleting staging directory .sparkStaging/application_1454466109748_0004 Exception in thread "main" org.apache.spark.SparkException: Application application_1454466109748_0004 finished with failed status at org.apache.spark.deploy.yarn.Client.run(Client.scala:1029) at org.apache.spark.deploy.yarn.Client$.main(Client.scala:1076) at org.apache.spark.deploy.yarn.Client.main(Client.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:731) at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:181) at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:206) at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:121) at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 16/02/03 15:57:36 INFO util.ShutdownHookManager: Shutdown hook called 16/02/03 15:57:36 INFO util.ShutdownHookManager: Deleting directory /tmp/spark-54531ae3-4d02-41be-8b9e-92f4b0f05807 |
SPARK_CLASSPATH was detected (set to '/data/hadoop/hive/lib/mysql-connector-java-5.1.38-bin.jar:').
This is deprecated in Spark 1.0+.
Please instead use:
- ./spark-submit with --driver-class-path to augment the driver classpath
- spark.executor.extraClassPath to augment the executor classpath
意思是不推荐在spark-env.sh中设置环境变量SPARK_CLASSPATH,可以改成如下推荐的方式:
./spark-sql --master yarn --driver-class-path /data/hadoop/hive/lib/mysql-connector-java-5.1.38-bin.jar
《HBase-0.98.0分布式安装指南》
《Hive 0.12.0安装指南》
《ZooKeeper-3.4.6分布式安装指南》
《Hadoop 2.3.0源码反向工程》
《在Linux上编译Hadoop-2.4.0》
《Accumulo-1.5.1安装指南》
《Drill 1.0.0安装指南》
《Shark 0.9.1安装指南》
更多,敬请关注技术博客:http://aquester.cublog.cn。