MapReduce找共同好友

一、在网上找的题,就拿来做了

mapreduce笔试题: 找出有共同好友的users

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 
A,C D,F 
A,D F,E 
A,F B,C,D,E,O
B,E C
C,F A,D 
D,E L
D,F A,E 
D,L E,F 
E,L D
F,M E
H,O A

二、代码部分

package com.wangs.jun;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.hadoop.conf.Configuration;
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;


public class FindFriends {
	/**
	 * A:B,C,D
			 * B:A C:A D:A
		 */
	public static class M1 extends Mapper<LongWritable, Text, Text, Text> {
		@Override
		protected void map(LongWritable key, Text value,
				Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String[] split = value.toString().replaceAll(":", ",").split(",");
			if (split.length > 0) {
				Text v = new Text(split[0]);
				Text k = new Text();
				for (int i = 1; i < split.length; i++) {// 不能从0开始,因为0是值
					k.set(split[i]);
					context.write(k, v);
				}
			}
		}
	}


	/**
	 * B:A , B:M, B,F
		 * A,M:B A,F:B M,F:B A,B:1
		 */
	public static class R1 extends Reducer<Text, Text, Text, Text> {


		@Override
		protected void reduce(Text key, Iterable<Text> values,
				Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			List<String> f = new ArrayList<>();// A,M,F
			for (Text s : values) {
				f.add(s.toString());
			}
			String[] ss = f.toArray(new String[0]);
			for (int i = 0; i < ss.length; i++) {// M
				String s1 = ss[i];
				for (int j = 0; j < ss.length; j++) {// F
					String s2 = ss[j];
					if (!s1.equals(s2) && s1.compareTo(s2) < 0) {
						context.write(new Text(s1 + "," + s2), key);
					}
				}


				String ff = s1 + "," + key.toString();// M,B
				if (s1.compareTo(key.toString()) > 0) {// B,M:1
					ff = key.toString() + "," + s1;
				}
				context.write(new Text(ff), new Text("1"));
			}
		}
	}


	/**
	 * A,M:B A,M:C A,M:1
			 * @author liuxing
		 */
	public static class M2 extends Mapper<LongWritable, Text, Text, Text> {
		@Override
		protected void map(LongWritable key, Text value,
				Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String[] split = value.toString().split("\t");
			context.write(new Text(split[0]), new Text(split[1]));
		}
	}


	// A,M:B,C,1,1
	public static class R2 extends Reducer<Text, Text, Text, Text> {


		@Override
		protected void reduce(Text key, Iterable<Text> values,
				Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			int count = 0;
			List<String> s = new ArrayList<>();
			for (Text t : values) {
				if ("1".equals(t.toString()))
					count += 1;
				else {
					s.add(t.toString());
				}
			}
			if (count == 2 && s.size() > 0) {
				StringBuffer sb = new StringBuffer();
				for (String ss : s) {
					sb.append(",").append(ss);
				}
				context.write(key, new Text(sb.substring(1).toString()));
			}
		}
	}
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(FindFriends.class);


		job.setMapperClass(M1.class);
		job.setReducerClass(R1.class);


		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);


		FileInputFormat.setInputPaths(job, new Path(args[0]));
		Path path = new Path(args[1]);


		FileOutputFormat.setOutputPath(job, path);
		boolean b = job.waitForCompletion(true);


		if (b) {
			Job job2 = Job.getInstance(conf);
			job2.setJarByClass(FindFriends.class);
			job2.setMapperClass(M2.class);
			job2.setReducerClass(R2.class);


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


			FileInputFormat.setInputPaths(job2, new Path(args[1]));
			Path path2 = new Path(args[2]);


			FileOutputFormat.setOutputPath(job2, path2);


			job2.waitForCompletion(true);
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值