期间遇到了无法转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. redis tutorail

    命令 set     get    incr expire  秒  ttl    -1 不会过期 list  : lpush  rpush  lpop  rpop   lrange   llen se ...

  2. sem_open中信号量命名

    问题: sem_open will failed with "No such file or directory"   解释1: 这是由于在Linux内核中,创建信号量的默认路径是 ...

  3. OutLook中添加Exchange失败问题

    问题: 在邮件中添加账户后,打开outlook时报出错误:无法启动 Microsoft Outlook. 无法打开 Outlook 窗口. 无法打开此文件夹集合. 必须先使用当前的配置文件连接到 Mi ...

  4. hdu 5137 去掉一个点 使得最短路最大(2014广州现场赛 K题)

    题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. Sample Input4 51 2 31 3 71 4 502 3 43 4 23 ...

  5. Visual Studio 2017 发布 附带下载地址

    链接: https://pan.baidu.com/s/1kFjGwyj5HwabvmJKiyLF_g 提取码: 关注公众号[GitHubCN]回复获取    winform框架源码-Devexpre ...

  6. Spark(九)Spark之Shuffle调优

    一.概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作.因此,如果要让作业的性能更上一层楼,就有必要对shuffle过程进行 ...

  7. 【LOJ】#2063. 「HAOI2016」字符合并

    题解 dp[i][j][S]表示区间[i,j]内剩余的数位状压后为S的最大值 这样转移起来不就是\(n^3 2^8\)了吗 冷静一下,我们可以发现一段区间内剩下的数位的个数是一定的,也就是我们可以在枚 ...

  8. Dubbo中多注册中心问题与服务分组

    一:注册中心 1.场景 Dubbo 支持同一服务向多注册中心同时注册, 或者不同服务分别注册到不同的注册中心上去, 甚至可以同时引用注册在不同注册中心上的同名服务. 2.多注册中心注册 中文站有些服务 ...

  9. HBase错误:ERROR: Can't get master address from ZooKeeper; znode data == null 解决办法

    一.问题背景 使用命令 $ hbase shell 进入hbase的shell之后使用create命令创建表时出现错误:ERROR: Can't get master address from Zoo ...

  10. 机器学习之路:python 文本特征提取 CountVectorizer, TfidfVectorizer

    本特征提取: 将文本数据转化成特征向量的过程 比较常用的文本特征表示法为词袋法词袋法: 不考虑词语出现的顺序,每个出现过的词汇单独作为一列特征 这些不重复的特征词汇集合为词表 每一个文本都可以在很长的 ...