IDEA上找博客共同好友案例

FFMapper1类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.atguigu.friend;

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

import java.io.IOException;

public class FFMapper1 extends Mapper<LongWritable,Text,Text,Text> {
private Text k = new Text();
private Text v = new Text();

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(":");
//关注者作为value
v.set(split[0]);

//被关注者作为key
for (String man : split[1].split(",")) {
k.set(man);
context.write(k,v);
}
}
}

FFReducer1类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.atguigu.friend;

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

import java.io.IOException;

public class FFReducer1 extends Reducer<Text,Text, Text,Text> {
private Text v = new Text();
private StringBuilder sb = new StringBuilder();

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//被关注的人为key,关注他的人为values
sb.delete(0,sb.length());
for (Text value : values) {
sb.append(value.toString()).append(",");
}
v.set(sb.toString());

context.write(key,v);
}
}

FFMapper2类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.atguigu.friend;

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

import java.io.IOException;

public class FFMapper2 extends Mapper<LongWritable, Text,Text,Text> {
private Text k = new Text();
private Text v = new Text();

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("\t");
v.set(split[0]);

String[] men = split[1].split(",");
for (int i = 0; i < men.length; i++) {
for (int i1 = i+1; i1 < men.length; i1++) {
if(men[i].compareTo(men[i1]) > 0)
k.set(men[i1] + "-" + men[i]);
else
k.set(men[i] + "-" + men[i1]);

context.write(k,v);
}

}
}
}

FFReducer2类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.atguigu.friend;

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

import java.io.IOException;

public class FFReducer2 extends Reducer<Text,Text,Text,Text> {
private Text v = new Text();
private StringBuilder sb = new StringBuilder();


@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
sb.delete(0,sb.length());
for (Text value : values) {
sb.append(value.toString()).append(",");
}
v.set(sb.toString());

context.write(key,v);
}
}

FFDriver类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.atguigu.friend;

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;

import java.io.IOException;

public class FFDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job1 = Job.getInstance(new Configuration());
job1.setJarByClass(FFDriver.class);
job1.setMapperClass(FFMapper1.class);
job1.setReducerClass(FFReducer1.class);

job1.setMapOutputKeyClass(Text.class);
job1.setMapOutputValueClass(Text.class);
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(Text.class);

FileInputFormat.setInputPaths(job1,new Path("F:/input"));
FileOutputFormat.setOutputPath(job1,new Path("F:/output"));
boolean b = job1.waitForCompletion(true);
if (b) {
Job job2 = Job.getInstance(new Configuration());
job2.setJarByClass(FFDriver.class);
job2.setMapperClass(FFMapper2.class);
job2.setReducerClass(FFReducer2.class);

job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(Text.class);
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(Text.class);

FileInputFormat.setInputPaths(job2, new Path("F:/output"));
FileOutputFormat.setOutputPath(job2, new Path("F:/output1"));
boolean b2 = job2.waitForCompletion(true);
System.exit(b2 ? 0 : 1);
}
}
}