A b c D E FB a C d EC a b ED a B EE a b C DF a each line is the first to represent himself, followed by his friends, to find out who has a common friend and who. Here is my source code, for reference only.
Package Findfriends;import Java.io.ioexception;import Java.util.set;import java.util.treeset;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.path;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 {public static class Findmap E Xtends mapper<object,text,text,text>{public void map (Object key,text value,context Context) throws Ioexcep tion, interruptedexception{set<string> Set = new treeset<string> (); string[] str = new string[value.tostring (). Split (""). length]; Str=value.tostring (). Split (""); for (int i=0;i<str.length;i++) {set.add (str[i]); } string[] Friends = new string[set.size ()]; Friends = Set.toarray (Friends); for (int i=0;i<friends.length;i++) for (int j=i+1;j<friends.length;j++) {String friend = Friends[i ]+FRIENDS[J]; Context.write (new text (friend), new text (str[0])); }}} public static class Findreduce extends reducer<text,text,text,text>{p ublic void reduce (Text key,iterable<text> values,context Context) throws ioexception,interruptedexception{ String value = ""; for (Text val:values) {if (value== "") {value=val.tostring (); } else{value=value+ ":" +val.tostring (); }} context.write (Key, new Text (value)); }} public static void Main (string[] args) throws IOException, ClassNotFoundException, INTERRUPTEDEXCEP tion{Configuration conf = new configuration (); Job Job = new Job (conf, "findfriends"); Job.setjarbyclaSS (Findfriends.class); Job.setmapperclass (Findmap.class); Job.setreducerclass (Findreduce.class); Job.setoutputkeyclass (Text.class); Job.setoutputvalueclass (Text.class); Job.setnumreducetasks (1); Fileinputformat.addinputpath (Job, New Path (Args[0])); Fileoutputformat.setoutputpath (Job, New Path (Args[1])); System.exit (Job.waitforcompletion (true)? 0:1); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
mr-to find a friend