Spark scala在2个数据集之间连接RDD

Spark scala在2个数据集之间连接RDD,scala,apache-spark,join,rdd,Scala,Apache Spark,Join,Rdd,假设我有两个数据集,如下所示: 数据集1: id, name, score 1, Bill, 200 2, Bew, 23 3, Amy, 44 4, Ramond, 68 id,message 1, i love Bill 2, i hate Bill 3, Bew go go ! 4, Amy is the best 5, Ramond is the wrost 6, Bill go go 7, Bill i love ya 8, Ramond is Bad 9, Amy is great

假设我有两个数据集,如下所示:

数据集1:

id, name, score
1, Bill, 200
2, Bew, 23
3, Amy, 44
4, Ramond, 68
id,message
1, i love Bill
2, i hate Bill
3, Bew go go !
4, Amy is the best
5, Ramond is the wrost
6, Bill go go
7, Bill i love ya
8, Ramond is Bad
9, Amy is great
数据集2:

id, name, score
1, Bill, 200
2, Bew, 23
3, Amy, 44
4, Ramond, 68
id,message
1, i love Bill
2, i hate Bill
3, Bew go go !
4, Amy is the best
5, Ramond is the wrost
6, Bill go go
7, Bill i love ya
8, Ramond is Bad
9, Amy is great
我想加入上述两个数据集,并根据数据集1中的姓名计算数据集2中出现的人名的最前面的数字,结果应该是:

Bill, 4
Ramond, 2 
..
..
我设法把他们两个连在一起,但不知道如何计算每个人出现的时间

如有任何建议,将不胜感激

编辑: 我的加入代码:

val rdd = sc.textFile("dataset1")
val rdd2 = sc.textFile("dataset2")
val rddPair1 = rdd.map { x =>
  var data = x.split(",")
  new Tuple2(data(0), data(1))
}
val rddPair2 = rdd2.map { x =>
  var data = x.split(",")
  new Tuple2(data(0), data(1))
}
rddPair1.join(rddPair2).collect().foreach(f =>{
  println(f._1+" "+f._2._1+" "+f._2._2)
})

使用
rdd
,实现您想要的解决方案将是复杂的。使用
dataframes
就不那么多了

第一步是将两个文件读入
dataframes
,如下所示

val df1 = sqlContext.read.format("com.databricks.spark.csv")
    .option("header", true)
  .load("dataset1")
val df2 = sqlContext.read.format("com.databricks.spark.csv")
  .option("header", true)
  .load("dataset1")
所以你应该

df1
+---+------+-----+
|id |name  |score|
+---+------+-----+
|1  |Bill  |200  |
|2  |Bew   |23   |
|3  |Amy   |44   |
|4  |Ramond|68   |
+---+------+-----+

df2
+---+-------------------+
|id |message            |
+---+-------------------+
|1  |i love Bill        |
|2  |i hate Bill        |
|3  |Bew go go !        |
|4  |Amy is the best    |
|5  |Ramond is the wrost|
|6  |Bill go go         |
|7  |Bill i love ya     |
|8  |Ramond is Bad      |
|9  |Amy is great       |
+---+-------------------+
join
groupBy
count
应按以下方式提供所需的输出:

df1.join(df2, df2("message").contains(df1("name")), "left").groupBy("name").count().as("count").show(false)
最终输出将是

+------+-----+
|name  |count|
+------+-----+
|Ramond|2    |
|Bill  |4    |
|Amy   |2    |
|Bew   |1    |
+------+-----+

您是如何加入这两个数据集的?分享这一点,加入后的计数应该使用reduceByKey或GroupByKeyThank获得评论@AmitKumar我已经添加了我的加入代码,你可以再看一看吗?基本上你是在“id”上加入这两个数据集的。在你问题中的上述数据中,基于加入“id”的数据的输出将只有一次名为“Bill”。输出是4 Ramond Amy是最好的2 Bew我讨厌Bill 3 Amy Bew go!1 Bill我爱Bill。在加入中,输出我4次没有看到名称“Bill”。确认加入列。