【问题标题】:Use two different RDDs in Scala spark在 Scala spark 中使用两个不同的 RDD
【发布时间】:2020-09-06 21:17:58
【问题描述】:

我有:

带有我想比较的点对的RDD1

(2,5), (3,7), ...

和 RDD2 与每个点的尺寸

(0,List(5,7)), (1,List(2,4)), ...

如何获取第二个 rdd 的尺寸,以便比较第一个 rdd 的对?

(两个 rdd 都很大,我无法收集它们)
(join 不适用于不同的 rdd 架构)
https://www.mdpi.com/1999-4893/12/8/166/htm#B28-algorithms-12-00166

【问题讨论】:

  • 您在寻找 Java 或 Scala 的解决方案吗?
  • 我只在 Scala (spark) 中寻找解决方案
  • 您希望在这些坐标上执行哪些类型的操作?让我们说如果他们是来自 df1 的 (x1,y1) 和 (x2,y2) 应该怎么办?例如x3 = x1-x2 和 y3 = y1-y2 是您要查找的输出吗?
  • 每一对RDD1由两个不同点的id组成。如果我有 RDD1 的 (id1,id2) 和 RDD2 的 ((id1,(x1,y1))) 和 ((id2,(x2,y2))),我必须在这两个点 id1 和id2

标签: join compare rdd dimensions points


【解决方案1】:

添加了加入行的示例,希望它对您有用。 还可以找到可以添加/修改代码以添加逻辑的占位符

import org.apache.spark.sql.functions._

import scala.collection.mutable

object JoinRdds {

  def main(args: Array[String]): Unit = {
    val spark = Constant.getSparkSess

    import spark.implicits._
    var df1 = List((2,5),(3,7)).toDF("x","y")  // 1st Dataframe
    val df2 = List((0,List(5,7)), (1,List(2,4))).toDF("id", "coordinates")  // 2nd Dataframe

    df1 = df1.withColumn("id", monotonically_increasing_id())  // Add row num to 1st DF
//    df2.join(df1, df1("id") === df2("id"))    // perform inner join
//      .drop("id")  // drop the id column
//      .show(false)

    val rdd = df2.join(df1, df1("id") === df2("id")).rdd  // here's your RDD you can
    val resultCoordinates : Array[(Int, Int)] = rdd.map(row => { // Iterate the result row by row
      // you can do all sort of operations per row return any type here.
      val coordinates = row.getAs[mutable.WrappedArray[Int]]("coordinates")
      val x = row.getAs[Integer]("x")
      val y = row.getAs[Integer]("y")
      (coordinates(0) - x , coordinates(1) - y )
    }).collect() // the collect call on the output
    resultCoordinates.foreach(r => println(s"(${r._1},${r._2})")) // printing the output result
  }

}


【讨论】:

  • RDD1 对代表点的 id。例如 RDD1 对 (2,5) 意味着我必须将 id=2 的点与 id=5 的点进行比较。这些点的维度存储在 RDD2 中,例如 (2,(x2,y2)) 和 (5,(x5,y5))。我必须比较 (x2,y2) 和 (x5,y5)
猜你喜欢
  • 2017-06-27
  • 2015-10-18
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2018-11-24
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多