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

来源:互联网 发布:淘宝神笔使用教程视频 编辑:程序博客网 时间:2024/04/28 17:25

因为一次写7道题,文章太长了,为了方便大家阅读,我分成了两篇。

接着上一篇文章,我们继续mapreduce编程之旅~~

 

1004: 题目

 

Single Table Join

描述

输入文件是一个包含有子女-父母表的文件。请编写一个程序,输入为此输入文件,输出是包含在子女-父母表中的孙子女-祖父母关系表。

输入

输入是包含有子女-父母表的一个文件

输出

输出是包含有孙子女-祖父母关系的一个文件,孙子女-祖父母关系是从子女-父母表中得出的。

样例输入

child parent
Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Mary
Lucy Ben
Jack Alice
Jack Jesse
Terry Alice
Terry Jesse
Philip Terry
Philip Alma
Mark Terry
Mark Alma

样例输出

grandchild  grandparent 
Jone        Alice 
Jone        Jesse 
Tom         Alice 
Tom         Jesse 
Jone        Mary 
Jone        Ben 
Tom         Mary 
Tom         Ben 
Mark        Jesse 
Mark        Alice 
Philip      Jesse 
Philip      Alice


1004:解题思路

单表的连接,这个比较有味道~~当然有可能是我水平有问题,所以写的比较复杂。

首先,我定义了一个自定义数据类型TextPair 关于自定义数据类型我这里也不多说了,大家可以百度一下,或者看看hadoop权威指南上面都会讲解。

接着:我们从输入可以看出,孩子和双亲都写在同一个文件,而我们要求的是祖孙关系,所以双亲类也会出现在孩子列。为了正确区分,所以我们借助自定义数据类型来完成。

我先上代码,在代码中我会详细注释:

[java] view plaincopy
  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 key1 = "";  
  6. String value1 = "";  
  7. StringTokenizer itr = new StringTokenizer(value.toString());  
  8. //从读入得行中 取出 孩子、双亲  
  9. if (itr.hasMoreElements()){  
  10. key1 = itr.nextToken();  
  11. }  
  12. if (itr.hasMoreElements()){  
  13. value1 = itr.nextToken();  
  14. }  
  15. //使用自定义的数据类型,作为key-value  
  16. //0-孩子, 1-代表双亲  
  17. //我这里将孩子和双亲进了交换输出,方便reduce进行 孩子-祖父的配对  
  18. context.write(new TextPair(key1, 0), new TextPair(value1, 1));  
  19. context.write(new TextPair(value1, 1), new TextPair(key1, 0));  
  20. }  
  21. }  
  22. public static  class wordcountReduce extends  
  23. Reducer{  
  24. public void reduce(TextPair key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  25. //上面定义了两个list,保存孩子和双亲  
  26. List child = new ArrayList();  
  27. List parent = new ArrayList();  
  28. for (TextPair str : values){  
  29. //通过比对 0 或者 1 就可以直接是孩子还是双亲  
  30. //具有同一个key值,表示这是双亲,而与双亲有关系的就是孩子和双亲的双亲,所以通过判断就是可以孩子和祖父  
  31. if (str.second.get() == 0){  
  32. child.add(str.first.toString());  
  33. }  
  34. else{  
  35. parent.add(str.first.toString());  
  36. }  
  37. }  
  38. if (child.size() != 0 && parent.size() != 0){  
  39. //一个孩子可能对应多个祖父、所以采用了双重循环,孩子作为外层循环  
  40. for (int i = 0; i < child.size(); i++){  
  41. for (int j = 0; j < parent.size(); j++){  
  42. context.write(new Text(child.get(i)), new Text(parent.get(j)));  
  43. }  
  44. }  
  45. }  
  46. }  
  47. }  
  48. //自定义数据类型,这个我就不多说了。  
  49. public static class TextPair implements WritableComparable {  
  50. private Text first;  
  51. private IntWritable second;  
  52. public TextPair() {  
  53. set(new Text(), new IntWritable());  
  54. }  
  55. public TextPair(String first, int second) {  
  56. set(new Text(first), new IntWritable(second));  
  57. }  
  58. public TextPair(Text first, IntWritable second) {  
  59. set(first, second);  
  60. }  
  61. public void set(Text first, IntWritable second) {  
  62. this.first = first;  
  63. this.second = second;  
  64. }  
  65. public Text getFirst() {  
  66. return first;  
  67. }  
  68. public String toString() {  
  69. return (first.toString());  
  70. }  
  71. public IntWritable getSecond() {  
  72. return second;  
  73. }  
  74. public void write(DataOutput out) throws IOException {  
  75. first.write(out);  
  76. second.write(out);  
  77. }  
  78. public void readFields(DataInput in) throws IOException {  
  79. first.readFields(in);  
  80. second.readFields(in);  
  81. }  
  82. public int compareTo(TextPair tp) {  
  83. //注意这里排序时,只对first排序,不对进行判断的0、1进行排序  
  84. int cmp = first.compareTo(tp.first);  
  85. return cmp;  
  86. }  
  87. }  
  88. public static  void main(String args[])throws Exception{  
  89. Configuration conf = new Configuration();  
  90. Job job = new Job(conf, "SingleJoin");  
  91. job.setJarByClass(MyMapre.class);  
  92. job.setMapOutputKeyClass(TextPair.class);  
  93. job.setMapOutputValueClass(TextPair.class);  
  94. job.setOutputKeyClass(Text.class);  
  95. job.setOutputValueClass(Text.class);  
  96. job.setMapperClass(wordcountMapper.class);  
  97. job.setReducerClass(wordcountReduce.class);  
  98. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  99. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  100. job.waitForCompletion(true);  
  101. }  
  102. }  

