1

(String, Int)我有一个包含元组的dstream

当我尝试combineByKey时,它说我指定参数:Partitioner

my_dstream.combineByKey(
      (v) => (v,1),
      (acc:(Int, Int), v) => (acc._1 + v, acc._2 + 1),
      (acc1:(Int, Int), acc2:(Int, Int)) => (acc1._1 + acc2._1, acc1._2 + acc2._2)
    )

但是,当我在 rdd 上使用它时,它可以正常工作:

 my_dstream.foreachRDD( rdd =>
      rdd.combineByKey(
        (v) => (v,1),
        (acc:(Int, Int), v) => (acc._1 + v, acc._2 + 1),
        (acc1:(Int, Int), acc2:(Int, Int)) => (acc1._1 + acc2._1, acc1._2 + acc2._2)
      ))

我在哪里可以得到这个分区器?

4

1 回答 1

1

我在哪里可以得到这个分区器?

您可以自己创建它。Spark 开箱即用,带有两个分区器:HashPartitionerRangePartitioner. 默认为前者。您可以通过它的构造函数进行实例化,您需要传递所需分区的数量:

val numOfPartitions = // specify the amount you want
val hashPartitioner = new HashPartitioner(numOfPartitions)

my_dstream.combineByKey(
  (v) => (v,1),
  (acc:(Int, Int), v) => (acc._1 + v, acc._2 + 1),
  (acc1:(Int, Int), acc2:(Int, Int)) => (acc1._1 + acc2._1, acc1._2 + acc2._2), 
                                        hashPartitioner
) 
于 2016-04-01T15:53:31.630 回答