MapReduce 中的Map后,sort不能对中文的key排序
今天写了一个用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排序的更多相关文章
- MapReduce中的Shuffle和Sort分析
MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme ...
- Hadoop : MapReduce中的Shuffle和Sort分析
地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...
- MapReduce中的map个数
在map阶段读取数据前,FileInputFormat会将输入文件分割成split.split的个数决定了map的个数.影响map个数(split个数)的主要因素有: 1) 文件的大小.当块(dfs. ...
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- Hadoop框架下MapReduce中的map个数如何控制
控制map个数的核心源码 long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job)); //getFormatMinS ...
- list中依据map<String,Object>的某个值排序
private void sort(List<Map<String, Object>> list) { Collections.sort(list, new Comparato ...
- MapReduce中combine、partition、shuffle的作用是什么
http://www.aboutyun.com/thread-8927-1-1.html Mapreduce在hadoop中是一个比較难以的概念.以下须要用心看,然后自己就能总结出来了. 概括: co ...
- Java Map 键值对排序 按key排序和按Value排序
一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...
- mapreduce 中 map数量与文件大小的关系
学习mapreduce过程中, map第一个阶段是从hdfs 中获取文件的并进行切片,我自己在好奇map的启动的数量和文件的大小有什么关系,进过学习得知map的数量和文件切片的数量有关系,那文件的大小 ...
随机推荐
- Oracle重建表索引及手工收集统计信息
Oracle重建所有表的索引的sql: SELECT 'alter index ' || INDEX_NAME || ' rebuild online nologging;' FROM USER_IN ...
- 一致性hash应用到redis
理解分布式存储的本质 有一个经典的实践经验: 数(值)据大了, 什么都是问题! 如果要求128B或更大数值计算, 哪么四则运算会是个大问题! 如果要求128T或更大日志存储, 哪么文件存储会是个大问题 ...
- Struts之ForwardAction
在Struts中,通过action跳转jsp,但是有时希望仅仅只是跳转页面,而不需要action,这时可以用ForwardAction. 定义一个仅仅是跳转的ForwardAction如下: < ...
- ListIterator-迭代器
Iterable(lang)-->Iterator(util)-->ListIterator(util) method: 1.void add(E o) 2.boolean hasNext ...
- 如何编写好的jQuery代码
本文就是自己看,如果您不小心进到了这里,请看源处,是这个作者翻译的:http://blog.sae.sina.com.cn/archives/4157 讨论jQuery和javascript性能的文章 ...
- 剑指offer--面试题12
题目:打印从1~最大的n位数 分析:知道陷阱在哪,即n很大时若用通常的int,long会溢出:想到用字符串解决,这涉及到字符转数字及反过来. 刚开始纠结于字符串怎么加1,想了片刻,觉得应该取出最后一位 ...
- 17.2 The DispatcherServlet
综述: Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed arou ...
- Oracle 一次执行多条语句
在.Net使用多次方法一次执行多条语句都不成功, 百度了许久才找到正确的解决方案. Oracle执行多条语句的时候 不能有物理换行 写法对比: 如下写法是不成功. begin into t_test ...
- PE文件结构详解(六)重定位
前面两篇 PE文件结构详解(四)PE导入表 和 PE文件结构详解(五)延迟导入表 介绍了PE文件中比较常用的两种导入方式,不知道大家有没有注意到,在调用导入函数时系统生成的代码是像下面这样的: 在这里 ...
- ifram一些常用的知识点
本文摘自:http://www.cnblogs.com/duankaige/archive/2012/09/20/2695012.html iframe的调用包括以下几个方面:(调用包含html ...