1. 程式人生 > >MapReduce案例5——求互粉好友對

MapReduce案例5——求互粉好友對

題目:
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,K

求互粉好友對:例如A的粉絲有B,B的粉絲有A,則為一對互粉好友對

思路:將資料按照從小到大的順序形成好友對,作為key值,在reduce裡面統計key的值,如果key數目為2,即認為是互為好友對。

/**
 * @author: lpj   
 * @date: 2018年3月16日 下午7:16:47
 * @Description:
 */
package lpj.reduceWork;

import java.io.IOException;
import java.security.cert.CertPathValidatorException.Reason;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;
/**
 *
 */
public class EachFansMR {
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
//		conf.addResource("hdfs-site.xml");//使用配置檔案
//		System.setProperty("HADOOP_USER_NAME", "hadoop");//使用叢集
		FileSystem fs = FileSystem.get(conf);//預設使用本地
		
		Job job = Job.getInstance(conf);
		job.setJarByClass(EachFansMR.class);
		job.setMapperClass(EachFansMR_Mapper.class);
		job.setReducerClass(EachFansMR_Reducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(NullWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
//		
//		String inputpath = args[0];
//		String outpath = args[1];
		
		Path inputPath = new Path("d:/a/homework5.txt");
		Path outputPath = new Path("d:/a/homework5");
		if (fs.exists(inputPath)) {
			fs.delete(outputPath, true);
		}
		
		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job, outputPath);
		boolean isdone = job.waitForCompletion(true);
		System.exit(isdone ? 0 : 1);
	}
	
	public static class EachFansMR_Mapper extends Mapper<LongWritable, Text, Text, NullWritable>{
		Text kout = new Text();
		Text valueout = new Text();
		@Override
		protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
			//A:B,C,D,F,E,O組合為兩兩好友對
			String [] reads = value.toString().trim().split(":");
			String kk = reads[0];
			String [] friends = reads[1].split(",");
			for(int i = 0; i < friends.length; i ++){
				String vv = friends[i];
				if (kk.compareTo(vv) < 0) {
					kout.set(kk + "-" + vv); 
				}else {
					kout.set(vv + "-" + kk);
				}
				context.write(kout, NullWritable.get());
			}
		}
	}
	public static class EachFansMR_Reducer extends Reducer<Text, NullWritable, Text, NullWritable>{
		Text kout = new Text();
		Text valueout = new Text();
		@Override
		protected void reduce(Text key, Iterable<NullWritable> values, Context context)throws IOException, InterruptedException {
			int count = 0;
			//判斷好友對數目
			for(NullWritable text : values){
				count ++;
			}
			if (count == 2) {
				context.write(key, NullWritable.get());
			}
		}
		
	}

}

結果:16對

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