如何在PySpark中获得不同的RDD RDD?

时间:2016-02-19 16:19:44

标签: python apache-spark pyspark rdd

我有一个字典的RDD,我想得到一个只有不同元素的RDD。但是,当我试着打电话时

rdd.distinct()

PySpark给了我以下错误

TypeError: unhashable type: 'dict'

    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:166)
    at org.apache.spark.api.python.PythonRunner$$anon$1.<init>(PythonRDD.scala:207)
    at org.apache.spark.api.python.PythonRunner.compute(PythonRDD.scala:125)
    at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:70)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:270)
    at org.apache.spark.api.python.PairwiseRDD.compute(PythonRDD.scala:342)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:270)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:73)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
    at org.apache.spark.scheduler.Task.run(Task.scala:89)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
16/02/19 16:55:56 WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/worker.py", line 111, in main
    process()
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/worker.py", line 106, in process
    serializer.dump_stream(func(split_index, iterator), outfile)
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/rdd.py", line 2346, in pipeline_func
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/rdd.py", line 2346, in pipeline_func
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/rdd.py", line 317, in func
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/rdd.py", line 1776, in combineLocally
  File "/usr/local/Cellar/apache-spark/1.6.0/libexec/python/lib/pyspark.zip/pyspark/shuffle.py", line 238, in mergeValues
    d[k] = comb(d[k], v) if k in d else creator(v)
TypeError: unhashable type: 'dict'

我在dict里面有一个键可以作为独特的元素使用,但是文档没有提供任何关于如何解决这个问题的线索。

编辑:内容由字符串,字符串数组和数字字典组成

编辑2:字典示例...我希望将相同“data_fingerprint”键的dicts视为相等:

{"id":"4eece341","data_fingerprint":"1707db7bddf011ad884d132bf80baf3c"}

由于

2 个答案:

答案 0 :(得分:2)

正如@ zero323在他的评论中指出的那样,你必须决定如何比较字典,因为它们不是可以删除的。一种方法是对密钥进行排序(因为它们不是以任何特定的顺序排列),例如通过lexycographic顺序。然后创建一个形式的字符串:

def dict_to_string(dict):
    ...
    return 'key1|value1|key2|value2...|keyn|valuen'

如果您有嵌套的不可用对象,则必须以递归方式执行此操作。

现在你可以将RDD转换为与字符串配对作为键(或其某种哈希值)

pairs = dictRDD.map(lambda d: (dict_to_string(d), d))

要获得你想要的东西,你只需要按键减少

distinctDicts = pairs.reduceByKey(lambda val1, val2: val1).values()

答案 1 :(得分:1)

由于您的数据提供了唯一的密钥,因此您可以执行以下操作:

(rdd
    .keyBy(lambda d: d.get("data_fingerprint"))
    .reduceByKey(lambda x, y: x)
    .values())

Python字典至少存在两个问题,这些问题使它们成为散列的不良候选者:

  • 可变性 - 这会使任何哈希变得棘手
  • 任意键顺序

前段时间有一个PEP提议frozerdictsPEP 0416),但最终被拒绝了。