当前位置:   article > 正文

Spark之安装和使用

removed taskset 11.0,whose tasks have all completed,from pool

Scala安装

  • Spark使用Scala开发,安装的Scala版本必须和Spark使用的版本一致,否则使用Scala编写的Spark任务会有兼容性问题
  • 可以到Spark官网查看确定Scala版本,或者通过执行Spark的bin/spark-shell查看
  • 依赖jdk,安装之前需要先安装好Scala依赖的最低jdk版本
  • 下载: https://scala-lang.org/download/
  • 解压:[root@master src]# tar xvzf scala-2.11.8.tgz
  • 设置环境变量(可选):
  1. export $SCALA_HOME=/usr/local/src/scala-2.11.8
  2. export $PATH=$PATH:$SCALA_HOME/bin

Spark安装

  • 如果需要部署Spark standalone集群模式,master和slave节点间需要建立ssh互信;如果直接跑在Yarn集群上,则不需要,因为master 和 slave不需要启动
  • 如果只是需要本地调试,不需要启动任何节点,./bin/spark-submit使用本地模式提交任务;本地模式,只是在本地机器模拟执行
  • 如果配置文件中使用的机器名未配置名称和ip的映射,需要在/etc/hosts配置
  • 如果任务是通过Yarn执行,需要先启动Hadoop
  • Spark master HA版本需要依赖ZooKeeper
  • Spark是Scala编写,执行需要依赖Scala环境,Scala版本需要和Spark使用的Scala版本一致
  • 可以通过./bin/spark-shell执行查看Scala版本:Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181)
  • 下载地址:http://archive.apache.org/dist/spark/
  • 解压:[root@master src]# tar xvzf spark-2.0.2-bin-hadoop2.6.tgz
  • 配置:
  1. [root@master conf]# cp spark-env.sh.template spark-env.sh
  2. [root@master conf]# cp slaves.template slaves
  3. [root@master conf]# vim slaves
  4. slave1
  5. slave2
  6. [root@master spark-2.0.2-bin-hadoop2.6]# mkdir tmp
  7. [root@master conf]# vim spark-env.sh
  8. export JAVA_HOME=/usr/local/src/jdk1.8.0_181
  9. export HADOOP_HOME=/usr/local/src/hadoop-2.6.5
  10. export HADOOP_CONF_DIR=/usr/local/src/hadoop-2.6.5/etc/hadoop
  11. SPARK_MASTER_IP=master
  12. SPARK_LOCAL_DIRS=/usr/local/src/spark-2.0.2-bin-hadoop2.6/tmp
  13. SPARK_DRIVER_MEMORY=512M
  14. SPARK_EXECUTOR_MEMORY=512M
  • Scala和Spark同步到slave1和slave2
  1. # 同步到slave1
  2. [root@master src]# scp -rp ./scala-2.12.6 slave1:/usr/local/src
  3. [root@master src]# scp -rp ./spark-2.0.2-bin-hadoop2.6 slave1:/usr/local/src
  4. # 同步到slave2
  5. [root@master src]# scp -rp ./scala-2.12.6 slave2:/usr/local/src
  6. [root@master src]# scp -rp ./spark-2.0.2-bin-hadoop2.6 slave2:/usr/local/src
  • 启动Spark集群
  1. # master机
  2. [root@master spark-2.0.2-bin-hadoop2.6]# ./sbin/start-all.sh
  3. starting org.apache.spark.deploy.master.Master, logging to /usr/local/src/spark-2.0.2-bin-hadoop2.6/logs/spark-wadeyu-org.apache.spark.deploy.master.Master-1-master.out
  4. slave1: starting org.apache.spark.deploy.worker.Worker, logging to /usr/local/src/spark-2.0.2-bin-hadoop2.6/logs/spark-root-org.apache.spark.deploy.worker.Worker-1-slave1.out
  5. slave2: starting org.apache.spark.deploy.worker.Worker, logging to /usr/local/src/spark-2.0.2-bin-hadoop2.6/logs/spark-root-org.apache.spark.deploy.worker.Worker-1-slave2.out
  6. # 查看jps进程,启动成功,会出现Master进程
  7. [root@master spark-2.0.2-bin-hadoop2.6]# jps
  8. 1953 SecondaryNameNode
  9. 25954 Master
  10. 26020 Jps
  11. 1803 NameNode
  12. 2123 ResourceManager
  13. # slave机
  14. # 查看jps进程,启动成功,会出现Worker进程
  15. [root@slave1 wadeyu]# jps
  16. 1314 DataNode
  17. 7509 Worker
  18. 7574 Jps
  19. 1386 NodeManager
  • 验证集群
  1. [root@master spark-2.0.2-bin-hadoop2.6]# ./bin/spark-submit --class org.apache.spark.examples.SparkPi --master spark://master:7077 examples/jars/spark-examples_2.11-2.0.2.jar 100
  2. ......
  3. 18/09/19 17:12:09 INFO scheduler.TaskSchedulerImpl: Removed TaskSet 0.0, whose tasks have all completed, from pool
  4. 18/09/19 17:12:09 INFO scheduler.DAGScheduler: ResultStage 0 (reduce at SparkPi.scala:38) finished in 17.381 s
  5. 18/09/19 17:12:09 INFO scheduler.DAGScheduler: Job 0 finished: reduce at SparkPi.scala:38, took 20.376037 s
  6. Pi is roughly 3.1418439141843915
  7. 18/09/19 17:12:12 INFO server.ServerConnector: Stopped ServerConnector@6865c751{HTTP/1.1}{0.0.0.0:4040}
  8. ......

