Spark的HashPartitioner方式的Python实现
2016-05-26 00:00
651 查看
摘要: Spark的HashPartitioner方式的Python实现,可以通过Python来获取某个键在Spark中的分区索引
spark中的默认分区方式是org.apache.spark.HashPartitioner,具体代码如下所示:
如果想要在Python中获取一个key的分区,只需要实现hashCode,然后取模。
hashCode的实现方式如下:
验证
Scala实现
Python实现
spark中的默认分区方式是org.apache.spark.HashPartitioner,具体代码如下所示:
[code=language-scala]class HashPartitioner(partitions: Int) extends Partitioner { require(partitions >= 0, s"Number of partitions ($partitions) cannot be negative.") def numPartitions: Int = partitions def getPartition(key: Any): Int = key match { case null => 0 case _ => Utils.nonNegativeMod(key.hashCode, numPartitions) } override def equals(other: Any): Boolean = other match { case h: HashPartitioner => h.numPartitions == numPartitions case _ => false } override def hashCode: Int = numPartitions }
如果想要在Python中获取一个key的分区,只需要实现hashCode,然后取模。
hashCode的实现方式如下:
[code=language-python]def java_string_hashcode(s): h = 0 for c in s: h = (31 * h + ord(c)) & 0xFFFFFFFF return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
验证
Scala实现
Python实现
相关文章推荐
- Python学习笔记(一)
- Python中各种集合 list tuple set dict
- Python 中一些需要查询的东西
- Python基础(六) 基础文件操作
- Python heapq priority queue
- Python中的引用的使用注意
- 6款好用的Python IDE
- Python点滴小知识
- Python:完整版 wc
- python爬虫:案例四:新浪微指数(未完全解决
- python函数知识
- Python菜鸟之路:Python基础——函数
- python web框架cherrypy小demo
- Meet Python: little notes 3 - function
- python 背单词
- Robot Framework 教程 - 一个完整的例子
- 1. python入门杂记
- Python学习 常识+基础基础
- 关于python 2.7要求输出汉字问题
- 关于python 2.7要求输出汉字问题