1005: 题目

Multi-table Join

描述

输入有两个文件,一个名为factory的输入文件包含描述工厂名和其对应地址ID的表,另一个名为address的输入文件包含描述地址名和其ID的表格。请编写一个程序输出工厂名和其对应地址的名字。

输入

输入有两个文件,第一个描述了工厂名和对应地址的ID,第二个输入文件描述了地址名和其ID。

输出

输出是一个包含工厂名和其对应地名的文件。

输入样例

input: 
factory:
factoryname addressID
Beijing Red Star 1
Shenzhen Thunder 3
Guangzhou Honda 2
Beijing Rising 1
Guangzhou Development Bank 2
Tencent 3
Bank of Beijing 1
address:
addressID addressname
1 Beijing
2 Guangzhou
3 Shenzhen
4 Xian

输出样例

output:
factoryname  addressname
Bank of Beijing Beijing
Beijing Red Star Beijing 
Beijing Rising Beijing 
Guangzhou Development Bank Guangzhou 
Guangzhou Honda Guangzhou
Shenzhen Thunder Shenzhen 
Tencent Shenzhen

1005解题思路:

这题跟1004的思路都差不多,能做出1004,那么1005也就不在话下了。

我们已经使用1004的自定义数据类型TextPair ,因为我们从一个文件中读入得数据分为两类,所以使用TextPair 对其进行区分。

还是上代码吧,我在代码里详细注释:

[java] view plaincopy
  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. //这里比较特殊,因为一个工厂名中包含了空格,所以我们要正确分割就要注意了。  
  6. String str = "";  
  7. String id = "";  
  8. String value1 = "";  
  9. //分割  
  10. StringTokenizer itr = new StringTokenizer(value.toString());  
  11. while (itr.hasMoreElements()){  
  12. str = itr.nextToken();  
  13. //如果第一个域不包含了0-9就证明是factory文件的内容  
  14. if (!str.matches("[0-9]")){  
  15. value1 += str;  //包含多个str  
  16. value1 += " ";  
  17. }else//否则是address文件的内容  
  18. id = str;  //第一个域就是Id  
  19. //如果value1不为空则是factor,已经分解完全 factor-1  
  20. if (!value1.isEmpty()) {   
  21. context.write(new Text(id), new TextPair(value1, 1));  
  22. return;  
  23. }   
  24. }  
  25. }  
  26. //如果前面都没return 那么就是address文件的内容 adress-0  
  27. context.write(new Text(id), new TextPair(value1, 0)); }  
  28. }  
  29. public static  class wordcountReduce extends  
  30. Reducer{  
  31. public void reduce(Text key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  32. //依旧定义两个list来保存。  
  33. List factor = new ArrayList();  
  34. List address = new ArrayList();  
  35. for (TextPair str : values){  
  36. //1-factor  
  37. if (str.second.get() == 1){  
  38. factor.add(str.first.toString());  
  39. }  
  40. else{  
  41. //0-adress  
  42. address.add(str.first.toString());  
  43. }  
  44. }  
  45. //因为一个地方可能对应多个工厂,所以将adress作为外层循环  
  46. if (factor.size() != 0 && address.size() != 0){  
  47. for (int i = 0; i < address.size(); i++){  
  48. for (int j = 0; j < factor.size(); j++){  
  49. context.write(new Text(factor.get(j)), new Text(address.get(i)));  
  50. }  
  51. }  
  52. }  
  53. }  
  54. }  
  55. //自定义数据类型,不多说了。  
  56. public static class TextPair implements WritableComparable {  
  57. private Text first;  
  58. private IntWritable second;  
  59. public TextPair() {  
  60. set(new Text(), new IntWritable());  
  61. }  
  62. public TextPair(String first, int second) {  
  63. set(new Text(first), new IntWritable(second));  
  64. }  
  65. public TextPair(Text first, IntWritable second) {  
  66. set(first, second);  
  67. }  
  68. public void set(Text first, IntWritable second) {  
  69. this.first = first;  
  70. this.second = second;  
  71. }  
  72. public Text getFirst() {  
  73. return first;  
  74. }  
  75. public String toString() {  
  76. return (first.toString());  
  77. }  
  78. public IntWritable getSecond() {  
  79. return second;  
  80. }  
  81. public void write(DataOutput out) throws IOException {  
  82. first.write(out);  
  83. second.write(out);  
  84. }  
  85. public void readFields(DataInput in) throws IOException {  
  86. first.readFields(in);  
  87. second.readFields(in);  
  88. }  
  89. public int compareTo(TextPair tp) {  
  90. int cmp = first.compareTo(tp.first);  
  91. return cmp;  
  92. }  
  93. }  
  94. public static  void main(String args[])throws Exception{  
  95. Configuration conf = new Configuration();  
  96. Job job = new Job(conf, "MultiTableJoin");  
  97. job.setJarByClass(MyMapre.class);  
  98. job.setMapOutputKeyClass(Text.class);  
  99. job.setMapOutputValueClass(TextPair.class);  
  100. job.setOutputKeyClass(Text.class);  
  101. job.setOutputValueClass(Text.class);  
  102. job.setMapperClass(wordcountMapper.class);  
  103. job.setReducerClass(wordcountReduce.class);  
  104. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  105. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  106. job.waitForCompletion(true);  
  107. }  
  108. }  

