Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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/4/string/5.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
Java 有没有办法按行拆分RDD?_Java_String_Apache Spark_Split_Rdd - Fatal编程技术网

Java 有没有办法按行拆分RDD?

Java 有没有办法按行拆分RDD?,java,string,apache-spark,split,rdd,Java,String,Apache Spark,Split,Rdd,我在JavaRDD中有一堆20000行的数据。现在我想保存几个大小完全相同的文件,比如每个文件70行 我用下面的代码进行了尝试,但由于它不完全可分割,一些数据集由69、70或71行组成。斗争是我需要所有的大小相同的唱片,除了最后一张可以少一点的唱片 谢谢你的帮助!!!提前谢谢各位 myString.repartition286.saveAsTextFileoutputPath 您可以使用filterByRange执行类似伪代码的操作: for i = 0; i < javaRDD.size

我在JavaRDD中有一堆20000行的数据。现在我想保存几个大小完全相同的文件,比如每个文件70行

我用下面的代码进行了尝试,但由于它不完全可分割,一些数据集由69、70或71行组成。斗争是我需要所有的大小相同的唱片,除了最后一张可以少一点的唱片

谢谢你的帮助!!!提前谢谢各位


myString.repartition286.saveAsTextFileoutputPath

您可以使用filterByRange执行类似伪代码的操作:

for i = 0; i < javaRDD.size ; i+= 70
    val tempRDD = javaRDD.filterByRange(i,i+70).repartition(1)
    tempRDD.saveAsTextFile(outputPath + i.toString());

不幸的是,Scala给出了一个答案,但它是有效的

首先定义一个自定义分区器:

class IndexPartitioner[V](n_per_part: Int, rdd: org.apache.spark.rdd.RDD[_ <: Product2[Long, V]], do_cache: Boolean = true) extends org.apache.spark.Partitioner {

    val max = {
        if (do_cache) rdd.cache()
        rdd.map(_._1).max
    }

    override def numPartitions: Int = math.ceil(max.toDouble/n_per_part).toInt
    override def getPartition(key: Any): Int = key match {
        case k:Long => (k/n_per_part).toInt
        case _ => (key.hashCode/n_per_part).toInt
    }
}
创建并应用分区器:

val partitioner = new IndexPartitioner(70, rdd_idx)
val rdd_part = rdd_idx.partitionBy(partitioner).values
检查分区大小:

rdd_part
  .mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
  .toDF("partition_number","number_of_records")
  .show

/**
+----------------+-----------------+
|               0|               70|
|               1|               70|
|               2|               70|
|               3|               70|
|               4|               70|
|               5|               70|
|               6|               70|
|               7|               70|
|               8|               70|
|               9|               70|
|              10|               70|
|              11|               70|
|              12|               70|
|              13|               70|
|              14|               20|
+----------------+-----------------+
*/
每个分区一个文件:

import sqlContext.implicits._
rdd_part.toDF.write.format("com.databricks.spark.csv").save("/tmp/idx_part_test/")
+成功的第一步

XXX$ ls /tmp/idx_part_test/ | wc -l
16

Spark的工作原理并非如此。我可以问一下,为什么在每个文件中有完全相同的行数很重要吗?rdd.coalescerdd.count/70.toInt.saveAsTextFiledirectory\u其中u请尝试此合并saveAsTextFile@rohitprakash那对我不起作用。我的16353行,其中仅分为两个文件,分别为8242和8111行。trainDataFeatures.CoalescintTrainDataFeatures.count/70.saveAsTextFileoutputPathTrainFeatures@GlennieHellesSindholt,因为我想把我的数据放入SequenceRecordReader,以适应机器学习模型。我正在写我自己的答案,但这更简洁:JavaRDD没有filterByRange。我做错了什么?
XXX$ ls /tmp/idx_part_test/ | wc -l
16