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上的正序还是倒序就可以了. ...
随机推荐
- opencv(5)GUI
OpenCV的图形用户界面(Graphical User Interface, GUI)和绘图等相关功能也是很有用的功能,无论是可视化,图像调试还是我们这节要实现的标注任务,都可以有所帮助. 窗口循环 ...
- ZooKeeper实践:(1)集群管理
前言: 随着业务的扩大,用户的增多,访问量的增加,单机模式已经不能支撑,从而出现了从单机模式->垂直应用模式->集群模式,集群模式诞生了,伴随着一堆问题也油然而生,Master怎么选举,机 ...
- Kafka(一)Kafka的简介与架构
一.简介 1.1 概述 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ng ...
- day9--paramiko模块
志不坚者智不达 paramiko:在Linux链接其他机器,每台Linux机器都有一个SSHClient:Python自己也写了一个SSHClient,那么Python写paramiko创建SSHCl ...
- BC-NFS安装依赖
[root@BC-NFS01 glusterFS_installer]# sh install_local.sh 18-09-19 22:43:28 [install_local.sh] INFO : ...
- NHibernate 错误
Unable to locate persister for the entity named 'Model.Customer'.The persister define the persistenc ...
- 10 Best jQuery and HTML5 WYSIWYG Plugins
https://www.sitepoint.com/10-best-html-wysiwyg-plugins/
- ThreadLocal和ThreadLocalMap源码分析
目录 ThreadLocal和ThreadLocalMap源码分析 背景分析 定义 例子 源码分析 ThreadLocalMap源码分析 ThreadLocal源码分析 执行流程总结 源码分析总结 T ...
- 1019 General Palindromic Number (20)(20 point(s))
problem A number that will be the same when it is written forwards or backwards is known as a Palind ...
- Python 面向对象编程——继承和多态
<基本定义> 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超 ...