【问题标题】:NLineInputFormat not working in SparkNLineInputFormat 在 Spark 中不起作用
【发布时间】:2016-10-30 01:07:55
【问题描述】:

我想要的基本上是让每个数据元素由 10 行组成。但是,使用以下代码,每个元素仍然是一行。我在这里犯了什么错误?

val conf = new SparkConf().setAppName("MyApp")
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
conf.registerKryoClasses(Array[Class[_]](classOf[NLineInputFormat], classOf[LongWritable], 
 classOf[Text]))
val sc = new SparkContext(conf)

val c = new Configuration(sc.hadoopConfiguration)
c.set("lineinputformat.linespermap", 10);
val data = sc.newAPIHadoopFile(fname, classOf[NLineInputFormat], classOf[LongWritable], 
 classOf[Text], c)

【问题讨论】:

    标签: scala hadoop apache-spark


    【解决方案1】:

    NLineInputFormat 设计为 doesn't perform operation you expect it to:

    NLineInputFormat 将 N 行输入拆分为一个拆分。 (...) 分割输入文件,默认情况下,一行作为值馈送到一个映射任务。

    如您所见,它修改了拆分(Spark 命名法中的分区)的计算方式,而不是记录的确定方式。

    如果描述不清楚,我们可以用下面的例子来说明:

    def nline(n: Int, path: String) = {
      val sc = SparkContext.getOrCreate
      val conf = new Configuration(sc.hadoopConfiguration)
      conf.setInt("mapreduce.input.lineinputformat.linespermap", n);
    
      sc.newAPIHadoopFile(path,
        classOf[NLineInputFormat], classOf[LongWritable], classOf[Text], conf
      )
    }
    
    require(nline(1, "README.md").glom.map(_.size).first == 1)
    require(nline(2, "README.md").glom.map(_.size).first == 2)
    require(nline(3, "README.md").glom.map(_.size).first == 3)
    

    如上图所示,每个分区(可能不包括最后一个分区)正好包含 n 行。

    虽然您可以尝试对其进行改造以适应您的情况,但不建议将其用于较小的 linespermap 参数值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2015-05-26
      • 2017-05-21
      • 2021-01-03
      相关资源
      最近更新 更多