Spark: org.apache.spark.sql.AnalysisException: Resolved attribute(s) ... missing from....

背景:Spark scala代码处理数据。

前言:

请先检查代码,是否遗漏了字段,也就是要解析的字段XXX不存在。如果真的漏了,补上即可,不需要再往下看了。

具体报错日志如下:

ApplicationMaster: User class threw exception: org.apache.spark.sql.AnalysisException: Resolved attribute(s) team_id#51L missing from team_id#479L, … in operator !Join LeftOuter, (latest_secondary_team_id#328L = team_id#51L). Attribute(s) with the same name appear in the operation: team_id. Please check if the right attribute(s) are used.;;
可以看到少了team_id#51L,但日志里后面的所有字段中,是有team_id#479L的,但数字不一样。

什么场景下会出现这种问题:

A表joinB表的a\b\c字段得到C表,然后拿C表又 join了B表a\b\c字段时,此时可能会报错提示:Resolved attribute(s) 字段a#一个数字 missing from 字段a#另一个数字, …。推测是因为C join B如果能成功,会有2个a\b\c字段,但又不能有重名的字段。

解决方案一句话说明:

Reuse of the reference will create ambiguity in naming, so you will have to clone the df,或者重命名所有的列。(对引用的重复使用,会造成名字上的二义性,所以你需要克隆这个DataFrame或者重命名你用到的列)

我的代码:重命名所有需要的列

	// 选取需要的列,然后重命名所有列
    val recalculationTeamInfo = distinctRtxOrbacInfo.select("first_team_name")
      .withColumnRenamed("first_team_name", "new_" + "first_team_name")

PS: val dataframe2 = dataframe1不是克隆dataframe的正确写法。
val dataframe1Clone = dataframe1.as(“dataframe1Clone”)我试过这个方法,有时候可以解决这个问题的,但有时候不行,所以我一般推荐重命名列的方法,一了百了哈哈。

参考:

https://stackoverflow.com/questions/45713290/how-to-resolve-the-analysisexception-resolved-attributes-in-spark/53848160

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值