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

代码如下:

package com.pro.bq;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.fs.Path; public class AverageScore {
public static class MapAvg extends Mapper<Object, Text, Text, IntWritable>
{ public void map(Object key, Text value,Context context)
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);
//            }
String line=value.toString();
StringTokenizer tokenizer=new StringTokenizer(line,"\n");
while(tokenizer.hasMoreElements())
{
StringTokenizer token=new StringTokenizer(tokenizer.nextToken());
Text name=new Text(token.nextToken());
IntWritable score=new IntWritable(Integer.parseInt(token.nextToken()));
context.write(name,score);
}
}
}
public static class ReduceAvg extends Reducer<Text, IntWritable, Text, IntWritable>
{ public void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
int sum=0;
int cnt=0;
for(IntWritable val:values)
{
sum+=val.get();
cnt++;
}
sum=(Integer)sum/cnt;
context.write(key, new IntWritable(sum));
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf=new Configuration();
String[] hdfsPath=new String[]{"hdfs://localhost:9000/user/haduser/input/averageTest/","hdfs://localhost:9000/user/haduser/output/outAvgScore/"};
String[] otherArgs=new GenericOptionsParser(conf, hdfsPath).getRemainingArgs(); if(otherArgs.length!=2)
{
System.err.println("<in> <out>!!");
System.exit(2);
}
Job job=new Job();
job.setJarByClass(AverageScore.class); job.setMapperClass(MapAvg.class);
job.setReducerClass(ReduceAvg.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true)?0:1); } }
file1:
zhangsan
lisi
wangwu
zhaoliu file2:
张三
李四
王五
赵六 file3:
zhangsan
lisi
wangwu
zhaoliu file4:
李四
张三
王五
赵六

结果如下:

lisi    38
wangwu 49
zhangsan 27
zhaoliu 60
张三 2
李四 1
王五 2
赵六 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. [ios]ipad下的splitViewController 让你的APP看起来酷酷的!

    在ipad下可以使用splitViewController splitViewController下包含两个viewController 这是一种将屏幕一分为二的方式. 在水平状态下会出现成两个左右两 ...

  2. Pomodairo,番茄工作法-解刨篇

    处于“信息大爆炸”的 e 时代的我们每天必定要处理很多的事情,不管是工作.学习.生活......面对这么多的纷杂的事物我们将如何应对?如何做到有条不紊的进行?高效.轻松.愉快的完成它呢?这时一款精致的 ...

  3. 转载 -- C# 中的委托和事件

    原文地址:http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx C# 中的委托和事件 引言 委 ...

  4. 初见IOS的UI之:UI控件的属性frame bounds center 和transform

    这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...

  5. Error:No marked region found along edge. - Found along top edge.

    android开发的时候,初次使用.9图片出现以下: Error:No marked region found along edge. - Found along top edge. 原因: 如图:上 ...

  6. 关于iOS中的文本操作-管理text fields 和 text views

    Managing Text Fields and Text Views 管理UITextField和UITextView实例 UITextField和UITextView的实例拥有两个最主要的功能:展 ...

  7. 01-03-02-2【Nhibernate (版本3.3.1.4000) 出入江湖】CRUP操作-Save方法的一些问题

    此文由于当时不知道NHibernate的Sava方法不是更新操作,不知道Save就是Add,造成如下荒唐的求证过程,但结论是对的 ,可报废此文,特此声明. NHibernate--Save方法: Cu ...

  8. StringTokenizer用法

    import java.util.StringTokenizer; public class StringTokenizerTest { public static void main(String[ ...

  9. **【ci框架】精通CodeIgniter框架

    http://blog.csdn.net/yanhui_wei/article/details/25803945 一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架 ...

  10. **使用 Git Hook 实现网站的自动部署

    http://www.tuicool.com/articles/3QRB7jU 自动化能解放人类的双手,而且更重要的是,因为按照规定的流程来走,也减少了很多误操作的产生.不知道大家平时都是怎么样更新自 ...