import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 FriendsOfFriends {
public static class FriendsMapper extends Mapper<Object, Text, Text, Text> {
private Text userPair = new Text();
private Text mutualFriend = new Text();
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] tokens = value.toString().split("\t");
if (tokens.length == 2) {
String user = tokens[0];
String[] friends = tokens[1].split(",");
Arrays.sort(friends); // Sort the friends to ensure consistent key ordering
for (int i = 0; i < friends.length; i++) {
for (int j = i + 1; j < friends.length; j++) {
userPair.set(friends[i] + "," + friends[j]);
mutualFriend.set(user);
context.write(userPair, mutualFriend);
}
}
}
}
}
public static class FriendsReducer extends Reducer<Text, Text, Text, IntWritable> {
private IntWritable mutualFriendsCount = new IntWritable();
@Override
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Set<String> mutualFriendsSet = new HashSet<>();
for (Text value : values) {
mutualFriendsSet.add(value.toString());
}
mutualFriendsCount.set(mutualFriendsSet.size());
context.write(key, mutualFriendsCount);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "FriendsOfFriends");
job.setJarByClass(FriendsOfFriends.class);
job.setMapperClass(FriendsMapper.class);
job.setReducerClass(FriendsReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.
exit(job.
waitForCompletion(true) ? 0 : 1); }
}