【问题标题】:Transformations and Actions in Apache SparkApache Spark 中的转换和操作
【发布时间】:2015-06-09 15:17:05
【问题描述】:

我有 scala 代码,它使用通配符从 HDFS 获取多个输入文件,每个文件进入一个函数,在该函数中单独对每个文件进行处理。

import de.l3s.boilerpipe.extractors.KeepEverythingExtractor

    val data = sc.wholeTextFiles("hdfs://localhost:port/akshat/folder/*/*")

    val files = data.map { case (filename, content) => filename}


    def doSomething(file: String): (String,String) = { 


     // logic of processing a single file comes here

     val logData = sc.textFile(file);
     val c = logData.toLocalIterator.mkString
     val d = KeepEverythingExtractor.INSTANCE.getText(c)
     val e = sc.parallelize(d.split("\n"))
     val recipeName = e.take(10).last
     val prepTime = e.take(18).last

     (recipeName,prepTime)
    }

    //How transformation and action applied here?

我被困在如何应用进一步的转换和操作,以便我的所有输入文件都根据函数 doSomething 进行映射,并且每个输入文件的所有输出都使用 saveAsTextFile 存储在单个文件中。

【问题讨论】:

    标签: scala hadoop apache-spark hdfs


    【解决方案1】:

    所以如果我的理解是正确的,你有一个 RDD 对并且你希望对其进行更多的转换,然后将每个键的输出保存到一个唯一的文件中。对其进行更多转换相对容易,mapValue 将允许您仅对值编写转换,任何其他转换都将适用于 Pairs 的 RDD。

    但是,将输出保存到每个键的唯一文件中有点棘手。一种选择是尝试找到一种 hadoopoutput 格式,它可以满足您的需求,然后使用saveAsHadoopFile,另一种选择是使用foreach,然后只需编写代码以根据需要输出每个键/值对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多