MapReduce寻找共同好友
生活随笔
收集整理的這篇文章主要介紹了
MapReduce寻找共同好友
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.測試文件
A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J
2.方法
2-1.方法一:
1.將域用戶和好友分別作為值和鍵輸出{B,C,D,F,E,O}:A{A,C,E,K}:B2.可以看出:B,C,D,F,E,O都有共同好友A,3.把A的好友兩兩組合作為鍵,A作為值,冒泡輸出4.經過shuffle處理后,會把BC作為鍵,共同好友作為值放入集合中5.迭代集合中的好友,一次輸出即可2-2.方法二:
1.將用戶和好友作為鍵和值輸出A:B,C,D,F,E,O --A:B,C,D,F,E,OB:A,C,E,K --B:A,C,E,KC:F,A,D,I --C:A,D,F,ID:A,E,F,L --D:A,E,F,LE:B,C,D,M,L --E:B,C,D,L,M2.將所有鍵值對添加到map集合中3.取map的鍵(所有用戶)為數組4.迭代數組,通過用戶名"A"在map中取得他的好友5.迭代除用戶"A"以外的其他用戶,獲取這些用戶的好友;如果有用戶同時存在于"A"和"B"的好友列表中那么這些好友就是"AB"的共同好友--A:{B,C,D,F,E,O}--B:{A,C,E,K}"A"中存在"C,E"用戶,"B"中也存在"C,E"用戶,那么"C,E"就是AB的共同好友6.將"AB"作為鍵,共同好友作為值輸出即可3.代碼
public class Friends {// mappublic static class MRMapper extends Mapper<LongWritable, Text, Text, Text> {protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String str = value.toString();String friends = str.substring(2);System.out.println(friends);context.write(new Text(str.charAt(0) + ""), new Text(friends));}}// reducepublic static class MRReducer extends Reducer<Text, Text, Text, Text> {private static HashMap<String, String> map1 = new HashMap<String, String>();public void run(Context context) throws IOException, InterruptedException {try {while (context.nextKeyValue()) {reduce(context.getCurrentKey(), context.getValues(), context);}} finally {cleanup(context);}}public void reduce(Text key, Iterable<Text> iterable, Context context)throws IOException, InterruptedException {for (Text t : iterable) {map1.put(key.toString(), t.toString());}}public void cleanup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {List<String> list = new ArrayList<String>();Collection<String> keys = map1.keySet();// 所有用戶String keys1 = keys.toString();String keys2 = keys1.substring(1, keys1.length() - 1);String[] split = keys2.split(",");for (int i = 1; i < split.length; i++) {//迭代用戶String a = split[i].trim();for (int j = (i+1); j < split.length; j++) {//迭代除外層循環以外的用戶String b = split[j].trim();String a_and_b = "";// a的好友String af = map1.get(a);String[] friends = af.split(",");for (String s : friends) {//比較兩個用戶的好友列表,取共同好友if (map1.get(b).contains(s)) {a_and_b += "," + s;}}System.out.println(a + "," + b + " 共同好友 " + a_and_b);if (a_and_b.length() > 1) {list.add(a + "," + b + " 共同好友 :" + a_and_b.substring(1));}}}for(String s:list){context.write(new Text(""), new Text(s));}}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new Configuration();Job job = Job.getInstance(conf);job.setJarByClass(Friends.class);job.setMapperClass(MRMapper.class);job.setReducerClass(MRReducer.class);job.setCombinerClass(MRReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileInputFormat.setInputPaths(job, new Path("hdfs://hadoop5:9000/input/friends.txt"));FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop5:9000/output/friends"));System.out.println(job.waitForCompletion(true) ? 1 : 0);} }如果有更簡潔的方法,歡迎留言給博主。
轉載于:https://www.cnblogs.com/lyjing/p/7571002.html
總結
以上是生活随笔為你收集整理的MapReduce寻找共同好友的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GoldenGate单向复制配置(支持D
- 下一篇: 【贪心】【高精度】zoj3987 Num