人民大学云计算编程的网上评估平台--解题报告 1001-1003

这几天忙着找实习,所以日志耽搁了,现在来补起~~。

相信很多人都知道  PKU Online Judge, 现在中国人民大学也提供了一个类似的平台,但与北京在线评判系统不一样的是,中国人民大学的这个系统是专门评判mapreduce编程题的。

我把链接发出来,大家可以去试着做看看: http://cloudcomputing.ruc.edu.cn/index.jsp

大家在做题前,先看看“常见问题”根据系统要求的格式来写程序。不然不能正常运行。(我就是直接运行错了3次。 - -!)

可以看到这个平台的题目还不多,现在只有1000-1009,其中1008-1009的题目还没发出来。所以我们讨论1000-1007.

如果你想先自己测试下,下面的文章就可以先不忙看。等你解决其中的题,可以再来看这篇文章,大家可以共同提高。

1000 比较简单,用hadoop自带的例子都可以解决,我这里就不多说了。

1001 题目:

a+b per line

描述

有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入。表格如下:
name  Dec   Jan($)
CM    200   314
LY    2000  332
QQM   6000  333
ZYM   5000  333
BP    30    12 

你需要知道每个人这三个月的收入总和,那么你就需要将表格中一行代表收入的数字相加.下面请编写程序解决这个问题。

输入

输入只包含一个文件,文件中有一个表格,它的结构如下:
1 200   314
2 2000  332
3 6000  333
4 5000  333
5 30    12   
其中每行最前面的数字是行标

输出

输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的和。如下:
1 514
2 2332
3 6333
4 5333
5 42

输入样例

input:
1 200   314
2 2000  332
3 6000  333
4 6000  333
5 5000  333
6 30    12 

输出样例:

1 514
2 2332
3 6333
4 6333
5 5333
6 42

注意:
1 输入文件和输出文件都只有一个;
2 输入和输出文件每行的第一个数字都是行标;
3 每个数据都是正整数或者零.。

1001 解题思路:

1001的题目其实是很简单的,将读入的每一行用空格分隔,第一个域就是行号作为key、再将第二个域和第三个域相加作为value.

因为map阶段会根据key值自动排序,我们就不用操心了。至于key的排序顺序,我们以后讨论。

现在上代码:

[java]  view plain copy
  1. public class MyMapre {  
  2. public static  class wordcountMapper extends  
  3. Mapper{  
  4. public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
  5. Integer sum = 0;  
  6. String line = value.toString();  
  7. StringTokenizer itr = new StringTokenizer(line);//分割  
  8. if (itr.hasMoreElements())  
  9. key = new LongWritable(Integer.parseInt(itr.nextToken()));  //获取第一个域的值  
  10. while(itr.hasMoreElements()){  
  11. sum += Integer.parseInt(itr.nextToken()); //求和剩下的值  
  12. }  
  13. context.write(key, new IntWritable(sum));  
  14. }  
  15. }  
  16. public static  void main(String args[])throws Exception{  
  17.   
  18. Configuration conf = new Configuration();  
  19.   
  20. Job job = new Job(conf, "MyMapre");  
  21.   
  22. job.setJarByClass(MyMapre.class);  
  23.   
  24. job.setMapOutputKeyClass(LongWritable.class);  
  25. job.setMapOutputValueClass(IntWritable.class);  
  26.   
  27. job.setOutputKeyClass(LongWritable.class);  
  28. job.setOutputValueClass(IntWritable.class);  
  29.   
  30. job.setMapperClass(wordcountMapper.class);  
  31.   
  32. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  33. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  34.   
  35. job.waitForCompletion(true);  
  36. }  
  37. }  

1002 题目:

Sort

描述

你的程序需要读入输入数据文件,然后再将数据按升序排序后输出。在输入文件中,每一行都代表一个数据。

输入

输入是一组文本文件,在文本文件中每一行都是一个元数据,而且每个数据是用一个数字串代表待排序的数字。

输出

输出文件中每一行第一个数字是行标,后面一个数字是排好序的原始输入数据,注意排序顺序是从小到大升序排序。

输入样例

input1:

2
32
654
32
15
756
65223

input2:

5956
22
650
92

input3:

26
54
6

输出样例:
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223

1002 解题思路:

在上一题已经说过在map阶段会对key自动排序, 所以我们读入一行后(元数据),将其作为key,传递给reduce。我们可以看到最后输出的样例,还需要打印出行号。所以我们在reduce外面定义一个int 来记录总的行数(作为key输出)。而将map阶段传来的key作为reduce阶段的value输出。

上代码吧:

[java]  view plain copy
  1. public class MyMapre {  
  2. public static  class wordcountMapper extends  
  3. Mapper{  
  4. public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
  5. String one = value.toString();  
  6. context.write(new LongWritable(Integer.parseInt(one)) , key);  
  7. }  
  8. }  
  9. public static  class wordcountReduce extends  
  10. Reducer{  
  11. int sum = 0;  
  12. public void reduce(LongWritable key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  13. sum++;  
  14. context.write(new LongWritable(sum), key);  
  15. }  
  16. }  
  17. public static  void main(String args[])throws Exception{  
  18.   
  19. Configuration conf = new Configuration();  
  20.   
  21. Job job = new Job(conf, "Sort");  
  22.   
  23. job.setJarByClass(MyMapre.class);  
  24.   
  25. job.setOutputKeyClass(LongWritable.class);  
  26. job.setOutputValueClass(LongWritable.class);  
  27.   
  28. job.setMapOutputKeyClass(LongWritable.class);  
  29. job.setMapOutputValueClass(LongWritable.class);  
  30.   
  31. job.setMapperClass(wordcountMapper.class);  
  32. job.setReducerClass(wordcountReduce.class);  
  33.   
  34. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  35. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  36.   
  37. job.waitForCompletion(true);  
  38. }  
  39. }  

1003 题目:

Data deduplication

描述

你的程序要求读入输入文件,在去掉所有数据中的重复数据后输出结果。在输入文件中每一行是一个元数据。

输入

输入是一组文本文件,在每个输入文件中每一行是一个数据。每一个元数据都是一个字符串。

输出文件

输出文件的每一行都是在输入文件中出现过的一个数据,并且输出文件中的每一行都不相同。

输入样例

input1:
2006-6-9 a
2006-6-10 b
2006-6-11 c
2006-6-12 d
2006-6-13 a
2006-6-14 b
2006-6-15 c
2006-6-11 c
input2:
2006-6-9 b
2006-6-10 a
2006-6-11 b
2006-6-12 d
2006-6-13 a
2006-6-14 c
2006-6-15 d
2006-6-11 c

输出样例:
2006-6-10 a 
2006-6-10 b 
2006-6-11 b 
2006-6-11 c 
2006-6-12 d 
2006-6-13 a 
2006-6-14 b 
2006-6-14 c 
2006-6-15 c 
2006-6-15 d 
2006-6-9 a 
2006-6-9 b

注意:
1 输出结果是按照字典顺序排序的;
2 每一行都是一个元数据;
3 重复数据在输出文件中也要输出一次。
1003 解题思路:

首先还是将一行进行划分,将第一个域作为map阶段的key输出。第二个域作为map阶段的value输出。

reduce收到key-value对后,key相同时,会返回多个value。根据题意要求,value中出现的字母不能重复,所以我们要消掉重复的字母、而且最后需要排序,我们可以调用java自带的排序函数来实现。

上代码了:

[java]  view plain copy
  1. public class MyMapre {  
  2. public static  class wordcountMapper extends  
  3. Mapper{  
  4. public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
  5. String line = value.toString();  
  6. Text word = new Text();  
  7. Text one = new Text();  
  8. StringTokenizer itr = new StringTokenizer(line);//划分  
  9. if (itr.hasMoreElements()) word.set(itr.nextToken());  
  10. if (itr.hasMoreElements()) one.set(itr.nextToken());  
  11. //获取两个域的值  
  12. context.write(word, one);  
  13. }  
  14. }  
  15. public static  class wordcountReduce extends  
  16. Reducer{  
  17.   
  18. public void reduce(Text key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  19. String pre = ""//消除重复字母的变量  
  20. List list = new ArrayList(); //进行排序前存储的list  
  21. for (Text str : values){  
  22. if (!str.toString().equals(pre)) {  //如果不相等者更新pre变量  
  23. pre = str.toString();   
  24. list.add(pre);  //向list中添加不重复的元素  
  25. }   
  26. }  
  27. Collections.sort(list);  //排序  
  28. for (int i = 0; i < list.size(); i++)  
  29. context.write(key, new Text(list.get(i)));  //一次性输出  
  30. }  
  31. }  
  32.   
  33.   
  34. public static  void main(String args[])throws Exception{  
  35.   
  36. Configuration conf = new Configuration();  
  37.   
  38. Job job = new Job(conf, "deduplication");  
  39.   
  40. job.setJarByClass(MyMapre.class);  
  41.   
  42. job.setOutputKeyClass(Text.class);  
  43. job.setOutputValueClass(Text.class);  
  44.   
  45. job.setMapOutputKeyClass(Text.class);  
  46. job.setMapOutputValueClass(Text.class);  
  47.   
  48. job.setMapperClass(wordcountMapper.class);  
  49. job.setReducerClass(wordcountReduce.class);  
  50.   
  51. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  52. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  53.   
  54. job.waitForCompletion(true);  
  55. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值