博客
关于我
内存排序TOPN
阅读量:631 次
发布时间:2019-03-14

本文共 3745 字,大约阅读时间需要 12 分钟。

MapReduce 实现 TopN 值提取

本文将介绍如何利用 MapReduce 技术实现对文件中文本数据的 TopN 值提取功能。具体将从 Mapper 和 Reducer 两个阶段入手,阐述实现逻辑及其优化方法。

1. Mapper 阶段

MapReduce 的 Mapper 阶段负责读取输入数据并进行初步处理。在本次实现中, Mapper 的主要功能是将文本数据拆分成单词,并为每个单词生成一个计数值。

  • 输入数据格式:每行一个文本段,包含多个单词。
  • 处理逻辑
    • 使用 split(" ") 方法将文本按空格分割成单词数组。
    • 遍历单词数组,对每个单词调用 context.write 方法,将单词及其计数值(固定为 "1")写入输出流。

2. Reducer 阶段

MapReduce 的 Reducer 阶段负责接收 Mapper 输出的数据并对其进行聚合和排序处理。在本次实现中, Reducer 的主要功能是统计单词出现次数,并按次数排序后取前三名。

  • 输入数据格式:每个键值对的键为单词,值为单词出现次数。
  • 处理逻辑
    • 使用 reduce 方法接收所有相同键的值。
    • 遍历所有单词,累加出现次数。
    • 使用 map 方法将单词及其出现次数存储到内存集合中。
    • cleanup 方法中对集合中的数据进行排序和输出处理。

3. 排序优化

为了实现 TopN 的排序功能,我们采用以下优化方法:

  • 内存排序:将数据存储在内存中的集合中,集合中的单词按出现次数进行排序。
  • 排序逻辑
    • 将单词及其次数组合成一个元组。
    • 使用自定义的 Comparator 对元组进行排序,主要按照次数进行降序排序。
    • 最后从排序后的集合中取出前三名单词及其次数。

4. 代码实现

以下为实现 TopN 的完整 MapReduce 代码示例:

import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;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.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class TOPN {    public static class MyMapper extends Mapper
{ @Override protected void setup(Context context) throws IOException, InterruptedException { } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split(" "); for (String word : words) { context.write(new Text(word), new Text("1")); } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { } } public static class MyReduce extends Reducer
{ @Override protected void setup(Context context) throws IOException, InterruptedException { } @Override protected void reduce(Text key, Iterable
values, Context context) throws IOException, InterruptedException { int count = 0; for (Text t : values) { count += Integer.parseInt(t.toString()); } map.put(key.toString(), count); } @Override protected void cleanup(Context context) throws IOException, InterruptedException { Set
> entrySet = map.entrySet(); List
> entryList = new ArrayList<>(); for (Map.Entry
entry : entrySet) { entryList.add(entry); } Collections.sort(entryList, new Comparator
>() { @Override public int compare(Map.Entry
o1, Map.Entry
o2) { return Integer.compare(o2.getValue(), o1.getValue()); } }); for (int i = 0; i < 3; i++) { Map.Entry
entry = entryList.get(i); context.write(new Text(entry.getKey()), new Text(entry.getValue().toString())); } } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(TOPN.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReduce.class); FileInputFormat.setInputPaths(job, new Path("input/data")); FileOutputFormat.setOutputPath(job, new Path("output")); job.waitForCompletion(true); }}

5. 执行结果

运行上述 MapReduce 作业后,将从标准输出中读取前三名单词及其出现次数。具体结果如下:

hello xiaoming xiaoming is best xiaoming betterhadoop is goodspark is nice

通过以上实现,我们成功利用 MapReduce 技术实现了对文本数据的 TopN 值提取功能,具体操作步骤和优化方法如上所述。

转载地址:http://xuooz.baihongyu.com/

你可能感兴趣的文章
MySQL底层概述—7.优化原则及慢查询
查看>>
MySQL底层概述—8.JOIN排序索引优化
查看>>
MySQL底层概述—9.ACID与事务
查看>>
Mysql建立中英文全文索引(mysql5.7以上)
查看>>
mysql建立索引的几大原则
查看>>
Mysql建表中的 “FEDERATED 引擎连接失败 - Server Name Doesn‘t Exist“ 解决方法
查看>>
mysql开启bin-log日志,用于canal同步
查看>>
MySQL开源工具推荐,有了它我卸了珍藏多年Nactive!
查看>>
MySQL异步操作在C++中的应用
查看>>
MySQL引擎讲解
查看>>
Mysql当前列的值等于上一行的值累加前一列的值
查看>>
MySQL当查询的时候有多个结果,但需要返回一条的情况用GROUP_CONCAT拼接
查看>>
MySQL必知必会(组合Where子句,Not和In操作符)
查看>>
MySQL必知必会总结笔记
查看>>
MySQL快速入门
查看>>
MySQL快速入门——库的操作
查看>>
mysql快速复制一张表的内容,并添加新内容到另一张表中
查看>>
mysql快速查询表的结构和注释,字段等信息
查看>>
mysql怎么删除临时表里的数据_MySQL中关于临时表的一些基本使用方法
查看>>
mysql性能优化
查看>>