1006: 题目

Sum

描述

输入文件是一组文本文件,每个输入文件中都包含很多行,每行都是一个数字字符串,代表了一个特别大的数字。需要注意的是这个数字的低位在字符串的开头,高位在字符串的结尾。请编写一个程序求包含在输入文件中的所有数字的和并输出。

输入

输入有很多文件组成,每个文件都有很多行,每行都由一个数字字符串代表一个数字。

输出

输出时一个文件,这个文件中第一行的第一个数字是行标,第二个数字式输入文件中所有数字的和。

输入样例

input: 
file1:
1235546665312
112344569882
326434546462
21346546846
file2:
3654354655
3215456463
21235465463
321265465
65465463
32
file3:
31654
654564564
3541231564
351646846
3164646
3163

输出样例

output:
1 8685932816082

注意:
1 只有一个输出文件;
2 输出文件的第一行由行标"1"和所有数字的和组成;
3 每个数字都是正整数或者零。每个数字都超过50位,所以常用数据类型是无法存储的;
4 数字的低位在数字字符串的左侧,高位在数字字符串的右侧。比如样例输入第一个输入文件的第一行代表的数字是2135666455321。

1006解题思路:1006主要解决两个问题,一:大数加法。 二:将所有数据归一

第一个问题是常规解法,我不多说。 第二,因为我们最后需要求出一个总结果,所以就需要将所有的key归成一个group。当然我们可以自定义group的划分,这个可以参考hadoop权威指南,以后如果有需要,我会写出来的。我这里用了一个简单解决办法。(能用简单的办法,当然用简单的办法)

我结合代码给大家讲解吧:

