Spark数据集Joinwith错误:连接条件丢失或微不足道

时间:2018-10-05 06:12:46

标签: java apache-spark apache-spark-sql apache-spark-dataset

我想在Spark中加入两个数据集。这就是我所做的:

Dataset<Row> data = spark.read().format("parquet").load("hdfs://path");
Dataset<Person> p1= data.filter("id < 200").as(Encoders.bean(Person.class)).alias("ds1");
Dataset<Person> p2= data.filter("id < 100").as(Encoders.bean(Person.class)).alias("ds2");
p1.joinWith(p2, p1.col("ds1.id").equalTo(p2.col("ds2.id")) ,"inner").show();

运行程序时出现此错误:

Detected implicit cartesian product for INNER join between logical plans
Project [named_struct(id, id#3L, fname, fname#1, lname, lname#4, email, email#0, gender, gender#2) AS _1#41]
+- Filter (named_struct(id, id#3L, fname, fname#1, lname, lname#4, email, email#0, gender, gender#2).id = named_struct(id, id#3L, fname, fname#1, lname, lname#4, email, email#0, gender, gender#2).id)
   +- Relation[email#0,fname#1,gender#2,id#3L,lname#4] parquet
and
Project [named_struct(id, id#39L, fname, fname#37, lname, lname#40, email, email#36, gender, gender#38) AS _2#42]
+- Relation[email#36,fname#37,gender#38,id#39L,lname#40] parquet
Join condition is missing or trivial.
Either: use the CROSS JOIN syntax to allow cartesian products between these
relations, or: enable implicit cartesian products by setting the configuration
variable spark.sql.crossJoin.enabled=true;

从错误中我了解到的源代码是:它认为this is a cross join(第1311-1328行),但不是。

我还看到this solution也是因为这是因为结构共享相同的谱系,所以我们应该使用别名,而我却使用了别名,但是它没有用。我该如何解决这个问题?

也有与此问题相关的错误报告:spark-25150

1 个答案:

答案 0 :(得分:1)

在“ col”附近没有数据集前缀(“ p1。”,“ p2。”)必须起作用:

import static org.apache.spark.sql.functions.col;
p1.joinWith(p2, col("ds1.id").equalTo(col("ds2.id")) ,"inner").show();