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上的正序还是倒序就可以了. ...
随机推荐
- mybatis 易百练习笔记
1. session.commit() 增删改需要提交 session.close() session需要关闭 2. insert into t() values() 不用写i ...
- require和import的区别
require:是一种common协议,大家按照这个约定书写自己的代码,实现模块化. import:是ES6的模块语法实现.是语言自身的模块实现.
- Luogu P1160 【队列安排】
详细的链表讲解 很明显的一个链表裸题 和普通的链表有一个区别就是这个题 可以O(1)插入,O(1)查询 然后我们为了方便,采用双向链表,定义s.f作为指针数组 更详细的解释见代码 #include&l ...
- appium----【Mac】address already in user 127.0.0.1:4725,端口被占用的查找与kill进程
报错截图示例: 解决方法: Mac: lsof -i tcp:4723 #查看端口号 sudo kill -9 29443 #杀死进程 Windows: netstat -aon|fi ...
- 国际混淆C代码大赛获奖作品部分源码
国际C语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从1984年开始,每年举办一次(1997年.1999年 ...
- 利用Metrics+influxdb+grafana构建监控平台
https://blog.csdn.net/fishmai/article/details/51817429
- 【58沈剑架构系列】互联网公司为啥不使用mysql分区表?
缘起:有个朋友问我分区表在58的应用,我回答不出来,在我印象中,百度.58都没有听说有分区表相关的应用,业内进行一些技术交流的时候也更多的是自己分库分表,而不是使用分区表.于是去网上查了一下,并询问了 ...
- SQL Server 跨服务器快速数据转移
最近遇到一个问题,要将 a 服务器上的 A 库,迁移到 b 服务器上的 B 库,两个库的数据结构是一样的,但是数据库版本是 a 比 b 高,通过 sqlserver 还原这条路是走不通了,那难道除了 ...
- oneDay
难受过 迷茫过 失望过 耍脾气过 开心过 伤心过 疼过 走了这么久的路: 我只想说 程序的道路上 很难走: 本来准备都放弃了: 自己逼自己了很久想明白了: 不能什么时候都想着靠外力 自己的不足就是自己 ...
- iOS Sprite Kit教程之场景的设置
iOS Sprite Kit教程之场景的设置 Sprite Kit中设置场景 在图2.8所示的效果中,可以看到新增的场景是没有任何内容的,本节将讲解对场景的三个设置,即颜色的设置.显示模式的设置以及测 ...