SHARE
TWEET

Untitled

a guest Jun 19th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TwoPartsPartitioner(override val numPartitions: Int) extends Partitioner {
  2.  
  3.     def getPartition(key: Any): Int = key match {
  4.         case s: String => {
  5.             if (s(0).toUpper > 'J') 1 else 0
  6.         }  
  7.     }
  8.  
  9.     override def equals(other: Any): Boolean = other.isInstanceOf[AlphabetPartitioner]
  10.     override def hashCode: Int = 0
  11. }
  12.  
  13. var x = sc.parallelize(Array(("sandeep",1),("giri",1),("abhishek",1),("sravani",1),("jude",1)), 3)
  14. x.glom().collect()
  15.  
  16. //Array(Array((sandeep,1)), Array((giri,1), (abhishek,1)), Array((sravani,1), (jude,1)))
  17. //[ [(sandeep,1)], [(giri,1), (abhishek,1)], [(sravani,1), (jude,1)] ]
  18.  
  19.  
  20. var y = x.partitionBy(new TwoPartsPartitioner(2))
  21. y.glom().collect()
  22. //Array(Array((giri,1), (abhishek,1), (jude,1)), Array((sandeep,1), (sravani,1)))
  23. //[ [(giri,1), (abhishek,1), (jude,1)], [(sandeep,1), (sravani,1)] ]
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top