Scala 如何利用Spark中的map函数获取第一个和第三个单词
我试图使用Scala 如何利用Spark中的map函数获取第一个和第三个单词,scala,apache-spark,Scala,Apache Spark,我试图使用map函数检索每行的第一个和第三个索引值b,作为arrray 以下内容将生成第一个单词的数组: File.map(l => l.split(" ")(0)).collect() 我尝试了以下方法,但没有成功: File.map(l => l.split(" ")(0)(2)).collect() File.map(l => l.split(" ")(0,2)).collect() File.map(l => l.split(" ")(0)+(2)).col
map
函数检索每行的第一个和第三个索引值b,作为arrray
以下内容将生成第一个单词的数组:
File.map(l => l.split(" ")(0)).collect()
我尝试了以下方法,但没有成功:
File.map(l => l.split(" ")(0)(2)).collect()
File.map(l => l.split(" ")(0,2)).collect()
File.map(l => l.split(" ")(0)+(2)).collect()
这就是您可以做的,您需要从map函数返回元组,如下所示
File.map(l => (l.split(" ")(0), l.split(" ")(2)))
.collect()
希望这有帮助 这就是您可以做的,您需要从map函数返回元组,如下所示
File.map(l => (l.split(" ")(0), l.split(" ")(2)))
.collect()
希望这有帮助 您可以将其作为模式匹配:
File.
map {
_.split(" ").take(3) match {
case Array(firstWord, _, thirdWord) => (firstWord, thirdWord)
// Consider handling cases where there are fewer than three words
}
}.
collect()
您可以将其作为模式匹配:
File.
map {
_.split(" ").take(3) match {
case Array(firstWord, _, thirdWord) => (firstWord, thirdWord)
// Consider handling cases where there are fewer than three words
}
}.
collect()
如果您希望使用
RDD[Array[String]]
,则可以执行以下操作
File.map(line => line.split(" ")).map(words => Array(words(0), words(2))).collect()
如果您希望使用
RDD[Array[String]]
,则可以执行以下操作
File.map(line => line.split(" ")).map(words => Array(words(0), words(2))).collect()
非常感谢你!非常感谢你!