【Hadoop】44-社交粉丝数据分析

1、需求

以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的)

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

求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?

2、解题思路

2.1.第一步

Map端
读一行   A:B,C,D,F,E,O
输出    <B,A><C,A><D,A><F,A><E,A><O,A>
在读一行   B:A,C,E,K
输出   <A,B><C,B><E,B><K,B>

REDUCE端
拿到的数据比如<C,A><C,B><C,E><C,F><C,G>......
输出: 
<A-B,C>
<A-E,C>
<A-F,C>
<A-G,C>
<B-E,C>
<B-F,C>.....

2.2、第二步

Map端
读入一行<A-B,C>
直接输出<A-B,C>

Reduce端
读入数据  <A-B,C><A-B,F><A-B,G>.......
输出: A-B  C,F,G,.....

3、代码实现

public class SharedFriendsStepOne {
	static class SharedFriendsStepOneMapper extends Mapper<LongWritable, Text, Text, Text> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			// A:B,C,D,F,E,O
			String line = value.toString();
			String[] person_friends = line.split(":");
			String person = person_friends[0];
			String friends = person_friends[1];
			for (String friend : friends.split(",")) {
				// 输出<好友,人>
				context.write(new Text(friend), new Text(person));
			}
		}
	}

	static class SharedFriendsStepOneReducer extends Reducer<Text, Text, Text, Text> {
		@Override
		protected void reduce(Text friend, Iterable<Text> persons, Context context) throws IOException, InterruptedException {
			StringBuffer sb = new StringBuffer();
			for (Text person : persons) {
				sb.append(person).append(",");

			}
			context.write(friend, new Text(sb.toString()));
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(SharedFriendsStepOne.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setMapperClass(SharedFriendsStepOneMapper.class);
		job.setReducerClass(SharedFriendsStepOneReducer.class);
		FileInputFormat.setInputPaths(job, new Path("D:/srcdata/friends"));
		FileOutputFormat.setOutputPath(job, new Path("D:/temp/out"));
		job.waitForCompletion(true);
	}
}
public class SharedFriendsStepTwo {
	static class SharedFriendsStepTwoMapper extends Mapper<LongWritable, Text, Text, Text> {
		// 拿到的数据是上一个步骤的输出结果
		// A I,K,C,B,G,F,H,O,D,
		// 友 人,人,人
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			String line = value.toString();
			String[] friend_persons = line.split("\t");
			String friend = friend_persons[0];
			String[] persons = friend_persons[1].split(",");
			Arrays.sort(persons);
			for (int i = 0; i < persons.length - 1; i++) {
				for (int j = i + 1; j < persons.length; j++) {
					// 发出 <人-人,好友> ,这样,相同的“人-人”对的所有好友就会到同1个reduce中去
					context.write(new Text(persons[i] + "-" + persons[j]), new Text(friend));
				}
			}
		}
	}

	static class SharedFriendsStepTwoReducer extends Reducer<Text, Text, Text, Text> {
		@Override
		protected void reduce(Text person_person, Iterable<Text> friends, Context context) throws IOException, InterruptedException {
			StringBuffer sb = new StringBuffer();
			for (Text friend : friends) {
				sb.append(friend).append(" ");
			}
			context.write(person_person, new Text(sb.toString()));
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(SharedFriendsStepTwo.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setMapperClass(SharedFriendsStepTwoMapper.class);
		job.setReducerClass(SharedFriendsStepTwoReducer.class);
		FileInputFormat.setInputPaths(job, new Path("D:/temp/out/part-r-00000"));
		FileOutputFormat.setOutputPath(job, new Path("D:/temp/out2"));
		job.waitForCompletion(true);
	}
}

4、扩展:求互粉的人!!!!

4.1、解题思路

将上面步骤一的好友对排序,即<a,b><b,a>排序后都是<a,b><a,b>,作为key值,在reduce里面统计key的值,如果key数目为2,即认为是互为好友对。

4.2、实现

public static class FansMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
	Text text1 = new Text();
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		/**
		 * A:B,C,D,F,E,O
		 * B:A,C,E,K
		 * C:F,A,D,I
		 */
		String[] split1 = value.toString().trim().split(":");
		String cc = split1[0];
		String[] split2 = split1[1].split(",");
		//用来判断好友对的个数,如果个数等于2,则两个互粉
		for (int i = 0; i < split2.length; i++) {
			String xx = split2[i];
			if (cc.compareTo(xx) < 0) {
				text1.set(cc+"-"+xx);
			}else {
				text1.set(xx+"-"+cc);
			}
			context.write(text1, NullWritable.get());
		}
	}
}
	
public static class FansReducer extends Reducer<Text, NullWritable, Text, NullWritable>{
	@Override
	protected void reduce(Text key, Iterable<NullWritable> values, Context context)
			throws IOException, InterruptedException {
		//用来记录好友互粉的个数
		int count = 0;
		for(NullWritable dd : values){
			count++;
		}
		if (count == 2) {
			context.write(key, NullWritable.get());
		}
	}
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值