mr-to find a friend

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.