hadoop笔记之MapReduce的应用案例(利用MapReduce进行排序)

MapReduce的应用案例(利用MapReduce进行排序)

MapReduce的应用案例(利用MapReduce进行排序)

思路:

Reduce之后直接进行结果合并

具体样例:

程序名:Sort.java

import java.io.IOException;

import java.util.StringTokenizer;

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.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Partitioner;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class Sort {//map将输入中的value化成IntWritable类型作为输出的key    public static class Map extends            Mapper<Object, Text, IntWritable, IntWritable> {

private static IntWritable data = new IntWritable();//实现map函数        public void map(Object key, Text value, Context context)                throws IOException, InterruptedException {            String line = value.toString();

data.set(Integer.parseInt(line));

context.write(data, new IntWritable(1));

}

}/*reduce将输入中的key复制到输出数据的key上,然后根据输入的value-list中的元素的个数决定key的输出次数,用全局linenum来代表key的位次*/    public static class Reduce extends            Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {

private static IntWritable linenum = new IntWritable(1);//实现reduce函数        public void reduce(IntWritable key, Iterable<IntWritable> values,                Context context) throws IOException, InterruptedException {

for (IntWritable val : values) {

context.write(linenum, key);

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

}    }

public static class Partition extends Partitioner<IntWritable, IntWritable> {

@Override        public int getPartition(IntWritable key, IntWritable value,                int numPartitions) {            int MaxNumber = 65223;            int bound = MaxNumber / numPartitions + 1;            int keynumber = key.get();            for (int i = 0; i < numPartitions; i++) {                if (keynumber < bound * i && keynumber >= bound * (i - 1))                    return i - 1;            }            return 0;        }    }

/**     * @param args     */

public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        Configuration conf = new Configuration();        String[] otherArgs = new GenericOptionsParser(conf, args)                .getRemainingArgs();        if (otherArgs.length != 2) {            System.err.println("Usage WordCount <int> <out>");            System.exit(2);        }        Job job = new Job(conf, "Sort");        job.setJarByClass(Sort.class);        //设置map和reduce处理类        job.setMapperClass(Map.class);        job.setPartitionerClass(Partition.class);        job.setReducerClass(Reduce.class);        job.setOutputKeyClass(IntWritable.class);        job.setOutputValueClass(IntWritable.class);        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));        System.exit(job.waitForCompletion(true) ? 0 : 1);    }

}

时间: 2024-08-27 17:54:27

hadoop笔记之MapReduce的应用案例(利用MapReduce进行排序)的相关文章

2018-08-04 期 MapReduce倒排索引编程案例2(jobControll方式)

1.第一阶段MapReduce任务程序 package cn.itcast.bigdata.index; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import or

Hadoop阅读笔记(二)——利用MapReduce求平均数和去重

前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一年……我在博客园-_-#,希望用dt的代码燃烧脑细胞,温暖小心窝. 上篇<Hadoop阅读笔记(一)——强大的MapReduce>主要介绍了MapReduce的在大数据集上处理的优势以及运行机制,通过专利数据编写Demo加深了对于MapReduce中输入输出数据结构的细节理解.有了理论上的指导,仍

Hadoop MapReduce编程入门案例

Hadoop入门例程简析中 (下面的程序下载地址:http://download.csdn.net/detail/zpcandzhj/7810829) 一.一些说明 (1)Hadoop新旧API的区别 新的API倾向于使用虚类(抽象类),而不是接口,因为这更容易扩展. 例如,可以无需修改类的实现而在虚类中添加一个方法(即用默认的实现). 在新的API中,mapper和reducer现在都是虚类. 新的API 放在org.apache.hadoop.mapreduce 包(和子包)中.之前版本的A

Hadoop 中利用 mapreduce 读写 mysql 数据

Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP 的需求,我们需要 mapreduce 与 mysql 进行数据的交互,而这些特性正是 hbase 或者 hive 目前亟待改进的地方. 好了言归正传,简单的说说背景.原理以及需要注意的地方: 1.为了方便 MapReduce 直接访问关系型数据库(Mysql,Oracle),Hadoop提供了DBInp

MapReduce 单词统计案例编程

MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1.   解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz到/opt/software目录下. 解压到/opt/tools目录下: [[email protected] tools]$ tar -zxf /opt/sofeware/eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz -C /opt/tool

2018-08-03 期 MapReduce倒排索引编程案例1(Combiner方式)

package cn.sjq.bigdata.inverted.index; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.map

hadoop笔记之Hive入门(Hive的体系结构)

Hive入门(二) Hive入门(二) Hive的体系结构 ○ Hive的元数据 Hive将元数据存储在数据库中(metastore),支持mysql.derby.oracle等数据库,Hive默认是derby数据库 Hive中的元数据包括表的名字,表的列和分区及其属性,表的属性(是否为外部表等),表的数据所在目录等 ○ HQL的执行过程 解释器.编译器.优化器完成HQL查询语句从词法分析.语法分析.编译.优化以及查询计划(Plan)的生成.生成的查询计划存储在HDFS中,并在随后有MapRed

Hadoop笔记(一)

1.Hadoop要解决2个问题 海量数据的存储--HDFS 海量数据的分析--MapReduce HDFS的设计目标 Very large files Streaming data access Commodity hardware HDFS不适合的场景 Low-latency data access Lots of small files Multiple writers,arbitrary file modifications 2.宿主机(windows)和客户机(虚拟机中的linux)网络

利用MapReduce实现倒排索引

这里来学习的是利用MapReduce的分布式编程模型来实现简单的倒排索引. 首先什么是倒排索引? 倒排索引是文档检索中最常用的数据结构,被广泛地应用于全文搜索引擎. 它主要是用来存储某个单词(或词组)在一个文档或一组文档中存储位置的映射,即可以通过内容来查找文档: 而不是通过文档来确定文档所包含的内容,因而被称作倒排索引(Inverted Index). 倒排索引的基本原理和建立过程可以用图来说明. 各种类型的文件经过解析后变成纯文本,再经过中文分词,并与对应的文档号进行组合, 就形成了最简单的