今天写了一个用mapreduce求平均分的程序,结果是出来了,可是没有按照“学生名字”进行排序,如果是英文名字的话,结果是排好序的。

代码如下:

  1. package com.pro.bq;
  2.  
  3. import java.io.IOException;
  4. import java.util.StringTokenizer;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.io.IntWritable;
  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.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  16. import org.apache.hadoop.util.GenericOptionsParser;
  17. import org.apache.hadoop.fs.Path;
  18.  
  19. public class AverageScore {
  20. public static class MapAvg extends Mapper<Object, Text, Text, IntWritable>
  21. {
  22.  
  23. public void map(Object key, Text value,Context context)
  24. throws IOException, InterruptedException {
    //            String[] lineData=value.toString().split(" ");//split中间如果有很多“ ”的话lineData的长度增加,灵活性差
    //            if(lineData.length==2)
    //            {        
    //                name.set(lineData[0]);
    //                score.set(Integer.parseInt(lineData[1]));
    //                context.write(name,score);
    //            }
  25. String line=value.toString();
  26. StringTokenizer tokenizer=new StringTokenizer(line,"\n");
  27. while(tokenizer.hasMoreElements())
  28. {
  29. StringTokenizer token=new StringTokenizer(tokenizer.nextToken());
  30. Text name=new Text(token.nextToken());
  31. IntWritable score=new IntWritable(Integer.parseInt(token.nextToken()));
  32. context.write(name,score);
  33. }
  34. }
  35. }
  36. public static class ReduceAvg extends Reducer<Text, IntWritable, Text, IntWritable>
  37. {
  38.  
  39. public void reduce(Text key, Iterable<IntWritable> values,Context context)
  40. throws IOException, InterruptedException {
  41. // TODO Auto-generated method stub
  42. int sum=0;
  43. int cnt=0;
  44. for(IntWritable val:values)
  45. {
  46. sum+=val.get();
  47. cnt++;
  48. }
  49. sum=(Integer)sum/cnt;
  50. context.write(key, new IntWritable(sum));
  51. }
  52. }
  53.  
  54. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  55. Configuration conf=new Configuration();
  56. String[] hdfsPath=new String[]{"hdfs://localhost:9000/user/haduser/input/averageTest/","hdfs://localhost:9000/user/haduser/output/outAvgScore/"};
  57. String[] otherArgs=new GenericOptionsParser(conf, hdfsPath).getRemainingArgs();
  58.  
  59. if(otherArgs.length!=2)
  60. {
  61. System.err.println("<in> <out>!!");
  62. System.exit(2);
  63. }
  64. Job job=new Job();
  65. job.setJarByClass(AverageScore.class);
  66.  
  67. job.setMapperClass(MapAvg.class);
  68. job.setReducerClass(ReduceAvg.class);
  69.  
  70. job.setOutputKeyClass(Text.class);
  71. job.setOutputValueClass(IntWritable.class);
  72.  
  73. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  74. FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
  75. System.exit(job.waitForCompletion(true)?0:1);
  76.  
  77. }
  78.  
  79. }
  1. file1:
  2. zhangsan
  3. lisi
  4. wangwu
  5. zhaoliu
  6.  
  7. file2:
  8. 张三
  9. 李四
  10. 王五
  11. 赵六
  12.  
  13. file3:
  14. zhangsan
  15. lisi
  16. wangwu
  17. zhaoliu
  18.  
  19. file4:
  20. 李四
  21. 张三
  22. 王五
  23. 赵六

结果如下:

  1. lisi 38
  2. wangwu 49
  3. zhangsan 27
  4. zhaoliu 60
  5. 张三 2
  6. 李四 1
  7. 王五 2
  8. 赵六 3

难道不支持中文的排序??以后学会自己写Partitioner后是不是可以自己写排序的程序??以后解决...

MapReduce 中的Map后,sort不能对中文的key排序的更多相关文章

  1. MapReduce中的Shuffle和Sort分析

    MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme ...

  2. Hadoop : MapReduce中的Shuffle和Sort分析

    地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...

  3. MapReduce中的map个数

    在map阶段读取数据前,FileInputFormat会将输入文件分割成split.split的个数决定了map的个数.影响map个数(split个数)的主要因素有: 1) 文件的大小.当块(dfs. ...

  4. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  5. Hadoop框架下MapReduce中的map个数如何控制

    控制map个数的核心源码 long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job)); //getFormatMinS ...

  6. list中依据map&lt;String,Object&gt;的某个值排序

    private void sort(List<Map<String, Object>> list) { Collections.sort(list, new Comparato ...

  7. MapReduce中combine、partition、shuffle的作用是什么

    http://www.aboutyun.com/thread-8927-1-1.html Mapreduce在hadoop中是一个比較难以的概念.以下须要用心看,然后自己就能总结出来了. 概括: co ...

  8. Java Map 键值对排序 按key排序和按Value排序

    一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...

  9. mapreduce 中 map数量与文件大小的关系

    学习mapreduce过程中, map第一个阶段是从hdfs 中获取文件的并进行切片,我自己在好奇map的启动的数量和文件的大小有什么关系,进过学习得知map的数量和文件切片的数量有关系,那文件的大小 ...

随机推荐

  1. per-project basis

    Of course, HSQLDB connection parameters should be stored on a per-project basis, instead of only onc ...

  2. 分析 "End" "Unload Me" "Exit Sub" 之间的区别与联系

    之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End”  跟  “Unload  Me”  在敲程序 的时候经常敲到,“exit  sub”  更是熟悉,下面,解析: End  ...

  3. How to create jar for Android Library Project

    http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project This works ...

  4. MySQL、SqlServer、Oracle三大主流数据库分页查询

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法.可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应用 ...

  5. 使用GitHub建立自己的个人主页

    1.建仓库 在自己的库里建一个hujun123qwe.github.io的库 即可以使用这个名字当网址访问. 2.写内容 在库里建一个首页文件 index.html 这个个人主页只支持静态的内容,像p ...

  6. 2565: 最长双回文串 - BZOJ

    Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同). 输入长度为n的串S,求S的最长双回文子串T ...

  7. Hibernate各种主键生成策略与配置详解【附1--<generator class="foreign">】

    1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据库都无关,可以跨数据库.在存储对象前,必须要使用主 ...

  8. trie树(前缀树)

    问题描述:   Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  9. DX SetFVF

    自由顶点格式(flexible vertex format,FVF) http://www.cnblogs.com/xmzyl/articles/1604096.html if( SUCCEEDED( ...

  10. sql中临时表的创建和使用【本文转自多人博客】

    本模块原网址:http://www.cnblogs.com/jeffwongishandsome/archive/2009/08/05/1526466.html 原作者:Jeff Wong 1.创建方 ...