【问题标题】:Spark: How to save the output (saveAsTextFile) to files with equal size?Spark:如何将输出(saveAsTextFile)保存到大小相等的文件中?
【发布时间】:2015-09-01 21:36:52
【问题描述】:

我有以下代码,尝试将 RDD 输出到 1000 个具有文件大小相等的文件。但是,我仍然只有 70 个输出文件,而且文件大小差别很大(范围从 50M 到 2G)。为了使输出文件大小相等,我需要做任何额外的步骤吗?谢谢!

val myRDD = input.flatMap { t => ??? }
                 .reduceByKey { (t1, t2) => ??? ; t3 }
                 .sortBy(-_._2.size)
                 .repartition(1000)
                 .map(t => (t._1 + "_" + t._2.size, t._2.toString))

myRDD.saveAsTextFile("myOutput", classOf[GzipCodec])

【问题讨论】:

    标签: scala apache-spark rdd


    【解决方案1】:

    您可以使用RangePartitioner 创建大小相等的分区,然后将其保存为文本文件。

    示例取自there:

    import org.apache.spark.RangePartitioner;
    var file=sc.textFile("<my local path>")    
    var partitionedFile=file.map(x=>(x,1))
    var data= partitionedFile.partitionBy(new RangePartitioner(3, partitionedFile))
    data.glom().collect()(0).length
    data.glom().collect()(1).length
    data.glom().collect()(2).length
    

    在您的情况下,运行saveAsTextFile() 应该足够了。

    【讨论】:

    • 你介意分享一个如何使用它的例子吗?谢谢!
    • 我现在时间不多,所以我找到了现成的解决方案,请验证是否可以。
    【解决方案2】:

    这很简单,你需要做的就是使用 repartition(1000) 并且您的文件大小将相等且恰好为 1000

    您的代码已修改:

    val myRDD = input.flatMap { t => ??? }
                     .reduceByKey { (t1, t2) => ??? ; t3 }
                     .sortBy(-_._2.size)
                     .repartition(1000)
                     .map(t => (t._1 + "_" + t._2.size, t._2.toString)).repartition(1000)
    
    myRDD.saveAsTextFile("myOutput", classOf[GzipCodec])
    

    【讨论】:

      【解决方案3】:
      the following answer will solve your purpose
      
      val myRDD = input.flatMap { t => ??? }
                       .reduceByKey { (t1, t2) => ??? ; t3 }
                       .sortBy(-_._2.size)
                       .repartition(1000)
                       .map(t => (t._1 + "_" + t._2.size, t._2.toString))
      
      myRDD.repartition(1000).saveAsTextFile("myOutput", classOf[GzipCodec])
      
      One thing to note that original rdd will have it's existance even after this because it is immutable 
      
      or even you can use coalesce(1000) if you want to set max partitions to set  
      

      【讨论】:

        猜你喜欢
        • 2018-09-24
        • 1970-01-01
        • 2015-06-10
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        相关资源
        最近更新 更多