期间遇到了无法转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实现排序功能的更多相关文章

  1. Mapreduce之排序&规约&实战案例

    MapReduce 排序和序列化 简单介绍 ①序列化 (Serialization) 是指把结构化对象转化为字节流②反序列化 (Deserialization) 是序列化的逆过程. 把字节流转为结构化 ...

  2. 禁用datagridview中的自动排序功能

    把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...

  3. MapReduce --全排序

    MapReduce全排序的方法1: 每个map任务对自己的输入数据进行排序,但是无法做到全局排序,需要将数据传递到reduce,然后通过reduce进行一次总的排序,但是这样做的要求是只能有一个red ...

  4. ListBox实现拖拽排序功能

    1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...

  5. 简单实现Redis缓存中的排序功能

    1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...

  6. Java实现中文字符串的排序功能

    package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...

  7. MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能

    MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...

  8. nls_sort和nlssort 排序功能介绍

    nls_sort和nlssort 排序功能介绍 博客分类: oracle   ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码 ...

  9. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

随机推荐

  1. Codeforces 948C Producing Snow(优先队列+思维)

    题目链接:http://codeforces.com/contest/948/problem/C 题目大意:给定长度n(n<=1e5),第一行v[i]表示表示第i堆雪的体积,第二行t[i]表示第 ...

  2. 20165333 2016-2017-2 《Java程序设计》第1周学习总结

    20165333 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 java 的地位 Java 的特点 安装JDK 系统环境的设置 Java程序的编写,编译和运 ...

  3. MyBatis的动态插入语句(经常报‘无效的列类型’)

    最近在工作中经常遇到一个情况:通过mybatis的标签执行插入语句,当表中字段比较多的时候,需要全部插入,而有时候的需求是只插入其中几个字段,但是会报错. 原来的语句,必须把所有字段都Set值. &l ...

  4. C#控制台应用程序之选课系统

    本程序以文本文件作为存储媒介,实现了一个简化版的选课系统,主要实现了以下功能 对学生信息的增删改查 对课程信息的增删改查 对学生选课的增删改查 using System; using System.C ...

  5. mysql千万级表关联优化(2)

    概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上. 这个 ...

  6. mongo oplog 整理

    首先需要介绍一下mongodb local库的作用 local库是MongoDB的系统库,记录着时间戳和索引和复制集等信息 test:PRIMARY> use local switched to ...

  7. linux 101 hacks 2date,grep,find

    感觉挨个按着作者来的太蠢了,我还是放自己觉得不错的东西把 用特定格式显示当前时间 以下的方法可以用各种不同的格式来显示当前时间: $ date Thu Jan :: PST $ date --date ...

  8. JavaScript event loop事件循环 macrotask与microtask

    macrotask  姑且称为宏任务,在很多上下文也被简称为task.例如: setTimeout, setInterval, setImmediate, I/O, UI rendering. mic ...

  9. 010 secondary namenode(同步元数据和日志)

    1.格式化 首先格式化之后只剩下一个根目录. 格式化后会出现元数据 集群启动之后,元数据放在内存中的(消耗内存中) 格式化后会产生镜像文件fsimage,元数据存储 启动的时候namenode会读取镜 ...

  10. DNS 设置

    用上 ip 靓号1.1.1.1,Cloudflare 花了多少钱? ipv4: 1.1.1.1 和 1.0.0.1 是目前最快的 DNS. 通过ipv6***之二--使用IPv6 DNS服务器