博客
关于我
内存排序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/

你可能感兴趣的文章
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>
Netty工作笔记0025---SocketChannel API
查看>>
Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
查看>>
Netty工作笔记0050---Netty核心模块1
查看>>
Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>