For the first one,

 input.map { case(x,l) => (x, l.reduce(_ + _) ) }

will do what you need.
For the second, yes, there's a difference, one is a List the other is a
Tuple. See for instance
See for instance
val a = (1,2,3)
a.getClass.getName
res4: String = scala.Tuple3

You should look up tuples in the Scala doc as they are not specific to
spark, in particular read up about case classes and pattern matching.


2016-01-03 12:00 GMT-08:00 jimitkr <ji...@softpath.net>:

> Hi,
>
> I've created tuples of type (String, List[Int]) and want to sum the values
> in the List[Int] part, i.e. the 2nd element in each tuple.
>
> Here is my list
> /  val
> input=sc.parallelize(List(("abc",List(1,2,3,4)),("def",List(5,6,7,8))))/
>
> I want to sum up values in the 2nd element of the tuple so that the output
> is
> (abc,10)
> (def, 26)
>
> I've tried fold, reduce, foldLeft but with no success in my below code to
> calculate total:
> /    val valuesForDEF=input.lookup("def")
>     val totalForDEF: Int = valuesForDEF.toList.reduce((x: Int,y: Int)=>x+y)
>     println("THE TOTAL FOR DEF IS" + totalForDEF)/
>
> How do i calculate the total?
>
> Another query. What will be the difference between the following tuples
> when
> created:
> /  val
> input=sc.parallelize(List(("abc",List(1,2,3,4)),("def",List(5,6,7,8))))/
> /  val input=sc.parallelize(List(("abc",(1,2,3,4)),("def",(5,6,7,8))))/
>
> Is there a difference in how (1,2,3,4) and List(1,2,3,4) is handled?
>
>
>
> --
> View this message in context:
> http://apache-spark-user-list.1001560.n3.nabble.com/Calculate-sum-of-values-in-2nd-element-of-tuple-tp25865.html
> Sent from the Apache Spark User List mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@spark.apache.org
> For additional commands, e-mail: user-h...@spark.apache.org
>
>


-- 
--------------------------------------------------------------
"Good judgment comes from experience.
Experience comes from bad judgment"
--------------------------------------------------------------

Reply via email to