簡體   English   中英

在 Scala spark 中使用兩個不同的 RDD

[英]Use two different RDDs in Scala spark

我有:

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

添加了加入行的示例,希望它對您有用。 還可以找到可以添加/修改代碼以添加邏輯的占位符

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
  }

}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM