您当前的位置: 首页 >  ar

180 Spark程序执行

杨林伟 发布时间:2019-08-14 15:27:04 ,浏览量:1

1.执行spark程序
/usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://node1.itcast.cn:7077 \
--executor-memory 1G \
--total-executor-cores 2 \
/usr/local/spark-1.5.2-bin-hadoop2.6/lib/spark-examples-1.5.2-hadoop2.6.0.jar \
100

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

2.Spark Shell

spark-shell 是Spark自带的交互式Shell程序,方便用户进行交互式编程,用户可以在该命令行下用scala编写spark程序。

2.1启动spark shell
/usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-shell \
--master spark://node1.itcast.cn:7077 \
--executor-memory 2g \
--total-executor-cores 2

参数说明:

--master spark://node1.itcast.cn:7077 指定Master的地址
--executor-memory 2g 指定每个worker可用内存为2G
--total-executor-cores 2 指定整个集群使用的cup核数为2个

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

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

2.在spark shell中编写WordCount程序

1.首先启动hdfs

2.向hdfs上传一个文件到hdfs://node1.itcast.cn:9000/words.txt

3.在spark shell中用scala语言编写spark程序

sc.textFile("hdfs://node1.itcast.cn:9000/words.txt").flatMap(_.split(" "))
.map((_,1)).reduceByKey(_+_).saveAsTextFile("hdfs://node1.itcast.cn:9000/out")

4.使用hdfs命令查看结果

hdfs dfs -ls hdfs://node1.itcast.cn:9000/out/p*

说明: sc =》是SparkContext对象,该对象时提交spark程序的入口 textFile(hdfs://node1.itcast.cn:9000/words.txt) =》是hdfs中读取数据 flatMap(_.split(" ")) =》先map在压平 map((_,1)) =》将单词和1构成元组 reduceByKey(+) =》按照key进行reduce,并将value累加 saveAsTextFile(“hdfs://node1.itcast.cn:9000/out”) =》将结果写入到hdfs中

关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 1浏览

    0关注

    3183博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0512s