首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型不匹配:预期字符串,实际列

类型不匹配:预期字符串,实际列
EN

Stack Overflow用户
提问于 2017-11-20 03:42:23
回答 0查看 2.4K关注 0票数 2

我正在使用Spark 2.2.0和Scala2.11对我的DataFrame进行一些转换。

问题出现在这行代码Math.abs($"right.product_price".asInstanceOf[Double] - $"left.product_price".asInstanceOf[Double])中。我想计算left.product_priceright.product_price之间的绝对差异。如果这些列中的任何一列包含null,则将null转换为0

然而,我得到一个错误:“类型不匹配:期望的字符串,实际的列”。如何才能以正确的方式进行此计算?

代码语言:javascript
复制
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")
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47381216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档