🌀 技术人生
凡事有交代,件件有着落,事事有回音
在Spark Shell中编写WordCount程序

Spark Shell是一个交互式的命令行,里面可以写Spark程序(Scala语言),也是一个客户端,用于提交Spark程序

1.启动Spark Shell bin/spark-shell

上边是没有指定Master地址的启动方式,启动后用的是spark的local模式运行的,是模拟了spark集群运行的过程 bin/spark-shell –master spark://cdh0:7077,cdh1:7077

上边是指定了Master地址的启动方式,会将任务提交到集群,这时候使用jps查看,可以看到机器上的SparkSubmit和CoarseGrainedExecutorBackend进程都已经存在了,SparkSubmit会连接Master,并申请计算资源,然后Master进行资源调度(让Worker来启动Executor)

2.向hdfs中上传一个用来测试的数据文件

例如: test.txt hdfs yarn hadoop hdfs yarn mapreduce hadoop yarn hdfs mapreduce

然后上传到hdfs中

3.在Spark Shell中编写WordCount程序

在Spark Shell中使用Scala编写Spark程序 sc.textFile(“hdfs://cdh0:8020/usr/ys/input/test.txt”).flatMap(.split(" “)).map((,1)).reduceByKey(+).saveAsTextFile(“hdfs://cdh0:8020/usr/output”)

参数说明:

sc是SparkContext对象,该对象是提交spark程序的入口

textFile(“hdfs://cdh0:8020/usr/ys/input/test.txt”)是向hdfs中读取数据

flatMap(_.split(” “))是先map后进行扁平化操作

map((_,1))是将单词和1构成元组

reduceByKey(+)是按照key进行reduce,并将value累加

saveAsTextFile(“hdfs://cdh0:8020/usr/output2”)是保存到hdfs的目录中

4.在hdfs中查看结果

bin/hdfs dfs -cat /usr/output//*


最后修改于 2018-10-11

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。