且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Spark/Scala中求和数据框的一列的值

更新时间:2023-11-18 23:22:16

如果要sum一列的所有值,则使用DataFrame的内部RDDreduce效率更高./p>

If you want to sum all values of one column, it's more efficient to use DataFrame's internal RDD and reduce.

import sqlContext.implicits._
import org.apache.spark.sql.functions._

val df = sc.parallelize(Array(10,2,3,4)).toDF("steps")
df.select(col("steps")).rdd.map(_(0).asInstanceOf[Int]).reduce(_+_)

//res1 Int = 19