【Spark】Spark-shell案例——standAlone模式下读取HDFS上存放的文件


可以先用local模式读取一下

步骤
一、先将做测试的数据上传到HDFS
cd /export/servers/sparkdatas
hdfs dfs -mkdir -p /sparkwordcount
hdfs dfs -put wordcount.txt  /sparkwordcount
二、开发scala代码
sc.textFile("hdfs://node01:8020/sparkwordcount/wordcount.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_ + _).collect

在这里插入图片描述
如果不需要查看结果,而是需要将结果存储为文本文件,则将.collect换成.saveAsTestFile(要存放的Path)即可


standAlone模式查看HDFS上的文件

步骤
一、退出local模式,重新进入Spark-shell
bin/spark-shell --master spark://node01:7077 
 --executor-memory 1g 
 --total-executor-cores 2
二、开发scala代码
sc.textFile("hdfs://node01:8020/sparkwordcount/wordcount.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_ + _).collect

在这里插入图片描述

原文地址:https://www.cnblogs.com/zzzsw0412/p/12772411.html