Spark-Shell运行任务

文章目录

    • 1.Spark-Shell 交互式编程
      • 1.1 启动命令
      • 1.2 Spark-Shell中运行wordcount
    • 2. spark-submit提交Job

   开始本篇博客之前,请先准备好环境,参见 【上一篇 Spark集群部署】

1.Spark-Shell 交互式编程

1.1 启动命令

bin/spark-shell \
--master spark://l0:7077 \
--executor-memory 1g \
--total-executor-cores 2
参数 解释
–master 集群的master URL (如 spark://192.168.191.130:7077)
–executor-memory 1G 指定每个executor可用内存为1G
–total-executor-cores 2 指定每个executor使用的cup核数为2个
–jars 添加任务所需的其他依赖

   如果启动spark shell时没有指定master地址,但是也可以正常启动spark shell和执行spark shell中的程序,其实是启动了spark的local模式,该模式仅在本机启动一个进程,没有与集群建立联系。

   Spark Shell中已经默认将SparkContext类初始化为对象sc。用户代码如果需要用到,则直接应用sc即可。

1.2 Spark-Shell中运行wordcount

scala> sc.textFile("hdfs://l0:8020/words.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).saveAsTextFile("hdfs://l0:8020/output")

   实现的功能是将HDFS中words.txt中的内容,按照每个单词计数,结果存放于HDFS的output目录中。

2. spark-submit提交Job

bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://l0:7077 \
--executor-memory 1G \
--total-executor-cores 2 \
/home/hadoopadmin/spark-2.1.1-bin-hadoop2.7/examples/jars/spark-examples_2.11-2.1.1.jar \
100
参数 解释
–class 应用的启动类
spark-examples_2.11-2.1.1.jar 程序所在jar包
100 程序所需参数

该算法是利用蒙特•卡罗算法求PI

Spark-Shell运行任务_第1张图片

你可能感兴趣的:(大数据,spark)