MapReduce实现QQ好友推荐

大家都知道qq用户量上亿,每个用户又有很多的好友,因此,数据量十分的庞大,如何才能实现QQ的好友推荐呢? 下面举一个例子: A有QQ好友B B有QQ好友C 则A,C有可能是好友。 当A登录的时候,则会向A推荐C,当C登录的时候,则会向C推荐A。

Demo

输入数据

map阶段key:主value:从key:从value:主将一条记录分别作为key,,value进行输出。tom–>jasonjason–>tomtom–>lgdlgd–>tomreduce阶段将同一个key的values值进行两两组合。package FriendsRecommended;import javaimport javaimport javaimport javaimport javaimport javaimport orgimport orgimport orgimport orgimport orgimport orgimport orgimport orgimport orgimport orgimport orgpublic class FindFriends {private final static String INPUT_PATH = “hdfs://liguodong:8020/liguodong”;private final static String OUTPUT_PATH = “hdfs://liguodong:8020/liguodong/QQFriendRecommended”;public static void main(String[] args) throws IOException,URISyntaxException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH),conf);if(fileSystem.exists(new Path(OUTPUT_PATH))){fileSystem.delete(new Path(OUTPUT_PATH),true);}Job job = Job.getInstance(conf, “qq friend recommended”); job.setJarByClass(FindFriends.class);FileInputFormat.addInputPath(job, new Path(INPUT_PATH)); job.setMapperClass(MyMapper.class);job.setReducerClass(MyReudcer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));//提交作业System.exit(job.waitForCompletion(true) ? 0 : 1);}public static class MyMapper extends Mapper<LongWritable,Text,Text,Text>{@Overrideprotected void map(LongWritable k1, Text v1, Context context)throws IOException, InterruptedException {String line = v1.toString();String[] ss = line.split(“\\s+”);context.write(new Text(ss[0]), new Text(ss[1]));context.write(new Text(ss[1]), new Text(ss[0]));}}public static class MyReudcer extends Reducer<Text, Text, Text, Text>{@Overrideprotected void reduce(Text k2, Iterable<Text> v2s,Context context)throws IOException, InterruptedException {Set<String> set = new HashSet<String>();for (Text v2:v2s) {set.add(v2.toString());}if (set.size()>1) {for (Iterator i = set.iterator();i.hasNext();) {String qqName = (String)i.next();for (Iterator j = set.iterator();j.hasNext();){String otherqqName = (String)j.next();if(!qqName.equals(otherqqName)){context.write(new Text(qqName), new Text(otherqqName));}}}}}}}

输出结果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

要想捉大鱼,不能怕水深。要想摘玫瑰,就得不怕刺。

MapReduce实现QQ好友推荐

相关文章:

你感兴趣的文章:

标签云: