1. foreach doesn't expect any value from function being passed (in your func_foreach). so nothing happens. The return values are just lost. it's like calling a function without saving return value to another var. foreach also doesn't return anything so you don't get modified RDD (like map*). 2. RDD's are immutable. All transform functions (map*,groupBy*,reduceBy etc.) return new RDD. 3. Yes. It's just iterates through elements and calls the function being passed. That's it. It doesn't collect the values and don't return any new modified RDD.
On Mon, Jan 18, 2016 at 11:10 PM, charles li <charles.up...@gmail.com> wrote: > > hi, great thanks to david and ted, I know that the content of RDD can be > returned to driver using 'collect' method. > > but my question is: > > > 1. cause we can write any code we like in the function put into 'foreach', > so what happened when we actually write a 'return' sentence in the foreach > function? > 2. as the photo shows bellow, the content of RDD doesn't change after > foreach function, why? > 3. I feel a little confused about the 'foreach' method, it should be an > 'action', right? cause it return nothing. or is there any best practice of > the 'foreach' funtion? or can some one put your code snippet when using > 'foreach' method in your application, that would be awesome. > > > great thanks again > > > > > > On Tue, Jan 19, 2016 at 11:44 AM, Ted Yu <yuzhih...@gmail.com> wrote: > >> Here is signature for foreach: >> def foreach(f: T => Unit): Unit = withScope { >> >> I don't think you can return element in the way shown in the snippet. >> >> On Mon, Jan 18, 2016 at 7:34 PM, charles li <charles.up...@gmail.com> >> wrote: >> >>> code snippet >>> >>> >>> >>> the 'print' actually print info on the worker node, but I feel confused >>> where the 'return' value >>> goes to. for I get nothing on the driver node. >>> -- >>> *--------------------------------------* >>> a spark lover, a quant, a developer and a good man. >>> >>> http://github.com/litaotao >>> >> >> > > > -- > *--------------------------------------* > a spark lover, a quant, a developer and a good man. > > http://github.com/litaotao >