日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

有趣的面试题

發布時間:2023/11/29 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有趣的面试题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

MR找共同朋友,數據格式如下:

?

A B C D E F B A C D E C A B E D A B E E A B C D F A

?

第一字母表示本人,其他是他的朋友,找出有共同朋友的人,和共同朋友是誰

?

1 import java.io.IOException; 2 import java.util.Set; 3 import java.util.StringTokenizer; 4 import java.util.TreeSet; 5 6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.fs.Path; 8 import org.apache.hadoop.io.Text; 9 import org.apache.hadoop.mapreduce.Job; 10 import org.apache.hadoop.mapreduce.Mapper; 11 import org.apache.hadoop.mapreduce.Reducer; 12 import org.apache.hadoop.mapreduce.Mapper.Context; 13 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 14 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 15 import org.apache.hadoop.util.GenericOptionsParser; 16 17 public class FindFriend { 18 19 public static class ChangeMapper extends Mapper<Object, Text, Text, Text>{ 20 @Override 21 public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 22 StringTokenizer itr = new StringTokenizer(value.toString()); 23 Text owner = new Text(); 24 Set<String> set = new TreeSet<String>(); 25 owner.set(itr.nextToken()); 26 while (itr.hasMoreTokens()) { 27 set.add(itr.nextToken()); 28 } 29 String[] friends = new String[set.size()]; 30 friends = set.toArray(friends); 31 32 for(int i=0;i<friends.length;i++){ 33 for(int j=i+1;j<friends.length;j++){ 34 String outputkey = friends[i]+friends[j]; 35 context.write(new Text(outputkey),owner); 36 } 37 } 38 } 39 } 40 41 public static class FindReducer extends Reducer<Text,Text,Text,Text> { 42 public void reduce(Text key, Iterable<Text> values, 43 Context context) throws IOException, InterruptedException { 44 String commonfriends =""; 45 for (Text val : values) { 46 if(commonfriends == ""){ 47 commonfriends = val.toString(); 48 }else{ 49 commonfriends = commonfriends+":"+val.toString(); 50 } 51 } 52 context.write(key, new Text(commonfriends)); 53 } 54 } 55 56 57 public static void main(String[] args) throws IOException, 58 InterruptedException, ClassNotFoundException { 59 60 Configuration conf = new Configuration(); 61 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 62 if (otherArgs.length < 2) { 63 System.err.println("args error"); 64 System.exit(2); 65 } 66 Job job = new Job(conf, "word count"); 67 job.setJarByClass(FindFriend.class); 68 job.setMapperClass(ChangeMapper.class); 69 job.setCombinerClass(FindReducer.class); 70 job.setReducerClass(FindReducer.class); 71 job.setOutputKeyClass(Text.class); 72 job.setOutputValueClass(Text.class); 73 for (int i = 0; i < otherArgs.length - 1; ++i) { 74 FileInputFormat.addInputPath(job, new Path(otherArgs[i])); 75 } 76 FileOutputFormat.setOutputPath(job, 77 new Path(otherArgs[otherArgs.length - 1])); 78 System.exit(job.waitForCompletion(true) ? 0 : 1); 79 80 } 81 82 }

運行結果:

?

AB E:C:D AC E:B AD B:E AE C:B:D BC A:E BD A:E BE C:D:A BF A CD E:A:B CE A:B CF A DE B:A DF A EF A

?


轉自:http://www.aboutyun.com/thread-11107-1-1.html


?

轉載于:https://www.cnblogs.com/admln/p/Interesting-interview-topic.html

總結

以上是生活随笔為你收集整理的有趣的面试题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。