Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何利用Spark中的map函数获取第一个和第三个单词_Scala_Apache Spark - Fatal编程技术网

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()

非常感谢你!非常感谢你!