gpt4 book ai didi

hadoop - Spark - 寻找重叠值或寻找共同 friend 的变体

转载 作者:可可西里 更新时间:2023-11-01 14:22:50 26 4
gpt4 key购买 nike

我有一个问题想用 Spark 解决。我是 Spark 的新手,所以我不确定设计它的最佳方式是什么。

输入:

group1=user1,user2
group2=user1,user2,user3
group3=user2,user4
group4=user1,user4
group5=user3,user5
group6=user3,user4,user5
group7=user2,user4
group8=user1,user5
group9=user2,user4,user5
group10=user4,user5

我想找到每对用户之间的相互组数。所以对于上面的输入,我期望的输出是:

输出:

1st user || 2nd user || mutual/intersection count || union count
------------------------------------------------------------
user1 user2 2 7
user1 user3 1 6
user1 user4 1 9
user2 user4 3 8

我认为有几种方法可以解决这个问题,其中一种解决方案可能是:

  • 创建一个键值对,键是用户,值是组
  • 按键分组,然后我们将有一个用户所属的组列表
  • 然后找到两组之间的交集/并集

例子:

(1st stage): Map
group1=user1,user2 ==>
user1, group1
user2, group1
group2=user1,user2,user3 ==>
user1, group2
user2, group2
user3, group2
....
....
....


(2nd stage): Reduce by key
user1 -> group1, group2, group4, group8
user2 -> group1, group2, group3, group7, group9

但我的问题是,在我按键减少它们之后,以我想要的方式表示计数的最佳方式是什么?

有没有更好的方法来处理这个问题?用户的最大数量是恒定的,不会超过 5000,因此这是它将创建的最大 key 数量。但是输入可能包含接近 1B 行的几行。我不认为这会是一个问题,如果我错了请纠正我。

更新:

这是我对Spark一知半解(上个月才开始学Spark)想出来的解决这个问题的代码:

def createPair(line: String): Array[(String, String)] = {
val splits = line.split("=")
val kuid = splits(0)
splits(1).split(",").map { segment => (segment, kuid) }
}


val input = sc.textFile("input/test.log")
val pair = input.flatMap { line => createPair(line) }

val pairListDF = pair
.aggregateByKey(scala.collection.mutable.ListBuffer.empty[String])(
(kuidList, kuid) => { kuidList += kuid; kuidList },
(kuidList1, kuidList2) => { kuidList1.appendAll(kuidList2); kuidList1 })
.mapValues(_.toList).toDF().select($"_1".alias("user"), $"_2".alias("groups"))

pairListDF.registerTempTable("table")

sqlContext.udf.register("intersectCount", (list1: WrappedArray[String], list2: WrappedArray[String]) => list1.intersect(list2).size)
sqlContext.udf.register("unionCount", (list1: WrappedArray[String], list2: WrappedArray[String]) => list1.union(list2).distinct.size)

val populationDF = sqlContext.sql("SELECT t1.user AS user_first,"
+ "t2.user AS user_second,"
+ "intersectCount(t1.groups, t2.groups) AS intersect_count,"
+ "unionCount(t1.groups, t2.groups) AS union_count"
+ " FROM table t1 INNER JOIN table t2"
+ " ON t1.user < t2.user"
+ " ORDER BY user_first,user_second")

输出:

+----------+-----------+---------------+-----------+
|user_first|user_second|intersect_count|union_count|
+----------+-----------+---------------+-----------+
| user1| user2| 2| 7|
| user1| user3| 1| 6|
| user1| user4| 1| 9|
| user1| user5| 1| 8|
| user2| user3| 1| 7|
| user2| user4| 3| 8|
| user2| user5| 1| 9|
| user3| user4| 1| 8|
| user3| user5| 2| 6|
| user4| user5| 3| 8|
+----------+-----------+---------------+-----------+

很想得到一些关于我的代码和我遗漏的东西的反馈。请随意批评我的代码,因为我刚刚开始学习 Spark。再次感谢@axiom 的回答,比我预期的更小更好的解决方案。

