我正在使用Spark 2.2.0和Scala2.11对我的DataFrame进行一些转换。
问题出现在这行代码Math.abs($"right.product_price".asInstanceOf[Double] - $"left.product_price".asInstanceOf[Double])中。我想计算left.product_price和right.product_price之间的绝对差异。如果这些列中的任何一列包含null,则将null转换为0。
然而,我得到一个错误:“类型不匹配:期望的字符串,实际的列”。如何才能以正确的方式进行此计算?
val result = df.as("left")
// self-join by gender:
.join(df.as("right"), ($"left.gender" === $"right.gender")
// limit to 10 results per record:
.withColumn("rn", row_number().over(Window.partitionBy($"left.product_PK").orderBy($"right.product_PK")))
.filter($"rn <= 10").drop($"rn")
// group and collect_list to create products column:
.groupBy($"left.product_PK" as "product_PK")
.agg(collect_list(struct($"right.product_PK", Math.abs($"right.product_price".asInstanceOf[Double] - $"right.product_price".asInstanceOf[Double]))) as "products")https://stackoverflow.com/questions/47381216
复制相似问题