Sql 从列到数组Scala Spark
我试图在scala中的Sql 从列到数组Scala Spark,sql,arrays,scala,apache-spark,Sql,Arrays,Scala,Apache Spark,我试图在scala中的列上应用函数,但遇到了一些困难 有一个错误 found : org.apache.spark.sql.Column required: Array[Double] 有没有办法将列转换为数组? 多谢各位 更新: 非常感谢你的回答,我想我离我想要实现的目标越来越近了。我给你一点更多的背景: 代码如下: object Targa_Indicators_Full { def get_quantile (variable: Array[Double], perc:Doub
列
上应用函数,但遇到了一些困难
有一个错误
found : org.apache.spark.sql.Column
required: Array[Double]
有没有办法将列
转换为数组
?
多谢各位
更新:
非常感谢你的回答,我想我离我想要实现的目标越来越近了。我给你一点更多的背景:
代码如下:
object Targa_Indicators_Full {
def get_quantile (variable: Array[Double], perc:Double) : Double = {
val sorted_vec:Array[Double]=variable.sorted
val pos:Double= Math.round(perc*variable.length)-1
val quant:Double=sorted_vec(pos.toInt)
quant
}
def main(args: Array[String]): Unit = {
val get_quantileUDF = udf(get_quantile _)
val plate_speed =
trips_df.groupBy($"plate").agg(sum($"time_elapsed").alias("time"),sum($"space").alias("distance"),
stddev_samp($"distance"/$"time_elapsed").alias("sd_speed"),
get_quantileUDF($"distance"/$"time_elapsed",.75).alias("Quant_speed")).
withColumn("speed", $"distance" / $"time")
}
现在我得到了这个错误:
type mismatch;
[error] found : Double(0.75)
[error] required: org.apache.spark.sql.Column
[error] get_quantileUDF($"distanza"/$"tempo_intermedio",.75).alias("IQR_speed")
^
[error] one error found
我能做什么?
谢谢。您不能直接在Dataframe列上应用函数。您必须将现有函数转换为UDF。Spark提供用户定义自定义用户定义函数(UDF) 例如: 您有一个带有数组列的数据帧
scala> val df=sc.parallelize((1 to 100).toList.grouped(5).toList).toDF("value")
df: org.apache.spark.sql.DataFrame = [value: array<int>]
在应用于列之前,必须将其转换为udf
val convertUDF = udf(convert _)
然后您可以应用您的功能:
df.withColumn("new_col", convertUDF(col("value")))
您希望数组中包含什么?我正在尝试在排序后在数组中查找特定元素。问题是我在一个列上应用了这个函数,因此出现了一个错误。你能显示列的格式吗?你不能直接使用文本。您必须使用lit转换它,例如:df.withColumn(“new_col”,lit(10))
df.withColumn("new_col", convertUDF(col("value")))