文件的内容例如以下所看到的:

5

45

8

876

6

45

要求最后的输出格式:

1    5

2    6

3    8

4    45

5    45

5    876

首先,这个题目是须要对文件的内容进行排序操作。我们都知道在mapper阶段是会对key进行排序的,我们就利用这个出发,把输入一行的数据转换成int,再把该int做mapper的key输出,而value的输出随便,我们这里输出1;然后在reduce阶段我们把mapper的key做为reduce的value输出,而key仅仅需定义一个全局的静态变量,每次输出自增就可以。

package cn.lmj.mapreduce;





import java.io.IOException;

import java.util.Iterator;





import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapred.FileInputFormat;

import org.apache.hadoop.mapred.FileOutputFormat;

import org.apache.hadoop.mapred.JobClient;

import org.apache.hadoop.mapred.JobConf;

import org.apache.hadoop.mapred.MapReduceBase;

import org.apache.hadoop.mapred.Mapper;

import org.apache.hadoop.mapred.OutputCollector;

import org.apache.hadoop.mapred.Reducer;

import org.apache.hadoop.mapred.Reporter;

import org.apache.hadoop.mapred.TextInputFormat;

import org.apache.hadoop.mapred.TextOutputFormat;





public class Sort

{

public static class SortMapper extends MapReduceBase implements

Mapper<Object, Text, IntWritable, IntWritable>

{

@Override

public void map(Object key, Text value,

OutputCollector<IntWritable, IntWritable> output,

Reporter reporter) throws IOException

{

String line = value.toString();

int i = Integer.parseInt(line.toString());

output.collect(new IntWritable(i), new IntWritable(1));

}

}





public static class SortReducer extends MapReduceBase implements

Reducer<IntWritable, IntWritable, IntWritable, IntWritable>

{

//必须是全局的静态变量,由于reduce的实例在开发中可能会有非常多个,必须让多个对象共享同一个变量

private static IntWritable linenum = new IntWritable(1);





@Override

public void reduce(IntWritable key, Iterator<IntWritable> values,

OutputCollector<IntWritable, IntWritable> output,

Reporter reporter) throws IOException

{

while (values.hasNext())

{

values.next();

output.collect(linenum, key);

//每次输出让linenum加1

linenum = new IntWritable(linenum.get() + 1);

}

}

}





public static void main(String[] args) throws Exception

{

JobConf conf = new JobConf(Sort.class);

conf.setJobName("cccccc");





conf.setOutputKeyClass(IntWritable.class);

conf.setOutputValueClass(IntWritable.class);





conf.setMapperClass(SortMapper.class);

//注意,这个题目不能够设置Combiner对mapper之后的数据进行预先合拼

conf.setReducerClass(SortReducer.class);





conf.setInputFormat(TextInputFormat.class);

conf.setOutputFormat(TextOutputFormat.class);





FileInputFormat.setInputPaths(conf, new Path("/zuoye/file1/"));

FileOutputFormat.setOutputPath(conf, new Path("/zuoye/file1/output"));





JobClient.runJob(conf);

}

}

mapreduce程序来实现分类的更多相关文章

  1. hive--构建于hadoop之上、让你像写SQL一样编写MapReduce程序

    hive介绍 什么是hive? hive:由Facebook开源用于解决海量结构化日志的数据统计 hive是基于hadoop的一个数据仓库工具,可以将结构化的数据映射为数据库的一张表,并提供类SQL查 ...

  2. 攻城狮在路上(陆)-- 配置hadoop本地windows运行MapReduce程序环境

    本文的目的是实现在windows环境下实现模拟运行Map/Reduce程序.最终实现效果:MapReduce程序不会被提交到实际集群,但是运算结果会写入到集群的HDFS系统中. 一.环境说明:     ...

  3. windows环境下Eclipse开发MapReduce程序遇到的四个问题及解决办法

    按此文章<Hadoop集群(第7期)_Eclipse开发环境设置>进行MapReduce开发环境搭建的过程中遇到一些问题,饶了一些弯路,解决办法记录在此: 文档目的: 记录windows环 ...

  4. 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行

    今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...

  5. 如何在Hadoop的MapReduce程序中处理JSON文件

    简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...

  6. hadoop——在命令行下编译并运行map-reduce程序 2

     hadoop map-reduce程序的编译需要依赖hadoop的jar包,我尝试javac编译map-reduce时指定-classpath的包路径,但无奈hadoop的jar分布太散乱,根据自己 ...

  7. hadoop-初学者写map-reduce程序中容易出现的问题 3

    1.写hadoop的map-reduce程序之前所必须知道的基础知识: 1)hadoop map-reduce的自带的数据类型: Hadoop提供了如下内容的数据类型,这些数据类型都实现了Writab ...

  8. mapreduce程序编写(WordCount)

    折腾了半天.终于编写成功了第一个自己的mapreduce程序,并通过打jar包的方式运行起来了. 运行环境: windows 64bit eclipse 64bit jdk6.0 64bit 一.工程 ...

  9. 基于Maven管理的Mapreduce程序下载依赖包到LIB目录

    1.Mapreduce程序需要打包作为作业提交到Hadoop集群环境运行,但是程序中有相关的依赖包,如果没有一起打包,会出现xxxxClass Not Found . 2.在pom.xml文件< ...

随机推荐

  1. frontend http 前端名字定义问题

    https://www.winfae.com/admin/api/menu haproxy 日志: Jun 24 13:04:49 localhost haproxy[14817]: 115.236. ...

  2. PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程

    PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 - beike - ITeye技术网站 PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程 博客 ...

  3. 基于模糊Choquet积分的目标检测算法

    本文根据论文:Fuzzy Integral for Moving Object Detection-FUZZ-IEEE_2008的内容及自己的理解而成,如果想了解更多细节,请参考原文.在背景建模中,我 ...

  4. HDU4876ZCC loves cards(多校题)

    ZCC loves cards Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...

  5. poj 2586 Y2K Accounting Bug(贪心算法,水题一枚)

    #include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...

  6. hdu4489(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4489 题意:给一个n,求n个高矮不同的人排成一排使得高.矮依次排列的种数. 详细思路参考:http:/ ...

  7. mysql-merge合并表

    merge表 注意: 1 每个子表的结构必须一致,主表和子表的结构需要一致, 2 每个子表的索引在merge表中都会存在,所以在merge表中不能根据该索引进行唯一性检索. 3 子表需要是MyISAM ...

  8. (转)一篇很不错的介绍Eclipse插件Menu及其扩展点的文章

    原文在:http://tech.ddvip.com/2010-04/1271054623150507.html 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包 ...

  9. Hadoop Spark 集群简便安装总结

    本人实际安装经验,目的是为以后高速安装.仅供自己參考. 一.Hadoop 1.操作系统一如既往:①setup关掉防火墙.②vi /etc/sysconfig/selinux,改SELINUX=disa ...

  10. 解决eclipse project前出现红色感叹号 但没有提示错误出在什么地方build path jar包也没报错

    在problems窗体能够看到错误信息 菜单条 window-->show view-->problems 或alt+shift+q 然后按x 又一次选一下包就攻克了