Вот пример - с использованием широковещательной переменной.stopWords на самом деле включает в себя слова.
val dfsFilename = "/FileStore/tables/7dxa9btd1477497663691/Text_File_01-880f5.txt"
val readFileRDD = spark.sparkContext.textFile(dfsFilename)
// res4: Array[String] = Array(The the is Is a A to To OK ok I) //stopWords
val stopWordsInput = spark.sparkContext.textFile("/FileStore/tables/filter_words.txt")
val stopWords = stopWordsInput.flatMap(x => x.split(" ")).map(_.trim).collect.toSet
val broadcasted = sc.broadcast(stopWords)
val wcounts1 = readFileRDD.map(x => (x.replaceAll("[^A-Za-z0-9]", " ")
.trim.toLowerCase))
.flatMap(line=>line.split(" "))
.filter(broadcasted.value.contains(_))
.map(word=>(word, 1))
.reduceByKey(_ + _)
wcounts1.collect
возвращает:
res2: Array[(String, Int)] = Array((The,1), (I,3), (to,1), (the,1))
Вы можете украсить трансляцию на stopWords - это то, что я и сделал.
Я виделВы вводите XML и заменяете все.Вы можете возиться с этим по своему вкусу.Я также добавил пункт, чтобы поместить все это в нижний регистр.