Side effects w streamowaniu RDD

0

Nie rozumiem dlaczego nie chwyta side effect w poniższym przykładzie:

val data2: RDD[Array[Int]] = data.filter(x => !x.contains(i))
println(data2.count()) // 4343

var y = 2
data2.map(x => {
      y = 3
    })
println("y =" + y) // y = 2

skoro zapuszczam map po 4343 elementach to powinien mi 4343 zmienić wartość y na 3

(wiem, że nie ma to sensu, to tylko przykład. W rzeczywistości chcę dodawać se elementy do kolejki używając .map(x => ++= x))

1

https://spark.apache.org/docs/latest/rdd-programming-guide.html#rdd-operations

All transformations in Spark are lazy, in that they do not compute their results right away. Instead, they just remember the transformations applied to some base dataset (e.g. a file). The transformations are only computed when an action requires a result to be returned to the driver program.

0

a więc trzeba dodać np. collect()

1

Albo wykorzystać foreach zamiast map, skoro tak naprawdę nic nie mapujesz.

0

a tutaj co jest nie tak? Chodzi o scoping?

taka funkcja:

def addNeighbours(queue: mutable.Queue[Int], data: RDD[Array[Int]], i: Int): mutable.Queue[Int] = {
    data.filter(x => x.contains(i))
      .foreach(x => {
        println("queue length before adding = " + queue.length)
        println("elements to add length = " + x.length)
        queue ++= x
        println("queue length after adding = " + queue.length)
      })
    println("the end queue length =" + queue.length)
    queue
  }

w pętli foreach do kolejki dodają się elementy, ale po wyjściu queue jest dalej puste.
printuje:

queue length before adding = 213562
elements to add length = 111
queue length after adding = 213673
queue length before adding = 213673
elements to add length = 41
queue length after adding = 213714

the end queue length =0

1

Kij z tym zrobiłęm tak:


  def addNeighbours(queue: mutable.Queue[Int], data: RDD[Array[Int]], i: Int): mutable.Queue[Int] = {
    var neighbours = data.filter(x => x.contains(i))
      .flatMap(x => x)
      .collect()
      .toList
    queue ++= neighbours
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1