JerryLead / SparkInternals

Notes talking about the design and implementation of Apache Spark

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

关于PairRDD的key类型问题

zhaoyao opened this issue · comments

在Job 逻辑执行图中提到的

对 RDD 进行一系列的 transformation() 操作,每一个 transformation() 会产生一个或多个包含不同类型 T 的 RDD[T]。T 可以是 Scala 里面的基本类型或数据结构,不限于 (K, V)。但如果是 (K, V),K 不能是 Array 等复杂类型(因为难以在复杂类型上定义 partition 函数)。

我看1.1.0的HashPartitioner中的注释有提到,

Java arrays have hashCodes that are based on the arrays' identities rather than their contents,
so attempting to partition an RDD[Array[_]] or RDD[(Array[_], _)] using a HashPartitioner will
produce an unexpected or incorrect result.

Array仅是个特例,并不是所有复杂类型都不能使用吧

对,我这里可能没有说清楚,其实我的意思是:如果很难或者不能在上某个类型上定义正确的partition(),那么这个类型不能当作Key使用。Hadoop里面也是一样,一些自定的class可以当作key使用,但如果无法在上面定义partition(),那么就不能当作key。

那段注释是java数组的hashcode是基于数组的标示符而不是基于数组内容(元素),使用array作为分区键会产生不可预料不正确的结果。其实不光是数组,map作为分区键也不合适。这个东西可以引申一下,如果作为分区键的class很难产生value一致排序的分区,则不适合做分区键

@lqian 谢谢建议,下次修改时加上