代码之家  ›  专栏  ›  技术社区  ›  alborzdev

如何在PySpark中加入两个RDD?

  •  0
  • alborzdev  · 技术社区  · 3 年前

    我很难找到正确的方式加入PySpark中的RDD,以达到预期的效果。

    Here is the first RDD:
    +------+---+
    |    _1| _2|
    +------+---+
    |Python| 36|
    |     C|  6|
    |    C#|  8|
    +------+---+
    
    Here is the second RDD:
    +------+---+
    |    _1| _2|
    +------+---+
    |Python| 10|
    |     C|  1|
    |    C#|  1|
    +------+---+
    
    Here is the result I want:
    +------+---+---+
    |    _1| _2| _3|
    +------+---+---+
    |Python| 36| 10|
    |     C|  6|  1|
    |    C#|  8|  1|
    +------+---+---+
    

    我试过各种各样的方法 .join() .union() 两个RDD之间存在差异,但无法正确使用,如有任何帮助,将不胜感激!!

    1 回复  |  直到 3 年前
        1
  •  1
  •   pltc    3 年前
    用RDD
    rdd1 = sc.parallelize([('python', 36), ('c', 6), ('c#', 8)])
    rdd2 = sc.parallelize([('python', 10), ('c', 1), ('c#', 1)])
    rdd1.join(rdd2).map(lambda x: (x[0], *x[1])).toDF().show()
    +------+---+---+
    |    _1| _2| _3|
    +------+---+---+
    |python| 36| 10|
    |     c|  6|  1|
    |    c#|  8|  1|
    +------+---+---+
    
    使用DF
    df1 = rdd1.toDF(['c1', 'c2'])
    df2 = rdd2.toDF(['c1', 'c3'])
    rdd3 = df1.join(df2, on=['c1'], how='inner').rdd