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

本文共 3795 字,大约阅读时间需要 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 better
hadoop is goodspark is nice

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

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

你可能感兴趣的文章
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>