也可以通过Web UI查看 http://master:8080/

  • 关闭Spark集群
  1. [root@master spark-2.0.2-bin-hadoop2.6]# ./sbin/stop-all.sh
  2. slave1: stopping org.apache.spark.deploy.worker.Worker
  3. slave2: stopping org.apache.spark.deploy.worker.Worker
  4. stopping org.apache.spark.deploy.master.Master
  5. [root@master spark-2.0.2-bin-hadoop2.6]# jps
  6. 1953 SecondaryNameNode
  7. 1803 NameNode
  8. 2123 ResourceManager
  9. 26061 Jps

提交Spark任务模式

  • 本地模拟集群模式,适合本地调试
  1. [root@master src]# ./bin/spark-submit --master local[2](本地模式,启动2个线程) --class com.wadeyu.spark.ScalaWordCount(需要执行的类对象) ./scala_word_count.jar(你写的jar包) /data/The_Man_of_Property.txt(数据源,默认从hdfs取)
  2. ......
  3. (pains,1)
  4. (silence,,13)
  5. (pavement;,1)
  6. (washes,1)
  7. (lower,4)
  8. (comment,1)
  9. (scornfully.,1)
  10. (father-in-law,1)
  11. (shareholder.,2)
  12. 18/09/20 09:47:43 INFO spark.SparkContext: Invoking stop() from shutdown hook
  13. 18/09/20 09:47:43 INFO server.ServerConnector: Stopped ServerConnector@6a62689d{HTTP/1.1}{0.0.0.0:4041}
  14. ......
  • 提交到Spark独立集群
    1 未使用hadoop的计算资源,使用的是Spark自己的计算资源
    2 需要启动Spark集群(因为没有跑在hadoop yarn里)
  1. [root@master spark-2.0.2-bin-hadoop2.6]# ./bin/spark-submit --class org.apache.spark.examples.SparkPi --master spark://master:7077 ./examples/jars/spark-examples_2.11-2.0.2.jar 100
  2. ......
  3. 18/09/20 10:59:48 INFO scheduler.DAGScheduler: ResultStage 0 (reduce at SparkPi.scala:38) finished in 10.501 s
  4. 18/09/20 10:59:48 INFO scheduler.TaskSchedulerImpl: Removed TaskSet 0.0, whose tasks have all completed, from pool
  5. 18/09/20 10:59:48 INFO scheduler.DAGScheduler: Job 0 finished: reduce at SparkPi.scala:38, took 12.318682 s
  6. Pi is roughly 3.1423823142382314
  7. 18/09/20 10:59:48 INFO server.ServerConnector: Stopped ServerConnector@6d1d4d7{HTTP/1.1}{0.0.0.0:4040}
  8. 18/09/20 10:59:48 INFO handler.ContextHandler: Stopped o.s.j.s.ServletContextHandler@4eed46ee{/stages/stage/kill,null,UNAVAILABLE}
  9. ......
  • 提交到hadoop集群
  1. # yarn cluster模式
  2. k-2.0.2-bin-hadoop2.6]# ./bin/spark-submit --class org.apache.spark.examples.SparkPi --master yarn-cluster ./examples/jars/spark-examples_2.11-2.0.2.jar 100
  3. ......
  4. 18/09/20 14:11:16 INFO yarn.Client: Application report for application_1537422678993_0003 (state: RUNNING)
  5. 18/09/20 14:11:17 INFO yarn.Client: Application report for application_1537422678993_0003 (state: FINISHED)
  6. 18/09/20 14:11:17 INFO yarn.Client:
  7. client token: N/A
  8. diagnostics: N/A
  9. ApplicationMaster host: 192.168.1.17
  10. ApplicationMaster RPC port: 0
  11. queue: default
  12. start time: 1537423819027
  13. final status: SUCCEEDED
  14. tracking URL: http://master:8088/proxy/application_1537422678993_0003/
  15. user: root
  16. ......
  17. # yarn client模式
  18. [root@master spark-2.0.2-bin-hadoop2.6]# ./bin/spark-submit --class org.apache.spark.examples.SparkPi --master yarn-client ./examples/jars/spark-examples_2.11-2.0.2.jar 100
  19. ......
  20. 18/09/20 14:29:12 INFO cluster.YarnScheduler: Removed TaskSet 0.0, whose tasks have all completed, from pool
  21. 18/09/20 14:29:12 INFO scheduler.DAGScheduler: ResultStage 0 (reduce at SparkPi.scala:38) finished in 21.683 s
  22. 18/09/20 14:29:12 INFO scheduler.DAGScheduler: Job 0 finished: reduce at SparkPi.scala:38, took 31.225426 s
  23. Pi is roughly 3.1412563141256316
  24. 18/09/20 14:29:12 INFO server.ServerConnector: Stopped ServerConnector@2f162cc0{HTTP/1.1}{0.0.0.0:4040}
  25. 18/09/20 14:29:13 INFO handler.ContextHandler: Stopped o.s.j.s.ServletContextHandler@1849db1a{/stages/stage/kill,null,UNAVAILABLE}
  26. ......
  27. # 两者区别
  28. yarn-cluster spark AM 从集群中选一台NodeManager跑
  29. yarn-client spark AM 跑在提交任务的机器上