最佳答案

总结:

获取对数,然后利用事实

union(a, b) = count(a) + count(b) - intersection(a, b)

val data = sc.textFile("test")
//optionally data.cache(), depending on size of data.
val pairCounts = data.flatMap(pairs).reduceByKey(_ + _)
val singleCounts = data.flatMap(singles).reduceByKey(_ + _)
val singleCountMap = sc.broadcast(singleCounts.collectAsMap())
val result = pairCounts.map{case ((user1, user2), intersectionCount) =>(user1, user2, intersectionCount, singleCountMap.value(user1) + singleCountMap.value(user2) - intersectionCount)}


详情:

  1. 总共有 5000 个用户,2500 万个 key (每对 1 个)应该不会太多。我们可以使用 reduceByKey 来计算交点数。

  2. 个人计数可以很容易地在 map 中广播

  3. 现在众所周知:

    并集(用户 1,用户 2)= 计数(用户 1)+ 计数(用户 2)- 交集(用户 1,用户 2)

前两个计数是从广播映射中读取的,而我们映射的是对计数的 rdd。

代码:

//generate ((user1, user2), 1) for pair counts
def pairs(str: String) = {
val users = str.split("=")(1).split(",")
val n = users.length
for(i <- 0 until n; j <- i + 1 until n) yield {
val pair = if(users(i) < users(j)) {
(users(i), users(j))
} else {
(users(j), users(i))
} //order of the user in a list shouldn't matter
(pair, 1)
}
}

//generate (user, 1), to obtain single counts
def singles(str: String) = {
for(user <- str.split("=")(1).split(",")) yield (user, 1)
}


//read the rdd
scala> val data = sc.textFile("test")
scala> data.collect.map(println)
group1=user1,user2
group2=user1,user2,user3
group3=user2,user4
group4=user1,user4
group5=user3,user5
group6=user3,user4,user5
group7=user2,user4
group8=user1,user5
group9=user2,user4,user5
group10=user4,user5

//get the pair counts
scala> val pairCounts = data.flatMap(pairs).reduceByKey(_ + _)
pairCounts: org.apache.spark.rdd.RDD[((String, String), Int)] = ShuffledRDD[16] at reduceByKey at <console>:25



//just checking
scala> pairCounts.collect.map(println)
((user2,user3),1)
((user1,user3),1)
((user3,user4),1)
((user2,user5),1)
((user1,user5),1)
((user2,user4),3)
((user4,user5),3)
((user1,user4),1)
((user3,user5),2)
((user1,user2),2)

//single counts
scala> val singleCounts = data.flatMap(singles).reduceByKey(_ + _)
singleCounts: org.apache.spark.rdd.RDD[(String, Int)] = ShuffledRDD[20] at reduceByKey at <console>:25

scala> singleCounts.collect.map(println)

(user5,5)
(user3,3)
(user1,4)
(user2,5)
(user4,6)


//broadcast single counts
scala> val singleCountMap = sc.broadcast(singleCounts.collectAsMap())

//calculate the results:

最后:

scala> val res = pairCounts.map{case ((user1, user2), intersectionCount) => (user1, user2, intersectionCount, singleCountMap.value(user1) + singleCountMap.value(user2) - intersectionCount)}
res: org.apache.spark.rdd.RDD[(String, String, Int, Int)] = MapPartitionsRDD[23] at map at <console>:33

scala> res.collect.map(println)
(user2,user3,1,7)
(user1,user3,1,6)
(user3,user4,1,8)
(user2,user5,1,9)
(user1,user5,1,8)
(user2,user4,3,8)
(user4,user5,3,8)
(user1,user4,1,9)
(user3,user5,2,6)
(user1,user2,2,7)

注意:

  1. 在生成对时,我对元组进行排序,因为我们不希望用户在列表中的顺序很重要。

  2. 请将用户名字符串编码为整数,您可能会获得显着的性能提升。

关于hadoop - Spark - 寻找重叠值或寻找共同 friend 的变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com