Scala—来自spark SQLContext dataframe的第一个四分位、第三个四分位和IQR,不带配置单元
我有一个数据帧:Scala—来自spark SQLContext dataframe的第一个四分位、第三个四分位和IQR,不带配置单元,scala,apache-spark-sql,Scala,Apache Spark Sql,我有一个数据帧: data.show() +--------+------+------------------+ | Count| mean| stdev| +--------+------+------------------+ | 5| 6337| 1684.569470220803| | 3| 7224| 567.8250904401182| | 330| 20280|23954.260831863092| |
data.show()
+--------+------+------------------+
| Count| mean| stdev|
+--------+------+------------------+
| 5| 6337| 1684.569470220803|
| 3| 7224| 567.8250904401182|
| 330| 20280|23954.260831863092|
| 42| 26586| 32957.9072313323|
...
| 49| 23422|21244.094701798418|
| 4| 36949| 8616.596311769514|
| 35| 20915|14971.559603562522|
| 33| 20874|16657.756963894684|
| 14| 22698|15416.614921307082|
| 25| 19100| 12342.11627585264|
| 27| 21879|21363.736895687238|
+--------+------+------------------+
在不使用Hive的情况下,我希望得到列“mean”的第一个四分位数、第二个四分位数和IQR(四分位数范围)
其他解决方案似乎使用每个人都无法访问的Hive
我想首先指出,这似乎是一个非常昂贵的解决方案,但我使用Hive获得了我想要的东西。如果你能使用蜂巢,一定要这样做,因为这再简单不过了 我最终使用了commons-math3 jar。使用它的诀窍是将数据从数据帧中取出并放入数组中,供math3库使用。我从一开始就解决了这个问题。您可能需要根据列的数据类型使用“asInstanceOf”
import org.apache.commons.math3.stat.descriptive._
// Turn dataframe column into an Array[Long]
val mean = data.select("mean").rdd.map(row => row(0).asInstanceOf[Long]).collect()
// Create the math3 object and add values from the
// mean array to the descriptive statistics array
val arrMean = new DescriptiveStatistics()
genericArrayOps(mean).foreach(v => arrMean.addValue(v))
// Get first and third quartiles and then calc IQR
val meanQ1 = arrMean.getPercentile(25)
val meanQ3 = arrMean.getPercentile(75)
val meanIQR = meanQ3 - meanQ1