黑猴子的家:MapReduce 找微信共同好友分析

1、数据

https://www.jianshu.com/p/1613f171f466

2、需求

以上是微信的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的)
求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?

3、需求分析

先求出A、B、C、….等是谁的好友
第一次输出结果

A   I,K,C,B,G,F,H,O,D,
B   A,F,J,E,
C   A,E,B,H,F,G,K,
D   G,C,K,A,L,F,E,H,
E   G,M,L,H,A,F,B,D,
F   L,M,D,C,G,A,
G   M,
H   O,
I   O,C,
J   O,
K   B,
L   D,E,
M   E,F,
O   A,H,I,J,F,

第二次输出结果

A-B E C 
A-C D F 
A-D E F 
A-E D B C 
A-F O B C D E 
A-G F E C D 
A-H E C D O 
A-I O 
A-J O B 
A-K D C 
A-L F E D 
A-M E F 
B-C A 
B-D A E 
B-E C 
B-F E A C 
B-G C E A 
B-H A E C 
B-I A 
B-K C A 
B-L E 
B-M E 
B-O A 
C-D A F 
C-E D 
C-F D A 
C-G D F A 
C-H D A 
C-I A 
C-K A D 
C-L D F 
C-M F 
C-O I A 
D-E L 
D-F A E 
D-G E A F 
D-H A E 
D-I A 
D-K A 
D-L E F 
D-M F E 
D-O A 
E-F D M C B 
E-G C D 
E-H C D 
E-J B 
E-K C D 
E-L D 
F-G D C A E 
F-H A D O E C 
F-I O A 
F-J B O 
F-K D C A 
F-L E D 
F-M E 
F-O A 
G-H D C E A 
G-I A 
G-K D A C 
G-L D F E 
G-M E F 
G-O A 
H-I O A 
H-J O 
H-K A C D 
H-L D E 
H-M E 
H-O A 
I-J O 
I-K A 
I-O A 
K-L D 
K-O A 
L-M E F

4、 One - MapperReduce

(1)第一次Mapper

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class OneShareFriendsMapper extends Mapper<LongWritable, Text, Text, Text>{
    
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
            throws IOException, InterruptedException {
        // 1 获取一行 A:B,C,D,F,E,O
        String line = value.toString();
        
        // 2 切割
        String[] fileds = line.split(":");
        
        // 3 获取person和好友
        String person = fileds[0];
        String[] friends = fileds[1].split(",");
        
        // 4写出去
        for(String friend: friends){
            // 输出 <好友,人>
            context.write(new Text(friend), new Text(person));
        }
    }
}

(2)第一次Reducer

import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class OneShareFriendsReducer extends Reducer<Text, Text, Text, Text>{
    
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException {
        
        StringBuffer sb = new StringBuffer();
        //1 拼接
        for(Text person: values){
            sb.append(person).append(",");
        }
        
        //2 写出
        context.write(key, new Text(sb.toString()));
    }
}

(3)第一次Driver

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class OneShareFriendsDriver {

    public static void main(String[] args) throws Exception {
        // 1 获取job对象
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        
        // 2 指定jar包运行的路径
        job.setJarByClass(OneShareFriendsDriver.class);

        // 3 指定map/reduce使用的类
        job.setMapperClass(OneShareFriendsMapper.class);
        job.setReducerClass(OneShareFriendsReducer.class);
        
        // 4 指定map输出的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        
        // 5 指定最终输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        
        // 6 指定job的输入原始所在目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        
        // 7 提交
        boolean result = job.waitForCompletion(true);
        
        System.exit(result?1:0);
    }
}

5、Two - MapperReduce

(1)第二次Mapper

package com.atguigu.mapreduce.friends;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class TwoShareFriendsMapper extends Mapper<LongWritable, Text, Text, Text>{
    
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        // A I,K,C,B,G,F,H,O,D,
        // 友 人,人,人
        String line = value.toString();
        String[] friend_persons = line.split("\t");

        String friend = friend_persons[0];
        String[] persons = friend_persons[1].split(",");

        Arrays.sort(persons);

        for (int i = 0; i < persons.length - 1; i++) {
            
            for (int j = i + 1; j < persons.length; j++) {
                // 发出 <人-人,好友> ,这样,相同的“人-人”对的所有好友就会到同1个reduce中去
                context.write(new Text(persons[i] + "-" + persons[j]), new Text(friend));
            }
        }
    }
}

(2)第二次Reducer

import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class TwoShareFriendsReducer extends Reducer<Text, Text, Text, Text>{
    
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException {
        
        StringBuffer sb = new StringBuffer();

        for (Text friend : values) {
            sb.append(friend).append(" ");
        }
        
        context.write(key, new Text(sb.toString()));
    }
}

(3)第二次Driver


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class TwoShareFriendsDriver {

    public static void main(String[] args) throws Exception {
        // 1 获取job对象
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        
        // 2 指定jar包运行的路径
        job.setJarByClass(TwoShareFriendsDriver.class);

        // 3 指定map/reduce使用的类
        job.setMapperClass(TwoShareFriendsMapper.class);
        job.setReducerClass(TwoShareFriendsReducer.class);
        
        // 4 指定map输出的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        
        // 5 指定最终输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        
        // 6 指定job的输入原始所在目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        
        // 7 提交
        boolean result = job.waitForCompletion(true);
        
        System.exit(result?1:0);
    }
}

6、Code -> GitHub

https://github.com/liufengji/hadoop_mapreduce.git

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • MapReduce执行流程 MapReduce的执行步骤 1、Map任务处理 1.1 读取HDFS中的文件。每一行...
    依天立业阅读 6,720评论 0 8
  • 在一堆给定的文本文件中统计输出每一个单词出现的总次数 Code -> GitHubhttps://github.c...
    黑猴子的家阅读 4,421评论 0 6
  • 投射我们家的天然气能在我中午下班前修好,而且不用花钱。 投射老公主动关心家里情况,关心家人。 投射今天过得顺心喜悦...
    丽丽丫丫阅读 1,207评论 0 0
  • 与荷同生(原创) 你是凌波里盈盈的笑 深情、清澈而透灵 你是碧水里浅浅的温柔 每一瓣馨香都不忍触碰 你是月夜下曼妙...
    千誉嘉言阅读 2,755评论 23 20

友情链接更多精彩内容