[java] view plaincopy
  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. //注意这里的key,这就是我所谓的简单办法,用同一个key,那么在reduce阶段就可以加所有数据归到一个group  
  6. context.write(new LongWritable(1), value);  
  7. }  
  8. }  
  9. public static  class wordcountReduce extends  
  10. Reducer{  
  11. String tem = "0"//因为是大数,所以要string来存储  
  12. public void reduce(LongWritable key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  13. for (Text str : values){  
  14. //获取大数,调用Sum()大数加法函数  
  15. tem = Sum(tem, str.toString());  
  16. }  
  17. context.write(key, new Text(tem));  
  18. }  
  19. }  
  20. //这是我实现的大数加法函数,其实我作了很久心理斗争,因为这个函数写的实在不怎么样,大家可以自己实现,不要看我这个坏例子。呵呵~~ 这个函数我就不写注释了。  
  21. public  static String  Sum(String a, String b){  
  22. String c = "";  
  23. int a_len = a.length();  
  24. int b_len = b.length();  
  25. int jin = 0;  
  26. int a_first;  
  27. int b_first;  
  28. int temp;  
  29. while (a_len  > 0 && b_len  > 0){  
  30. a_first = Integer.parseInt(a.substring(01));  
  31. b_first = Integer.parseInt(b.substring(01));  
  32. a = a.substring(1);  
  33. b = b.substring(1);  
  34. temp= a_first + b_first +jin;  
  35. jin = temp/ 10;  
  36. temp= temp- 10 * jin;  
  37. c += temp;  
  38. a_len--;  
  39. b_len--;  
  40. }  
  41. if (a_len == 0 && b_len == 0 && jin != 0)  
  42. c += jin;  
  43. while (a_len > 0){  
  44. int k = Integer.parseInt(a.substring(01)) + jin;  
  45. a = a.substring(1);  
  46. c += k;  
  47. a_len--;  
  48. jin = 0;  
  49. }  
  50. while (b_len > 0){  
  51. int k = Integer.parseInt(b.substring(01)) + jin;  
  52. b = b.substring(1);  
  53. c += k;  
  54. b_len --;  
  55. jin = 0;  
  56. }  
  57. return c;  
  58. }   
  59. public static  void main(String args[])throws Exception{  
  60. Configuration conf = new Configuration();  
  61. Job job = new Job(conf, "Sum");  
  62. job.setJarByClass(MyMapre.class);  
  63. job.setMapOutputKeyClass(LongWritable.class);  
  64. job.setMapOutputValueClass(Text.class);  
  65. job.setOutputKeyClass(LongWritable.class);  
  66. job.setOutputValueClass(Text.class);  
  67. job.setMapperClass(wordcountMapper.class);  
  68. job.setReducerClass(wordcountReduce.class);  
  69. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  70. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  71. job.waitForCompletion(true);  
  72. }  
  73. }  

1007: 题目

 

 

WordCount Plus

描述

WordCount例子输入文本文件并计算单词出现的次数。现在有一个WordCount2.0版本,在这个版本中你必须处理含有"/.',"{}[]:;"等等字符的输入文件。在你切词的时候,你应该把"declare," 切成 "declare",同样 "Hello!"应该切成"Hello","can't"应该切成"can't"。

输入

输入是包含很多单词的文本文件。

出入

输出是一个文本文件,这个文件的每一行包含一个单词和这个单词在所有输入文件中出现的次数。在输出文件中单词是按照字典顺序排序的。

输入样例

input1:
hello world, bye world.
input2:
hello hadoop, bye hadoop!
输出样例

bye 2
hadoop 2
hello 2
world 2
1007解题思路:1007主要是对字符的过滤,这里我可以使用正则表达式来过滤。没什么难点~~

我们还是边看代码边说吧:

[java] view plaincopy
  1. public class MyMapre {  
  2. public static  class wordcountMapper extends  
  3. Mapper{  
  4. private final static IntWritable  one = new IntWritable(1);  
  5. private String pattern = "[^//w/']";  //定义正则表达式,过滤除数字、字母、“'” 外的字符  
  6. public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{  
  7. String line = value.toString().toLowerCase();  
  8. //用空格代替要过滤的字符  
  9. line = line.replaceAll(pattern, " ");  
  10. //划分  
  11. StringTokenizer itr = new StringTokenizer(line);  
  12. while(itr.hasMoreElements()){  
  13. context.write(new Text(itr.nextToken()), one);  
  14. }  
  15. }  
  16. }  
  17. public static  class wordcountReduce extends  
  18. Reducer{  
  19. public void reduce(Text key, Iterablevalues, Context context)throws IOException, InterruptedException{  
  20. //这里就比较简单了,跟wordcount一样,我就不多说了。  
  21. int sum = 0;  
  22. for (IntWritable str : values){  
  23. sum += str.get();  
  24. }  
  25. context.write(key, new IntWritable(sum));  
  26. }  
  27. }  
  28. public static  void main(String args[])throws Exception{  
  29. Configuration conf = new Configuration();  
  30. Job job = new Job(conf, "Plus");  
  31. job.setJarByClass(MyMapre.class);  
  32. job.setMapOutputKeyClass(Text.class);  
  33. job.setMapOutputValueClass(IntWritable.class);  
  34. job.setOutputKeyClass(Text.class);  
  35. job.setOutputValueClass(IntWritable.class);  
  36. job.setMapperClass(wordcountMapper.class);  
  37. job.setReducerClass(wordcountReduce.class);  
  38. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  39. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  40. job.waitForCompletion(true);  
  41. }  
  42. }  

 


终于写完了,当然这里写的是我的解题思路,如果各位大大有更好的想法,不妨分享出来,大家一起happy。上面的程序都能正确提交。

当然我不排除我程序中有考虑不周的地方或错误的地方(测试数据的不全面造成)的,如果各位大大能指出,我将不胜感激~~

我最后再说明下,因为程序是我从网站上的提交库直接取回来的,格式不太好看。对不住各位了~~

0 0
原创粉丝点击