如何使用合并更改分区数?

How to change the number of partitions using coalesce?

我将 spark 与 java 和 Cassandra 数据库一起使用,在我的程序中我使用 mapPartitions 来请求 cassadra。但我注意到我的 mapPartitions 仅在一个 spark 节点中执行。为了查看我的 RDD 中的分区数,我使用了:

System.out.println(MyRDD.partitions().size());

并且显示1个分区。 我发现我可以使用此编辑分区数:

JavaRDD MyRDD2= MyRDD.coalesce(8, false);

但是它不起作用,我的分区号仍然是 1。

能否帮我更改分区数?

您必须将 shuffle 设置为 true 才能合并到更多分区:

JavaRDD MyRDD2= MyRDD.coalesce(8, true);
As per coalesce() function of RDD, we can reduce the number of partition. For increasing partition number repartition() function should use.

var textRDD = scontext.textFile("file:///home/rajeev/Test.scala", 3);

    print("================== "+textRDD.getNumPartitions);

   var newRDD = textRDD.coalesce(6, false);
print("==================:: "+newRDD.getNumPartitions+"\n");

   var newRDD1 = textRDD.coalesce(6, true);
print("==================:: "+newRDD1.getNumPartitions+"\n");

Output is 3 and 3 and 6 respective print statement.

Ideally it should not be happen. Please could you explain. Is it because we are shuffling data.