mapreduce系列(7)--查找共同好友

一、概述

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-b :  c ,e

思路:
首先可以第一步可以把朋友作爲key,人作爲value,形成:友–>人,人,人。這樣的中間結果
第二把,把(人,人,人)進行排序,避免重複,然後進行兩兩匹配形成:(人-人)–>友。這樣的鍵值對,進行mr統計,最後結果就是兩兩的共同好友了
第一步代碼:
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

爲了防止b–>c和c–>b這樣同一對朋友的重複,所以,下面基於這個結果處理的時候,需要進行排序,這樣就能達到沒有重複朋友對的出現。

第二步:
SharedFriendsStepTwo.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;
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);
    }

}

最後計算得出的兩倆好友如下:

[root@mini03 ~]# hdfs dfs -cat /wc/friends/steptwo/*
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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章