2017-06-09 97 views
0

我在星火斯卡拉2個dataframes一個共同的領域,但其中之一是由唯一的列組成。我不得不加入他們,但他們沒有共同的列。行數是相同的。連接兩個數據幀沒有火花斯卡拉

val userFriends=userJson.select($"friends",$"user_id") 
val x = userFriends("friends") 
     .rdd 
     .map(x => x.getList(0).toArray.map(_.toString)) 
val y = x.map(z=>z.count(z=>true)).toDF("friendCount") 

我不得不加入userFriends其中y

+0

問題是什麼?到目前爲止你做了什麼 ? – Badda

+0

val userFriends = userJson.select($「friends」,$「user_id」) val x = userFriends(「friends」)。rdd.map(x => x.getList(0).toArray.map(_。 toString)) val y = x.map(z => z.count(z => true))。toDF(「friendCount」) 我必須加入userFriends與y – mastMarvizz

+1

@mastMarvizz歡迎來到SO。我已將代碼添加到問題中。按照例如下一次:-) – maasg

回答

0

這是不可能加入他們沒有共同的領域,除非你可以依靠排序,在這種情況下,你可以使用行數(與窗口 - 功能)在兩個dataframes和加入的行數。

但在你的情況下,這似乎沒有必要,只要保持user_id列在你的數據幀,這樣的事情應該工作:

val userFriends=userJson.select($"friends",$"user_id") 

val result_df = 
    userFriends.select($"friends",$"user_id") 
    .rdd 
    .map(x => (x.getList(0).toArray.map(_.toString).count(z=>true)),x.getInt(1))) 
    .toDF("friendsCount","user_id") 
+0

太感謝你了! – mastMarvizz