1. object Spark01_RDD_Part {
    2. def main(args: Array[String]): Unit = {
    3. val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("r1")
    4. val sc: SparkContext = new SparkContext(sparkConf)
    5. val rdd: RDD[(String, String)] = sc.makeRDD(List(
    6. ("nba","......."),
    7. ("cuba","......."),
    8. ("cba","......."),
    9. ("nba","......."),
    10. ),3)
    11. val partRDD: RDD[(String, String)] = rdd.partitionBy(new myPartitioner)
    12. partRDD.saveAsTextFile("output")
    13. sc.stop()
    14. }
    15. }
    16. class myPartitioner extends Partitioner{
    17. override def numPartitions: Int = 3
    18. override def getPartition(key: Any): Int = {
    19. key match {
    20. case "nba" => 0
    21. case "cba" => 1
    22. case _ => 2
    23. }
    24. }
    25. }