MapReduce找共同好友

用到的数据创建一个txt文件放进去就可以啦

A:B,D,E,H,I,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:B,C,D,E,O,M
G:Q,W,A,C,E,O
H:A,C,E,D,O
I:A,O
J:B,P
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J

我是用了两个mapreduce写的

第一个mapreduce

map端:
先根据":"  切分     然后在根据","  切分
String person= split[0];    //   当做   人 
String[] friends= split[1].split(",");   //  当做好友    



static class FriendMap01 extends Mapper<LongWritable, Text, Text, Text>{
		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String[] split = value.toString().split(":");	
			//好友作为key   人 作为vlaue
			String person= split[0];
			String[] friends= split[1].split(",");
			
			for (String str : friends) {
				context.write(new Text(str), new Text(person));
			}
	
		}
	}


reduce端:

static class FriendReducer01 extends Reducer<Text, Text, Text, Text>{
		@Override
		protected void reduce(Text friend, Iterable<Text> persons, Context context)
				throws IOException, InterruptedException {
			//定义一个Buffer数据做缓存
			StringBuffer sb=  new StringBuffer();
			for (Text person : persons) {
				sb.append(person).append(",");
			}
			context.write(friend, new Text(sb.toString()));
		}
	}
Driver端:
public static void main(String[] args) throws Exception, Exception {
		 //1·创建Configuration
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		//2·设置加载路径
		job.setJarByClass(Friends01.class);
		//3·设置map和reduce
		job.setMapperClass(FriendMap01.class);
		job.setReducerClass(FriendReducer01.class);
		
	    //4·设置map输出
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		//5·设置reduce输出
	    job.setOutputKeyClass(Text.class);
	    job.setOutputValueClass(Text.class);
	    //6·输入和输出路径
	    FileInputFormat.setInputPaths(job, new Path("C:\\Users\\slm\\Desktop\\datas\\friends.txt"));
	    FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\slm\\Desktop\\datas\\friend01"));
	    //提交任务,
	    job.waitForCompletion(true);
	}
第一步显示结果:
A	D,O,K,I,H,G,B,C,
B	F,E,J,A,
C	H,K,E,G,B,F,
D	H,E,F,L,K,C,A,
E	L,H,G,F,D,B,A,M,
F	D,C,M,L,
G	M,
H	O,A,
I	A,C,O,
J	O,
K	B,
L	D,E,
M	F,E,
O	F,A,G,H,I,
P	J,
Q	G,
W	G,

第二个mapreduce:

就是把上一个输出当做输入 通过切分 获取好友的人

人与人之间进行拼接 用- 做成人人对 做key 共同好友做value传到reduce端

map端:

static class FriendMap02 extends Mapper<LongWritable, Text, Text, Text>{
		@Override
		protected void map(LongWritable friend, Text value, Context context)
				throws IOException, InterruptedException {
			String[] friend_persons = value.toString().split("\t");
			String friends = friend_persons[0];
			String[] person = friend_persons[1].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(friends));
				}
			}
			
		}
	}
reduce端:


static class FriendReducer02 extends Reducer<Text, Text, Text, Text>{
		
		@Override
		protected void reduce(Text person_person, Iterable<Text> friends, Context context)
				throws IOException, InterruptedException {
				for (Text text : friends) {
					context.write(new Text(person_person), new Text(text));
				}
		
		}
	}

Driver端:

public static void main(String[] args) throws Exception {
		 //1·创建Configuration
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		//2·设置加载路径
		job.setJarByClass(Friends02.class);
		//3·设置map和reduce
		job.setMapperClass(FriendMap02.class);
		job.setReducerClass(FriendReducer02.class);
		
	    //4·设置map输出
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		//5·设置reduce输出
	    job.setOutputKeyClass(Text.class);
	    job.setOutputValueClass(Text.class);
	    //6·输入和输出路径
	    FileInputFormat.setInputPaths(job, new Path("C:\\Users\\slm\\Desktop\\datas\\friend01\\part-r-00000"));
	    FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\slm\\Desktop\\datas\\friend02"));
	    //提交任务,
	    job.waitForCompletion(true);
	}

谢谢,留下你来过的足迹!!!!!!!!!

猜你喜欢

转载自blog.csdn.net/weixin_38842096/article/details/84593096
今日推荐