【发布时间】:2018-08-21 09:45:12
【问题描述】:
我想将我的features(type: sparse vector of ml.linalg) 分解为每个特征的索引和值,所以我做了以下事情:
def zipKeyValue(vec:linalg.Vector) : Array[(Int,Double)] = {
val indice:Array[Int] = vec.toSparse.indices;
val value:Array[Double] = vec.toSparse.values;
indice.zip(value)
}
val udf1 = udf( zipKeyValue _)
val df1 = df.withColumn("features",udf1(col("features")));
val df2 = df1.withColumn("features",explode(col("features")) );
val udf2 = udf( ( f:Tuple2[Int,Double]) => f._1.toString ) ;
val udf3 = udf( (f:Tuple2[Int,Double]) =>f._2) ;
val df3 = df2.withColumn("key",udf2(col("features"))).withColumn("value",udf3(col("features")));
df3.show();
但我得到了错误:
Failed to execute user defined function(anonfun$38: (struct<_1:int,_2:double>) => string)
Caused by: java.lang.ClassCastException: org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema cannot be cast to scala.Tuple2
我很困惑,因为我的函数zipKeyValue 返回一个Tuple2[(Int,Double)],但实际上我得到了一个struct<_1:int,_2:double>。我该如何解决?
【问题讨论】:
标签: scala apache-spark apache-spark-sql user-defined-functions