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

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. PYQT4 + Python2.6 + eric4-4.2.2a的安装全过程

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

  2. MSA2312 enclosure 闪断后

    故障描述:由于电源原因,导致整个扩展柜闪断,硬盘全部为leftover状态. 存储划分配置:之前满配的一套MSA2312,划分为4个vd,后面两个vd无影响,前面2个VD都是一半在1号柜子,一半在2号 ...

  3. OCP读书笔记(7) - 使用RMAN执行恢复

    7.Using RMAN to Perform Recovery 使用RMAN进行完全恢复system表空间文件丢失的恢复 模拟损坏: SQL> conn /as sysdba; SQL> ...

  4. openGl超级宝典学习笔记 (1)第一个三角形

    执行效果 代码及解析: // // Triangle.cpp // Triangle // // Created by fengsser on 15/6/20. // Copyright (c) 20 ...

  5. Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS

    Patch to solve sqlite3_int64 error when building Python 2.7.3 on RHEL/CentOS Patch to solve sqlite3_ ...

  6. [置顶] ANDROID 返回,菜单和HOME键的监听

    ------网上找了很多资料,项目中使用,最后将经验总结如下: 1,返回和菜单键是可以直接重写onKeyDown(int keyCode, KeyEvent event) 方法监听: @Overrid ...

  7. Urban Dictionary: psd

    Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...

  8. Android5.0L下因sensorservice crash导致systemserver重新启动的第二种场景分析

    一.出问题的场景 1.Sensorservice线程正在处理compass sensor事件的过程中.检查了一次buffer的指针的有效性,并在稍后会传递到AKM获取数据的函数接口中使用 2.Sens ...

  9. Hadoop云计算大数据书籍分享

    1. 推荐书名    大数据云计算利器: Hadoop, The Definitive Guide, 1Ed.pdf(第1版)    大数据云计算利器: Hadoop, The Definitive ...

  10. hdu1159 LCS模板题

    题目分析 pid=1159">原题地址 最简单的最长公共子序列(LCS)问题的模板题了.不解释. ------------------------------------------- ...