提交spark任务脚本说明

  • 用法
  1. [root@master spark-2.0.2-bin-hadoop2.6]# ./bin/spark-submit --help
  2. Usage: spark-submit [options] <app jar | python file> [app arguments]
  3. Usage: spark-submit --kill [submission ID] --master [spark://...]
  4. Usage: spark-submit --status [submission ID] --master [spark://...]
  5. Usage: spark-submit run-example [options] example-class [example args]
  • 常用选项
选项含义备注
--master主节点,任务接受者spark://host:port, mesos://host:port, yarn, or local.
--deploy-modeAM节点所在位置client:本地,cluster:集群中slave中的一台,默认为client
--classjava或者scala主类名应用程序的主类
--name应用名--
--jarsJARSComma-separated list of local jars to include on the driver and executor classpaths.
--packagesPackagesComma-separated list of maven coordinates of jars to include on the driver and executor classpaths. Will search the local maven repo, then maven central and any additional remote repositories given by --repositories. The format for the coordinates should be groupId:artifactId:version.
--exclude-packages--Comma-separated list of groupId:artifactId, to exclude while resolving the dependencies provided in --packages to avoid dependency conflicts.
--repositories--Comma-separated list of additional remote repositories to search for the maven coordinates given with --packages.
--py-filesPY_FILESComma-separated list of .zip, .egg, or .py files to place on the PYTHONPATH for Python apps.
--filesFILESComma-separated list of files to be placed in the working directory of each executor.
--conf PROP=VALUE--Arbitrary Spark configuration property.
--properties-fileFILEPath to a file from which to load extra properties. If not specified, this will look for conf/spark-defaults.conf.
--driver-memoryMEMMemory for driver (e.g. 1000M, 2G) (Default: 1024M).
--driver-java-options--Extra Java options to pass to the driver.
--driver-library-path--Extra library path entries to pass to the driver.
--driver-class-path--Extra class path entries to pass to the driver. Note that jars added with --jars are automatically included in the classpath.
--executor-memoryMEMMemory per executor (e.g. 1000M, 2G) (Default: 1G).
--proxy-userNAMEUser to impersonate when submitting the application. This argument does not work with --principal / --keytab.
--help, -h--Show this help message and exit.
--verbose, -v--Print additional debug output.
--version,--Print the version of current Spark.

以下是不同模式专用的参数

  1. Spark standalone with cluster deploy mode only:
  2. --driver-cores NUM Cores for driver (Default: 1).
  3. Spark standalone or Mesos with cluster deploy mode only:
  4. --supervise If given, restarts the driver on failure.
  5. --kill SUBMISSION_ID If given, kills the driver specified.
  6. --status SUBMISSION_ID If given, requests the status of the driver specified.
  7. Spark standalone and Mesos only:
  8. --total-executor-cores NUM Total cores for all executors.
  9. Spark standalone and YARN only:
  10. --executor-cores NUM Number of cores per executor. (Default: 1 in YARN mode,
  11. or all available cores on the worker in standalone mode)
  12. YARN-only:
  13. --driver-cores NUM Number of cores used by the driver, only in cluster mode
  14. (Default: 1).
  15. --queue QUEUE_NAME The YARN queue to submit to (Default: "default").
  16. --num-executors NUM Number of executors to launch (Default: 2).
  17. If dynamic allocation is enabled, the initial number of
  18. executors will be at least NUM.
  19. --archives ARCHIVES Comma separated list of archives to be extracted into the
  20. working directory of each executor.
  21. --principal PRINCIPAL Principal to be used to login to KDC, while running on
  22. secure HDFS.
  23. --keytab KEYTAB The full path to the file that contains the keytab for the
  24. principal specified above. This keytab will be copied to
  25. the node running the Application Master via the Secure
  26. Distributed Cache, for renewing the login tickets and the
  27. delegation tokens periodically.

碰到的问题

  • 问题1
  1. ......
  2. Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction2$mcIII$sp
  3. at com.wadeyu.spark.ScalaWordCount$.main(ScalaWordCount.scala:18)
  4. at com.wadeyu.spark.ScalaWordCount.main(ScalaWordCount.scala)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  7. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  8. at java.lang.reflect.Method.invoke(Method.java:498)
  9. at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
  10. at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
  11. at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
  12. at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
  13. at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
  14. Caused by: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction2$mcIII$sp
  15. ... 11 more
  16. Caused by: java.lang.ClassNotFoundException: scala.runtime.java8.JFunction2$mcIII$sp
  17. at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  18. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  19. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  20. ... 11 more
  21. ......

原因:编写scala程序使用的scala版本和spark使用的scala版本不一致导致

解决:让两个版本一致

  • 问题2
  1. Error:scalac: missing or invalid dependency detected while loading class file 'RDD.class'.
  2. Could not access term hadoop in package org.apache,
  3. because it (or its dependencies) are missing. Check your build definition for
  4. missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
  5. A full rebuild may help if 'RDD.class' was compiled against an incompatible version of org.apache.
  6. Error:scalac: missing or invalid dependency detected while loading class file 'RDD.class'.
  7. Could not access term io in value org.apache.hadoop,
  8. because it (or its dependencies) are missing. Check your build definition for
  9. missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
  10. A full rebuild may help if 'RDD.class' was compiled against an incompatible version of org.apache.hadoop.

原因:intellij idea scala工程未导入spark jars包

解决:工程导入spark jars目录所有的jar包

  • 问题3
  1. client token: N/A diagnostics: Application application_1537417716278_0001 failed 2 times due to AM Container for appattempt_1537417716278_0001_000002 exited with exitCode: -103
  2. For more detailed output, check application tracking page:http://master:8088/proxy/application_1537417716278_0001/Then, click on links to logs of each attempt.
  3. Diagnostics: Container [pid=1606,containerID=container_1537417716278_0001_02_000001] is running beyond virtual memory limits. Current usage: 80.1 MB of 1 GB physical memory used; 2.2 GB of 2.1 GB virtual memory used. Killing container.
  4. Dump of the process-tree for container_1537417716278_0001_02_000001 :

原因:虚拟内存不够

解决:增加机器虚拟内存以及提高yarn虚拟内存和物理内存的比值,或者检查下程序是否出现了内存泄露

  1. # yarn-site.xml 增加配置虚拟内存和物理内存的比例
  2. <property>
  3. <name>yarn.nodemanager.vmem-pmem-ratio</name>
  4. <value>4.0</value>
  5. </property>
  • 问题4
  1. Failed redirect for container_1537419703454_0001_02_000001
  2. Failed while trying to construct the redirect url to the log server. Log Server url may not be configured
  3. java.lang.Exception: Unknown container. Container either has not started or has already completed or doesn't belong to this node at all.

原因:未配置好yarn history server 和未启动history server

解决;配置好history server 并且启动

参考资料

【0】八斗学院内部spark学习资料
【1】Spark2.0.2官方文档
https://spark.apache.org/docs/2.0.2/
【2】hadoop 在 yarn 上运行时报的虚拟内存错误,或者是物理内存不够错误
https://blog.csdn.net/T1DMzks/article/details/78818874
【3】centos用虚拟内存扩展内存
https://blog.csdn.net/herobacking/article/details/80371242
【4】设置/修改centos上的swap交换分区的方法
http://sujing2857.blog.163.com/blog/static/74485462009101143013852/

转载于:https://www.cnblogs.com/wadeyu/p/9681075.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/653227
推荐阅读
相关标签
  

闽ICP备14008679号