spark任务提交到yarn上命令总结

spark任务提交到yarn上命令总结

1. 使用spark-submit提交任务

  • 集群模式执行 SparkPi 任务,指定资源使用,指定eventLog目录
spark-submit  --class org.apache.spark.examples.SparkPi 
    --master yarn 
    --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 
    --deploy-mode cluster 
    --driver-memory 4g 
    --executor-memory 2g 
    --executor-cores 1 
    --queue thequeue 
    $SPARK_HOME/examples/jars/spark-examples*.jar 
    10
  • 不指定资源,使用yarn的默认资源分配。
spark-submit  --class org.apache.spark.examples.SparkPi 
    --master yarn 
    --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 
    --deploy-mode cluster 
    $SPARK_HOME/examples/jars/spark-examples*.jar 10
  • 动态的加载spark配置
./bin/spark-submit --name "My app" --master local[4] --conf spark.eventLog.enabled=false
  --conf "spark.executor.extraJavaOptions=-XX:+PrintGCDetails -XX:+PrintGCTimeStamps" myApp.jar
  • 客户端模式执行 SparkPi 任务:spark-submit
spark-submit  --class org.apache.spark.examples.SparkPi 
    --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 
    --master yarn 
    --deploy-mode client 
    --driver-memory 4g 
    --executor-memory 2g 
    --executor-cores 1 
    $SPARK_HOME/examples/jars/spark-examples*.jar 
    10

2. 使用spark-shell提交任务到yarn上

  • 使用spark-shell测试wordcont:使用-jars加载任务运行依赖的jar包,多个jar包以逗号分隔。
    spark-shell --master yarn --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 --jars /home/fxzhao/hadoop-lzo-0.4.20-SNAPSHOT.jar

在随后的终端框中如下scala脚本:统计hdfs://dbmtimehadoop/tmp/fuxin.zhao/wordcounttest 中各个单词的数量。
在scala终端中输入 “:paste”可以输入多条scala语句。按CRTL+d 结束。

val textFile = sc.textFile("hdfs://dbmtimehadoop/tmp/fx.zhao/wordcounttest")
val counts = textFile.flatMap(line => line.split(" "))
                 .map(word => (word, 1))
                 .reduceByKey(_ + _)
counts.saveAsTextFile("hdfs://dbmtimehadoop/tmp/fx.zhao/wordcounttest_res")

##########将统计结果按照key排序。
val textFile = sc.textFile("hdfs://dbmtimehadoop/tmp/fx.zhao/wordcounttest")
val counts = textFile.flatMap(line => line.split(" "))
                 .map(word => (word, 1))
                 .reduceByKey(_ + _)
                 .sortByKey()
counts.saveAsTextFile("hdfs://dbmtimehadoop/tmp/fx.zhao/wordcounttest_res")

  • Spark-shell 启动时添加添加依赖jar包:
    spark-shell --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 --jars $HADOOP_HOME/share/hadoop/common/lib/hadoop-lzo-0.4.20-SNAPSHOT.jar

3.spark-sql提交任务到spark的两种方式:

  • 本地模式:
    $ spark-sql --master local

  • yarn模式
    $ spark-sql --master yarn
    //启动spark-sql时指定eventLog的位置等其他配置(可以通过--conf 来配置修改默认的多个参数)。
    $ spark-sql --master yarn --conf spark.eventLog.dir=hdfs://dbmtimehadoop/tmp/spark2 --conf spark.sql.hive.metastore.version=2.1.0

原文地址:https://www.cnblogs.com/honeybee/p/6434656.html