package com.lq.bd; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class commFriendsOne { static class friendsMap 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(":"); for (String friend : person_friends[1].split(",")) { //输出<好友,本人> context.write(new Text(friend), new Text(person_friends[0])); } } } static class friendsReduce extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { StringBuffer sb = new StringBuffer(); for (Text person : values) { sb.append(person).append(","); } context.write(key, new Text(sb.toString())); } } public static void main(String[] args) { } }package com.lq.bd; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; import java.util.Arrays; public class commFriendsTwo { static class friendsMap extends Mapper<LongWritable, Text, Text, Text> { @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 - 2; i++) { for (int j = i + 1; j < persons.length - 1; j++) { context.write(new Text(persons[i] + "-" + persons[j]), new Text(friend)); } } } } static class friendsReduce extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { StringBuffer sb = new StringBuffer(); for (Text person : values) { sb.append(person).append(","); } context.write(key, new Text(sb.toString())); } public static void main(String[] args) { } }
mr共同好友
最新推荐文章于 2022-02-16 17:33:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)