MapReduce实现排序功能
期间遇到了无法转value的值为int型,我採用try catch解决
str2 2
str1 1
str3 3
str1 4
str4 7
str2 5
str3 9
用的\t隔开,得到结果
str1 1,4
str2 2,5
str3 3,9
str4 7
我这里map,reduce都是单独出来的类,用了自己定义的key
package com.kane.mr;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
import com.j_spaces.obf.fi;
//str2 2
//str1 1
//str3 3
//str1 4
//str4 7
//str2 5
//str3 9
public class IntPair implements WritableComparable<IntPair>{
public String getFirstKey() {
return firstKey;
}
public void setFirstKey(String firstKey) {
this.firstKey = firstKey;
}
public int getSecondKey() {
return secondKey;
}
public void setSecondKey(int secondKey) {
this.secondKey = secondKey;
}
private String firstKey;//str1
private int secondKey;//1
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(firstKey);
out.writeInt(secondKey);
}
@Override
public void readFields(DataInput in) throws IOException {
firstKey=in.readUTF();
secondKey=in.readInt();
}
//这里做比較,还有一个是自身本类,对key进行排序
@Override
public int compareTo(IntPair o) {
// int first=o.getFirstKey().compareTo(this.firstKey);
// if (first!=0) {
// return first;
// }
// else {
// return o.getSecondKey()-this.secondKey;
// }
return o.getFirstKey().compareTo(this.getFirstKey());
}
}
package com.kane.mr;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class SortMapper extends Mapper<Object,Text,IntPair,IntWritable>{
public IntPair intPair=new IntPair();
public IntWritable intWritable=new IntWritable(0);
@Override
protected void map(Object key, Text value,//str1 1
Context context)
throws IOException, InterruptedException {
//String[] values=value.toString().split("/t");
System.out.println(value);
int intValue;
try {
intValue = Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
intValue=6;
}//不加try catch总是读取value时,无法转成int型
intPair.setFirstKey(key.toString());
intPair.setSecondKey(intValue);
intWritable.set(intValue);
context.write(intPair, intWritable);// key(str2 2) 2
}
}
package com.kane.mr;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class SortReducer extends Reducer<IntPair, IntWritable, Text,Text>{
@Override
protected void reduce(IntPair key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException {
StringBuffer combineValue=new StringBuffer();
Iterator<IntWritable> itr=values.iterator();
while (itr.hasNext()) {
int value=itr.next().get();
combineValue.append(value+",");
}
context.write(new Text(key.getFirstKey()),new Text(combineValue.toString()));
}
}
package com.kane.mr;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner;
public class PartionTest extends Partitioner<IntPair, IntWritable>{
@Override
public int getPartition(IntPair key, IntWritable value, int numPartitions) {//reduce个数
return (key.getFirstKey().hashCode()&Integer.MAX_VALUE%numPartitions);
}
}
package com.kane.mr;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
public class TextComparator extends WritableComparator{
public TextComparator(){
super(IntPair.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
IntPair o1=(IntPair)a;
IntPair o2=(IntPair)b;
return o1.getFirstKey().compareTo(o2.getFirstKey());
}
}
package com.kane.mr;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
@SuppressWarnings("rawtypes")
public class TextIntCompartor extends WritableComparator{
protected TextIntCompartor() {
super(IntPair.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
IntPair o1=(IntPair)a;
IntPair o2=(IntPair)b;
int first=o1.getFirstKey().compareTo(o2.getFirstKey());
if (first!=0) {
return first;
}
else {
return o1.getSecondKey()-o2.getSecondKey();
}
}
}
package com.kane.mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class SortMain {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "Sort");
job.setJarByClass(SortMain.class);
job.setInputFormatClass(KeyValueTextInputFormat.class);//设定输入的格式是key(中间\t隔开)value
job.setMapperClass(SortMapper.class);
//job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(SortReducer.class);
job.setMapOutputKeyClass(IntPair.class);
job.setMapOutputValueClass(IntWritable.class);
job.setSortComparatorClass(TextIntCompartor.class);
job.setGroupingComparatorClass(TextComparator.class);//以key 进行group by
job.setPartitionerClass(PartionTest.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入參数,相应hadoop jar 相应类执行时在后面加的第一个參数
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出參数
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
导出jar包放到hadoop下,然后讲sort.txt放入到hdfs中,然后用hadoop jar KaneTest/sort.jar com.kane.mr.SoetMain /kane/sort.txt /kane/output命令运行
MapReduce实现排序功能的更多相关文章
- Mapreduce之排序&规约&实战案例
MapReduce 排序和序列化 简单介绍 ①序列化 (Serialization) 是指把结构化对象转化为字节流②反序列化 (Deserialization) 是序列化的逆过程. 把字节流转为结构化 ...
- 禁用datagridview中的自动排序功能
把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...
- MapReduce --全排序
MapReduce全排序的方法1: 每个map任务对自己的输入数据进行排序,但是无法做到全局排序,需要将数据传递到reduce,然后通过reduce进行一次总的排序,但是这样做的要求是只能有一个red ...
- ListBox实现拖拽排序功能
1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...
- 简单实现Redis缓存中的排序功能
1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...
- Java实现中文字符串的排序功能
package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...
- nls_sort和nlssort 排序功能介绍
nls_sort和nlssort 排序功能介绍 博客分类: oracle ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码 ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
随机推荐
- VS Code折腾记 - (3) 多图解VSCode基础功能
前言 想了想,对于一个刚接触VSCODE的人来说,有什么比图片更通俗易懂的呢? 启动界面 : 快捷键(Ctrl + Shift + E) Search && replace : 快捷键 ...
- HBase(一)HBase入门简介
一 HBase 的起源 HBase 的原型是 Google 的 BigTable 论文,受到了该论文思想的启发,目前作为 Hadoop 的子项目来开发维护,用于支持结构化的数据存储. Apache H ...
- day6作业--游戏人生
本节作业: 熟练使用类和模块,写一个交互性强.有冲突的程序. 思路: 1.各个模块之间的调用关系,如何使用类,各种方法的使用上面: 2.学了类,以为能用来解决所有问题,东西都要写在类里面: 3.下面自 ...
- jenkins中管理用户
jenkins中管理用户: 管理用户权限
- 20169211《Linux内核原理与分析》第五周作业
1.在自己的linux系统中搭建实验环境: 2.使用GDB调试内核跟踪启动过程: 3.分析start_kernel的代码. 1.在自己的linux系统中搭建实验环境 1.1 下载linux-3.18. ...
- ACM训练计划建议(转)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- 【SQL】184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- 复杂密码生成工具apg
复杂密码生成工具apg 密码是身份认证的重要方式.由于密码爆破方式的存在,弱密码非常不安全.为了构建复杂密码,Kali Linux预置了一个复杂密码生成工具apg.该工具可以提供可读密码和随机字符 ...
- [ 转载 ] Java Jvm内存介绍
一.基础理论知识 1.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Java虚拟机有着一个清晰的任务:执行Java程序.程序开始执行时他才运行,程序结束时他就停止.你在同一台机器上 ...
- Qt中文本编辑器实现语法高亮功能(Qscitinlla)
Scintilla是一个免费.跨平台.支持语法高亮的编辑控件.它完整支持源代码的编辑和调试,包括语法高亮.错误指示.代码完成(code completion)和调用提示(call tips).能包含标 ...