【问题标题】:Check whether value is key of another pair pyspark检查值是否是另一对pyspark的键
【发布时间】:2020-05-24 03:49:00
【问题描述】:

我想这个问题的答案可能太明显了。

但我想知道如何获取一个不作为我的 RDD 中任何对的键的值的列表,例如

pairs = [(3,2),(1,3),(1,4)] to [2,4] 
keys = pairs.keys().distinct()

现在我想过滤对的值并只返回那些 不在键中,例如:

filteredValues = pairs.values().filter(lambda x: x not in keys)

我得到的错误:

Exception: It appears that you are attempting to broadcast an RDD or reference an RDD from an action or transformation. RDD transformations and actions can only be invoked by the driver, not inside of other transformations; for example, rdd1.map(lambda x: rdd2.values.count() * x) is invalid because the values transformation and count action cannot be performed inside of the rdd1.map transformation. For more information, see SPARK-5063.

所以我清楚地看到问题在于我在对(RDD1)过滤功能中使用键(RDD2)。 问题是,我怎样才能克服这个问题,让我的过滤仍然正确?

感谢您提供建设性帮助。 谢谢。

【问题讨论】:

    标签: python pyspark rdd


    【解决方案1】:

    读完这个问题后,我想到了自加入。不确定这是否是最简单的方法:

      val myRdd = spark.sparkContext.parallelize(Seq((1,1),(2,2),(3,4),(4,5),(5,6),(1,6)))
    
      val myRdd2 = myRdd.map(x => (x._2, x._1))     // swap values and keys
        .leftOuterJoin(myRdd)                       // and left-join with the original rdd
        .filter(x => x._2._2 == None)
        .map(x => x._1)
        .distinct()
    

    LeftJoin 返回这些元组(见下文),因此需要最后三个步骤(过滤器、映射、不同)来过滤掉结果以获得“6”

    ...
    (1,(1,Some(6)))
    (6,(5,None))
    (6,(1,None))
    (5,(4,Some(6)))
    ...
    

    【讨论】:

    • 感谢伙伴!一般理解的一个小问题。到达那里的一个明显方法是调用 collect 来获取密钥。但是,这会降低 pyspark 的效率,因为所有数据都收集到驱动程序注释中,对吗? filtersValues = pair.values().filter(lambda x: x not in keys).collect()
    • 没错。 collect 会将 spark 分布式集合 (rdd) 转换为驱动程序节点上的 python 列表。如果您的数据集不是太大(适合驱动程序进程的内存),您可以这样做,然后以纯 Python 方式处理它。但是你会使用 Spark 只是“下载”,而不是“处理”你的数据
    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2017-12-04
    • 2022-01-18
    相关资源
    最近更新 更多