Apache Spark:如何在 scala-spark 中将 RDD[(String, String)] 转换为 Array[String]?
Apache Spark: How do I convert an RDD[(String, String)] into a Array[String] in scala-spark?
我的代码 运行 看起来像这样
val termCounts: Array[(String, Long)] = tokenized.flatMap(_.map(_ -> 1L)).reduceByKey(_ + _).collect().sortBy(-_._2)
// vocabArray: Chosen vocab (removing common terms)
val numStopwords = 20
val stopWord = sc.wholeTextFiles(".../stopword.txt")
val vocabArray1: Array[String] =
termCounts.takeRight(termCounts.size - numStopwords).map(_._1)
val vocabArray = vocabArray1 diff stopWord
看,我想使用 diff 函数,但它只适用于相同的类型。
当您使用 sc.wholeTextFiles("/root/folder/to/textfiles/") 时,它将将该文件夹中的每个零件文件读取到一个字符串中。
因此,如果您的设置是
/root/folder/to/textfiles/
.../part1.txt
.../part2.txt
.../part3.txt
part1.txt、part2.txt、part3.txt 都作为单个记录读取。所以你的 RDD[(String, String)] 将是一对文件名的路径和整个文件作为一个字符串。
像这样。
("/root/folder/to/text/files/part1.txt", "actual contents of part1.txt as a String"),
("/root/folder/to/text/files/part2.txt", "actual contents of part2.txt as a String")
...
您可能希望在映射之前对每个文件的实际内容进行标记。
stopWord.flatMap(tokenize(_._2)).collect()
我的代码 运行 看起来像这样
val termCounts: Array[(String, Long)] = tokenized.flatMap(_.map(_ -> 1L)).reduceByKey(_ + _).collect().sortBy(-_._2)
// vocabArray: Chosen vocab (removing common terms)
val numStopwords = 20
val stopWord = sc.wholeTextFiles(".../stopword.txt")
val vocabArray1: Array[String] =
termCounts.takeRight(termCounts.size - numStopwords).map(_._1)
val vocabArray = vocabArray1 diff stopWord
看,我想使用 diff 函数,但它只适用于相同的类型。
当您使用 sc.wholeTextFiles("/root/folder/to/textfiles/") 时,它将将该文件夹中的每个零件文件读取到一个字符串中。
因此,如果您的设置是
/root/folder/to/textfiles/
.../part1.txt
.../part2.txt
.../part3.txt
part1.txt、part2.txt、part3.txt 都作为单个记录读取。所以你的 RDD[(String, String)] 将是一对文件名的路径和整个文件作为一个字符串。
像这样。
("/root/folder/to/text/files/part1.txt", "actual contents of part1.txt as a String"),
("/root/folder/to/text/files/part2.txt", "actual contents of part2.txt as a String")
...
您可能希望在映射之前对每个文件的实际内容进行标记。
stopWord.flatMap(tokenize(_._2)).collect()