//map
package com.gh.day2_4;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
//A: b,c,f,h,j
//B:f,g,d
friend person
//map输出为b,c,f,h,j /A
public class FriendMap extends Mapper<LongWritable, Text,Text,Text> {
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,Text,Text>.Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("?;
String person = split[0];
String split1 = split[1];
String[] friend = split1.split(",");
for (String friends:friend
) {
context.write(new Text(friends), new Text(person));
}
}
}
//reduce
package com.gh.day2_4;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
friend person
// reduce处理成 A: b ,d,f,g
public class FriendReduce extends Reducer<Text,Text,Text,Text> {
protected void reduce(Text friend, Iterable
StringBuffer sb=new StringBuffer();
for (Text friends:values
) {
sb.append(friends).append(",");
}
context.write(friend,new Text(sb.toString()));
}
}
//map2
package com.gh.day2_4;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.Arrays;
person friend
//对persons进行排序,排列成A-B c,
public class FriendMap2 extends Mapper<LongWritable, Text,Text,Text> {
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,Text,Text>.Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("\t");
String friend = split[0];
String split1 = split[1];
String[] person = split1.split(",");
Arrays.sort(person);
for (int i = 0; i <person.length-1 ; i++) {
for (int j = i+1; j <person.length ; j++) {
context.write(new Text(person[i]+"-"+person[j]), new Text(friend));
}
}
}
}
//reduce2
package com.gh.day2_4;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
//处理成 A-B C,D,F
public class FriendReduce2 extends Reducer<Text,Text,Text,Text> {
protected void reduce(Text person, Iterable
StringBuffer sb=new StringBuffer();
for (Text friends:values
) {
sb.append(friends).append(",");
}
context.write(person,new Text(sb.toString()));
}
}
package com.gh.day2_4;
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 FriendText {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String inPath = “D:/ww/input11”;
String outPath = “D:/ww/output11”;
Configuration conf = new Configuration();
Job job = null;
job = Job.getInstance(conf);
job.setJarByClass(FriendText.class);
job.setMapperClass(FriendMap.class);
job.setReducerClass(FriendReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(inPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : -1);
}
}
package com.gh.day2_4;
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 FriendText2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String inPath = “D:/ww/output11”;
String outPath = “D:/ww/output12”;
Configuration conf = new Configuration();
Job job = null;
job = Job.getInstance(conf);
job.setJarByClass(FriendText2.class);
job.setMapperClass(FriendMap2.class);
job.setReducerClass(FriendReduce2.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(inPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : -1);
}
}