1.功能簡介
將txt文件中的數據進行數據去重並顯示在輸出框中
2.txt數據格式
3.源代碼
import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object spark01_wordcount { def main(args: Array[String]): Unit = { val sparConf = new SparkConf().setMaster("local").setAppName("WordCount") val sc = new SparkContext(sparConf) val lines:RDD[String] = sc.textFile("filepath") //filepath為txt文件路徑 val words:RDD[String]=lines.flatMap(_.split(" ")) //此處為以某個分隔符對每行數據進行切分,我用的是空格也可更改為其他符號如逗號 val afterdeal=words.distinct().collect().mkString(",") //去重操作並將去重結果以逗號為分隔符組成一個字符串 val realwords:Array[String] = afterdeal.split(",") realwords.foreach(println) sc.stop() } }
4.結果截圖