scala - Apache Spark 中的递归方法调用

标签 scala recursion apache-spark rdd

我正在从 Apache Spark 上的数据库构建家谱,使用递归搜索为数据库中的每个人找到最终的 parent (即位于家谱顶部的人)。

假设搜索他们的id时返回的第一个人是正确的 parent

val peopleById = peopleRDD.keyBy(f => f.id)
def findUltimateParentId(personId: String) : String = {

    if((personId == null) || (personId.length() == 0))
        return "-1"

    val personSeq = peopleById.lookup(personId)
    val person = personSeq(0)
    if(person.personId == "0 "|| person.id == person.parentId) {

        return person.id

    }
    else {

        return findUltimateParentId(person.parentId)

    }

}

val ultimateParentIds = peopleRDD.foreach(f => f.findUltimateParentId(f.parentId))

它给出了以下错误

"Caused by: org.apache.spark.SparkException: RDD transformations and actions can only be invoked by the driver, not inside of other transformations; for example, rdd1.map(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."



我从阅读其他类似问题中了解到问题在于我正在调用 findUltimateParentId在 foreach 循环中,如果我从 shell 中使用一个人的 id 调用该方法,它会返回正确的最终 parent id
但是,其他建议的解决方案都不适合我,或者至少我看不到如何在我的程序中实现它们,有人可以帮忙吗?

最佳答案

如果我理解正确的话——这里有一个适用于任何大小输入的解决方案(尽管性能可能不是很好)——它在 RDD 上执行 N 次迭代,其中 N 是“最深的家庭”(从祖先到 child 的最大距离)输入:

// representation of input: each person has an ID and an optional parent ID
case class Person(id: Int, parentId: Option[Int])

// representation of result: each person is optionally attached its "ultimate" ancestor,
// or none if it had no parent id in the first place
case class WithAncestor(person: Person, ancestor: Option[Person]) {
  def hasGrandparent: Boolean = ancestor.exists(_.parentId.isDefined)
}

object RecursiveParentLookup {
  // requested method
  def findUltimateParent(rdd: RDD[Person]): RDD[WithAncestor] = {

    // all persons keyed by id
    def byId = rdd.keyBy(_.id).cache()

    // recursive function that "climbs" one generation at each iteration
    def climbOneGeneration(persons: RDD[WithAncestor]): RDD[WithAncestor] = {
      val cached = persons.cache()
      // find which persons can climb further up family tree
      val haveGrandparents = cached.filter(_.hasGrandparent)

      if (haveGrandparents.isEmpty()) {
        cached // we're done, return result
      } else {
        val done = cached.filter(!_.hasGrandparent) // these are done, we'll return them as-is
        // for those who can - join with persons to find the grandparent and attach it instead of parent
        val withGrandparents = haveGrandparents
          .keyBy(_.ancestor.get.parentId.get) // grandparent id
          .join(byId)
          .values
          .map({ case (withAncestor, grandparent) => WithAncestor(withAncestor.person, Some(grandparent)) })
        // call this method recursively on the result
        done ++ climbOneGeneration(withGrandparents)
      }
    }

    // call recursive method - start by assuming each person is its own parent, if it has one:
    climbOneGeneration(rdd.map(p => WithAncestor(p, p.parentId.map(i => p))))
  }

}

这是一个测试,以更好地了解其工作原理:
/**
  *     Example input tree:
  *
  *            1             5
  *            |             |
  *      ----- 2 -----       6
  *      |           |
  *      3           4
  *
  */

val person1 = Person(1, None)
val person2 = Person(2, Some(1))
val person3 = Person(3, Some(2))
val person4 = Person(4, Some(2))
val person5 = Person(5, None)
val person6 = Person(6, Some(5))

test("find ultimate parent") {
  val input = sc.parallelize(Seq(person1, person2, person3, person4, person5, person6))
  val result = RecursiveParentLookup.findUltimateParent(input).collect()
  result should contain theSameElementsAs Seq(
    WithAncestor(person1, None),
    WithAncestor(person2, Some(person1)),
    WithAncestor(person3, Some(person1)),
    WithAncestor(person4, Some(person1)),
    WithAncestor(person5, None),
    WithAncestor(person6, Some(person5))
  )
}

将您的输入映射到这些 Person 应该很容易对象,并映射输出 WithAncestor对象变成你需要的任何东西。请注意,此代码假定如果任何人具有 parentId X - 输入中实际存在具有该 id 的另一个人

关于scala - Apache Spark 中的递归方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35464374/

相关文章:

Scala 在可变函数中下划线星号?

scala - Some(null) 到 Stringtype nullable scala.matcherror

scala - 如何在 Scala 中对列表中的两个邻居求和

javascript - 请解释一下这个递归javascript函数

java - 独立集群中的 Spark 动态分配使我的应用程序失败

java - 在 Java 或 scala 中将 CSV 转换为 Avro 文件

scala - Spark : How do I query an array in a column?

Haskell递归效率

apache-spark - 如何在 Windows 8 上构建 Spark 1.1.0?

apache-spark - Spark : cast decimal without changing nullable property of column