Hadoop-MapReduce案例-求共同好友

13 篇文章 0 订阅
8 篇文章 0 订阅

原始文件如下:(冒号前是人名,冒号后是好友的名字) 

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

要求:求出哪些人两两之间有共同好友,及他俩的共同好友都是谁
如下:(A和D的共同好友是E和F)

A-D     E F

思路
第一步:可以把朋友作为key,人作为value,形成:友–>人,人,人 如:C -> A,B,E,F,G,H,K  即C是A,B,E,F,G,H,K的好友。这样的中间结果
代码实现:例如 将原文件中的 A:B,C,D,F,E,O 变成 (B,A) (C,A) (D,A) (F,A) (E,A) (O,A) 以(key,value)形式map输出,到了reduce中所有相同的key分组,通过reduce逻辑变成C -> A,B,E,F,G,H,K  )

第二步:把(人,人,人)进行排序,避免重复,然后进行两两匹配形成:(人-人)–>友,
代码实现:将上一步的C -> A,B,E,F,G,H,K 在map中变成 (A-B ,C),(A-E,C),(A-F,C)......输出。ruduce中会按照key(如A-B)分组,最后结果就是两两的共同好友了


第一步代码:
SharedFriendsStepOne.java

package friends;

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

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by tianjun on 2017/3/20.
 */
public class SharedFriendsStepOne {

    static class SharedFriendsStepOneMapper extends Mapper<LongWritable,Text,Text,Text> {
        Text k = new Text();
        Text v = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] person_friends = line.split(":");
            String person = person_friends[0];
            String[] friends = person_friends[1].split(",");
            for(String friend : friends){
                k.set(friend);
                v.set(person);
                //<好友,人>
                context.write(k,v);
            }
        }
    }

    static class SharedFriendsStepOneReduce extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text friend, Iterable<Text> persons, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for(Text person : persons){
                if(sb.length()!=0){
                    sb.append(",");
                }
                sb.append(person);
            }
            context.write(friend,new Text(sb.toString()));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            System.setProperty("HADOOP_USER_NAME", "root");
        }

        Configuration conf = new Configuration();

        conf.set("mapreduce.framework.name","yarn");
        conf.set("yarn.resourcemanager.hostname","mini01");
        conf.set("fs.defaultFS","hdfs://mini01:9000/");

//            默认就是local模式
//        conf.set("mapreduce.framework.name","local");
//        conf.set("mapreduce.jobtracker.address","local");
//        conf.set("fs.defaultFS","file:///");


        Job wcjob = Job.getInstance(conf);

        wcjob.setJar("F:/myWorkPlace/java/dubbo/demo/dubbo-demo/mr-demo1/target/mr.demo-1.0-SNAPSHOT.jar");

        //如果从本地拷贝,是不行的,这时需要使用setJar
//        wcjob.setJarByClass(Rjoin.class);

        wcjob.setMapperClass(SharedFriendsStepOneMapper.class);
        wcjob.setReducerClass(SharedFriendsStepOneReduce.class);

        //设置我们的业务逻辑Mapper类的输出key和value的数据类型
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(Text.class);


        //设置我们的业务逻辑Reducer类的输出key和value的数据类型
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(Text.class);


        //如果不设置InputFormat,默认就是使用TextInputFormat.class
//        wcjob.setInputFormatClass(CombineFileInputFormat.class);
//        CombineFileInputFormat.setMaxInputSplitSize(wcjob,4194304);
//        CombineFileInputFormat.setMinInputSplitSize(wcjob,2097152);


        FileSystem fs = FileSystem.get(new URI("hdfs://mini01:9000"), new Configuration(), "root");
        Path path = new Path("hdfs://mini01:9000/wc/friends/stepone");
        if (fs.exists(path)) {
            fs.delete(path, true);
        }

        //指定要处理的数据所在的位置
        FileInputFormat.setInputPaths(wcjob, new Path("hdfs://mini01:9000/input/friends"));
        //指定处理完成之后的结果所保存的位置
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://mini01:9000/wc/friends/stepone"));

        boolean res = wcjob.waitForCompletion(true);
        System.exit(res ? 0 : 1);
    }

}

结果如下:

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

第二步代码:
SharedFriendsStepTwo.java

为了防止b–>c和c–>b这样同一对朋友的重复,所以,下面基于这个结果处理的时候,需要进行排序,这样就能达到没有重复朋友对的出现。

package friends;

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

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * Created by tianjun on 2017/3/20.
 */
public class SharedFriendsStepTwo {

    static class SharedFriendsStepTwoMapper extends Mapper<LongWritable,Text,Text,Text> {
        Text k = new Text();
        Text v = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            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-2;i++){
                for(int j=i+1;j<persons.length-1;j++){
                    //<人-人,好友> ,这样相同的“人-人”对好友发到一起了
                    context.write(new Text(persons[i]+"-"+persons[j]),new Text(friend));
                }
            }
        }
    }

    static class SharedFriendsStepTwoReduce extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text person_person, Iterable<Text> friends, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for(Text friend : friends){
                sb.append(friend).append(" ");
            }
            context.write(person_person,new Text(sb.toString()));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            System.setProperty("HADOOP_USER_NAME", "root");
        }

        Configuration conf = new Configuration();

        conf.set("mapreduce.framework.name","yarn");
        conf.set("yarn.resourcemanager.hostname","mini01");
        conf.set("fs.defaultFS","hdfs://mini01:9000/");

//            默认就是local模式
//        conf.set("mapreduce.framework.name","local");
//        conf.set("mapreduce.jobtracker.address","local");
//        conf.set("fs.defaultFS","file:///");


        Job wcjob = Job.getInstance(conf);

        wcjob.setJar("F:/myWorkPlace/java/dubbo/demo/dubbo-demo/mr-demo1/target/mr.demo-1.0-SNAPSHOT.jar");

        //如果从本地拷贝,是不行的,这时需要使用setJar
//        wcjob.setJarByClass(Rjoin.class);

        wcjob.setMapperClass(SharedFriendsStepTwoMapper.class);
        wcjob.setReducerClass(SharedFriendsStepTwoReduce.class);

        //设置我们的业务逻辑Mapper类的输出key和value的数据类型
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(Text.class);


        //设置我们的业务逻辑Reducer类的输出key和value的数据类型
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(Text.class);


        //如果不设置InputFormat,默认就是使用TextInputFormat.class
//        wcjob.setInputFormatClass(CombineFileInputFormat.class);
//        CombineFileInputFormat.setMaxInputSplitSize(wcjob,4194304);
//        CombineFileInputFormat.setMinInputSplitSize(wcjob,2097152);


        FileSystem fs = FileSystem.get(new URI("hdfs://mini01:9000"), new Configuration(), "root");
        Path path = new Path("hdfs://mini01:9000/wc/friends/steptwo");
        if (fs.exists(path)) {
            fs.delete(path, true);
        }

        //指定要处理的数据所在的位置
        FileInputFormat.setInputPaths(wcjob, new Path("hdfs://mini01:9000/wc/friends/stepone"));
        //指定处理完成之后的结果所保存的位置
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://mini01:9000/wc/friends/steptwo"));

        boolean res = wcjob.waitForCompletion(true);
        System.exit(res ? 0 : 1);
    }

}

结果如下:

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

原文:https://blog.csdn.net/tianjun2012/article/details/64125224

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值