【问题标题】:spark join causing column id ambiguity error火花连接导致列 ID 歧义错误
【发布时间】:2019-08-25 22:49:24
【问题描述】:

我有以下数据框:

accumulated_results_df
 |-- company_id: string (nullable = true)
 |-- max_dd: string (nullable = true)
 |-- min_dd: string (nullable = true)
 |-- count: string (nullable = true)
 |-- mean: string (nullable = true)

computed_df
 |-- company_id: string (nullable = true)
 |-- min_dd: date (nullable = true)
 |-- max_dd: date (nullable = true)
 |-- mean: double (nullable = true)
 |-- count: long (nullable = false)

尝试使用 spark-sql 进行连接,如下所示

 val resultDf = accumulated_results_df.as("a").join(computed_df.as("c"), 
                             ( $"a.company_id" === $"c.company_id" ) && ( $"c.min_dd" > $"a.max_dd" ), "left")

给出错误:

org.apache.spark.sql.AnalysisException: Reference 'company_id' is ambiguous, could be: a.company_id, c.company_id.;

我在这里做错了什么以及如何解决这个问题?

【问题讨论】:

  • 您应该使用别名来消除歧义。你可以看这里stackoverflow.com/questions/33778664/…
  • 之后,您是否尝试从 resultDf 中选择 company_id?该列可能会被复制。
  • @Shaido 不...但我得到两个数据框的列,但我只想留下数据框列怎么做?有什么想法吗?
  • @Anshul 谢谢,但那是在 pyspark 中,对吗?我在 scala 中这样做

标签: apache-spark apache-spark-sql datastax spark-cassandra-connector


【解决方案1】:

应该使用 col 函数来正确引用别名数据框和列

val resultDf = (accumulated_results_df.as("a")
       .join(
           computed_df.as("c"),
           (col("a.company_id") === col("c.company_id")) && (col("c.min_dd") > col("a.max_dd")), 
           "left"
        )

【讨论】:

【解决方案2】:

我已经修复了它,如下所示。

val resultDf = accumulated_results_df.join(computed_df.withColumnRenamed("company_id", "right_company_id").as("c"), 
                             (  accumulated_results_df("company_id" ) === $"c.right_company_id" && ( $"c.min_dd" > accumulated_results_df("max_dd") ) )
                        , "left")

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多