开发者社区> 问答> 正文

Dataframes join在Spark Scala中返回空结果

我在Spark Scala中有四个数据框(Spark版本:2.3和Spark-sql:2.11和Scala版本:2.11.0),例如:

ratingsDf

ratings id
0 1
1 2
1 3
0 4
0 5
1 6
1 7
1 8
0 9
1 10

GpredictionsDf

gprediction id
0 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
0 9
1 10

RpredictionsDf

rprediction id
0 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10

LpredictionsDf

lprediction id
0 1
1 2
1 3
0 4
1 5
1 6
1 7
1 8
0 9
1 10

我需要通过连接“id”列上的所有四个表来创建一个DataFrame。我试过以下两种方法来做到这一点:

方法1:

val ensembleDf = GpredictionsDf.join(rpredjoin, gpredjoin("id") === RpredictionsDf("id"))

                           .join(LpredictionsDf, LpredictionsDf("id") === RpredictionsDf("id"))
                           .join(ratingsDf, ratingsDf("id") === RpredictionsDf("id"))
                           .select("gprediction", "rprediction", "lprediction", "ratings")

方法2:

ratingsDf.createOrReplaceTempView("ratingjoin");
GpredictionsDf.createOrReplaceTempView("gpredjoin")
RpredictionsDf.createOrReplaceTempView("rpredjoin")
LpredictionsDf.createOrReplaceTempView("lpredjoin")

val ensembleDf = sqlContext.sql("SELECT gprediction, rprediction, lprediction, ratings FROM gpredjoin, rpredjoin, lpredjoin, ratingjoin WHERE " +
"gpredjoin.id = rpredjoin.id AND rpredjoin.id = lpredjoin.id AND lpredjoin.id = ratingjoin.id");
但是,在这两种情况下,我的联接失败并返回空

ensembleDf.show();

知道为什么会这样吗?为了解决这个问题,我需要做些什么代码更改?

展开
收起
社区小助手 2018-12-06 15:35:03 3025 0
1 条回答
写回答
取消 提交回答
  • 社区小助手是spark中国社区的管理员,我会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关spark的问题及回答。
    scala> val ratingsDf = Seq((0,1),(1,2),(1,3),(0,4),(0,5),(1,6),(1,7),(1,8),(0,9),(1,10)).toDF("ratings","id")
    

    scala> val GpredictionsDf = Seq((0,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(0,9),(1,10)).toDF("gprediction", "id")

    scala> val RpredictionsDf = Seq((0,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10)).toDF("rprediction", "id")

    scala> val LpredictionsDf = Seq((0,1),(1,2),(1,3),(0,4),(1,5),(1,6),(1,7),(1,8),(0,9),(1,10)).toDF("lprediction", "id")

    scala> val ensembleDf = GpredictionsDf.join(RpredictionsDf, GpredictionsDf("id") === RpredictionsDf("id") ).join(LpredictionsDf, LpredictionsDf("id") === RpredictionsDf("id")).join(ratingsDf, ratingsDf("id") === RpredictionsDf("id")).select("gprediction", "rprediction", "lprediction", "ratings")

    scala> ensembleDf.show

    +-----------+-----------+-----------+-------+
    |gprediction|rprediction|lprediction|ratings|
    +-----------+-----------+-----------+-------+
    |          0|          0|          0|      0|
    |          1|          1|          1|      1|
    |          1|          1|          1|      1|
    |          1|          1|          0|      0|
    |          1|          1|          1|      0|
    |          1|          1|          1|      1|
    |          1|          1|          1|      1|
    |          1|          1|          1|      1|
    |          0|          1|          0|      0|
    |          1|          1|          1|      1|
    +-----------+-----------+-----------+-------+
    2019-07-17 23:18:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hybrid Cloud and Apache Spark 立即下载
Scalable Deep Learning on Spark 立即下载
Comparison of Spark SQL with Hive 立即下载