You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Tim Chou <ti...@gmail.com> on 2014/11/08 05:16:42 UTC

How to add elements into map?

Here is the code I run in spark-shell:

val table = sc.textFile(args(1))
val histMap = collection.mutable.Map[Int,Int]()
for (x <- table) {


        val tuple = x.split('|')

          histMap.put(tuple(0).toInt, 1)


    }

Why is histMap still null?
Is there something wrong with my code?

Thanks,
Fang

Re: How to add elements into map?

Posted by Tobias Pfeiffer <tg...@preferred.jp>.
Hi,

On Sat, Nov 8, 2014 at 1:39 PM, Tim Chou <ti...@gmail.com> wrote:
>
> val table = sc.textFile(args(1))
> val histMap = collection.mutable.Map[Int,Int]()
> for (x <- table) {
>
>
>         val tuple = x.split('|')
>
>
>         histMap.put(tuple(0).toInt, 1)
>
>
>     }
>

What will happen here is that histMap (an empty Map) will be serialized and
sent to all Spark workers. Each worker will fill it locally with the data
that was processed locally, but it won't ever be sent back to the Spark
driver, that's why you don't see anything there.

Tobias

Fwd: How to add elements into map?

Posted by Tim Chou <ti...@gmail.com>.
Here is the code I run in spark-shell:

val table = sc.textFile(args(1))
val histMap = collection.mutable.Map[Int,Int]()
for (x <- table) {


        val tuple = x.split('|')


        histMap.put(tuple(0).toInt, 1)


    }

Why is histMap still null?
Is there something wrong with my code?

Thanks,
Tim

Re: How to add elements into map?

Posted by lalit1303 <la...@sigmoidanalytics.com>.
It doesn't work that way.
Following is the correct way:

val table = sc.textFile(args(1))
val histMap = table.map(x => {
    x.split('|')(0).toInt,1
})




-----
Lalit Yadav
lalit@sigmoidanalytics.com
--
View this message in context: http://apache-spark-user-list.1001560.n3.nabble.com/How-to-add-elements-into-map-tp18395p18399.html
Sent from the Apache Spark User List mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@spark.apache.org
For additional commands, e-mail: user-help@spark.apache.org