update variables using map function on spark

一个人想着一个人 提交于 2019-12-13 10:42:09

问题


Here is my code:

val dataRDD = sc.textFile(args(0)).map(line => line.split(" ")).map(x => Array(x(0).toInt, x(1).toInt, x(2).toInt))
var arr = new Array[Int](3)
printArr(arr)
dataRDD.map(x => {arr=x})
printArr(arr)

This code is not working properly. How can i make it work successfully?


回答1:


Okay, so operations on RDDs are performed in parallel by different workers (usually on different machines in the cluster) and therefore you cannot pass in this type of "global" object arr to be updated. You see, each worker will receive their own copy of arr which they will update, but the driver will never know.

I'm guessing that what you want to do here is to collect all the arrays from the RDD, which you can do with a simple collect action:

val dataRDD = sc.textFile(args(0)).map(line => line.split(" ")).map(x => Array(x(0).toInt, x(1).toInt, x(2).toInt))
val arr = dataRDD.collect()

Where arr has type Array[Array[Int]]. You can then run through arr with normal array operations.



来源:https://stackoverflow.com/questions/32774527/update-variables-using-map-function-on-spark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!