gpt4 book ai didi

java - 查找所有用户的好友# : How to implement with Hadoop Mapreduce?

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

假设我有如下输入:

(1,2)(2,1)(1,3)(3,2)(2,4)(4,1) 

预期输出如下:

(1,(2,3,4)) -> (1,3) //second index is total friend #
(2,(1,3,4)) -> (2,3)
(3,(1,2)) -> (3,2)
(4,(1,2)) -> (4,2)

我知道如何在 Java 中使用哈希集来做到这一点。但不知道这如何与 mapreduce 模型一起工作。任何人都可以就这个问题提出任何想法或示例代码吗?我会很感激的。

-------------------------------------------- --------------------------------------

这是我天真的解决方案:1 个映射器,两个 reducer 。映射器 将组织输入(1,2),(2,1),(1,3);

将输出组织为

*(1,hashset<2>),(2,hashSet<1>),(1,hashset<2>),(2 ,哈希集<1>),(1,哈希集<3>),(3,哈希集<1>).*

Reducer1:

将mapper的输出作为输入,输出为:

*(1,hashset<2,3>), (3,hashset<1>)and (2,hashset<1>)< em>*

Reducer2:

将reducer1的输出作为输入,输出为:

*(1,2)、(3,1) 和 (2,1)*

这只是我天真的解决方案。我不确定这是否可以通过 hadoop 的代码来完成。

最佳答案

我认为应该有一个简单的方法来解决这个问题。

Mapper Input: (1,2)(2,1)(1,3)(3,2)(2,4)(4,1)

像这样为每一对发出两条记录:

Mapper Output/ Reducer Input:

Key => Value
1 => 2
2 => 1
2 => 1
1 => 2
1 => 3
3 => 1
3 => 2
2 => 3
2 => 4
4 => 2
4 => 1
1 => 1

在 reducer 端,你会得到 4 个不同的组,如下所示:

Reducer Output:

Key => Values
1 => [2,3,4]
2 => [1,3,4]
3 => [1,2]
4 => [1,2]

现在,您可以根据需要格式化结果。 :)让我知道是否有人可以看到这种方法中的任何问题

关于java - 查找所有用户的好友# : How to implement with Hadoop Mapreduce?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20389529/

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