MapReduce中怎么实现倒排索引
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,今天就跟大家聊聊有关MapReduce中怎么实现倒排索引,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。需求: 为a, b, c 3个文本文件中
千家信息网最后更新 2025年12月02日MapReduce中怎么实现倒排索引
今天就跟大家聊聊有关MapReduce中怎么实现倒排索引,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
需求: 为a, b, c 3个文本文件中的单词建倒排索引
输出格式:
a:
hello world
hello hadoop
hello world
b:
spark hadoop
hello hadoop
world hadoop
c:
spark world
hello world
hello spark
map阶段
context.write("hello:a","1")context.write("hello:a","1")context.write("hello:a","1")map阶段输出: <"hello:a",{1,1,1}>
combine阶段
context.write("hello","a:3");context.write("hello","b:1");context.write("hello","c:2");combine阶段输出: <"hello",{"a:3","b:1","c:2"}>
reduce阶段
context.write("hello","a:3,b:1,c:2");reduce阶段输出: <"hello","a:3,b:1,c:2">
定义Mapper类, 该类继承org.apache.hadoop.mapreduce.Mapper
并重写map()方法
public class IIMapper extends Mapper{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = StringUtils.split(line, " "); // 从context中获取文件切片inputSplit FileSplit inputSplit = (FileSplit) context.getInputSplit(); // 从inputSplit中获取文件的绝对路径path String path = inputSplit.getPath().toString(); int index = path.lastIndexOf("/"); // 从path中截取文件名 String fileName = path.substring(index + 1); for (String word : words) { context.write(new Text(word + ":" + fileName), new Text("1")); } // map输出结果 <"hello:a",{1,1,1}> } }
定义Combiner类, 该类继承org.apache.hadoop.mapreduce.Reducer
combine阶段是map阶段和reduce阶段的中间过程
并重写reduce()方法
public class IICombiner extends Reducer{ @Override protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { String[] data = key.toString().split(":"); String word = data[0]; String fileName = data[1]; int count = 0; for (Text value : values) { count += Integer.parseInt(value.toString()); } context.write(new Text(word), new Text(fileName + ":" + count)); // combine输出结果 <"hello",{"a:3","b:1","c:2"}> } }
定义Reducer类, 该类继承org.apache.hadoop.mapreduce.Reducer
并重写reduce()方法
public class IIReducer extends Reducer{ @Override protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { StringBuilder sb = new StringBuilder(); for (Text value : values) { sb.append(value.toString() + "\t"); } context.write(key, new Text(sb.toString())); // reduce输出结果 <"hello","a:3,b:1,c:2"> } }
测试倒排索引
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job job = Job.getInstance(new Configuration()); job.setJarByClass(InverseIndexRunner.class); // 设置job的主类 job.setMapperClass(IIMapper.class); // 设置Mapper类 job.setCombinerClass(IICombiner.class); // 设置Combiner类 job.setReducerClass(IIReducer.class); // 设置Reducer类 job.setMapOutputKeyClass(Text.class); // 设置map阶段输出Key的类型 job.setMapOutputValueClass(Text.class); // 设置map阶段输出Value的类型 job.setOutputKeyClass(Text.class); // 设置reduce阶段输出Key的类型 job.setOutputValueClass(Text.class); // 设置reduce阶段输出Value的类型 // 设置job输入路径(从main方法参数args中获取) FileInputFormat.setInputPaths(job, new Path(args[0])); // 设置job输出路径(从main方法参数args中获取) FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); // 提交job }job输出的结果文件:
hadoop a:1 b:3
hello b:1 c:2 a:3
spark b:1 c:2
world c:2 b:1 a:2
看完上述内容,你们对MapReduce中怎么实现倒排索引有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
阶段
输出
文件
方法
索引
类型
结果
内容
路径
参数
单词
文件名
文本
更多
格式
知识
篇文章
行业
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全教育家长观后感
保护你的网络安全漫画
我的世界tnt跑酷服务器
微信小程序云开发数据库中的图片
数据库导入不支持降级路径
做软件开发好还是硬件开发好
安徽少儿编程软件开发
不懂网络安全的人是幸福的
服务器转到装系统界面会卡
oracle数据库教案
选中数据库报错1049
数据库主键和外键的定义
航天模拟器如何换服务器
安徽浪潮服务器维修哪家好
mysql数据库运维工具
74422161服务器号
服务器业务系统安全
数据库提取原理
资源管理数据库
吕梁网络技术团队
5g移动网络技术论文
ip6 服务器
什么情况下数据库会断开
云服务器运行环境
交行总行软件开发
恺英网络技术有限公司
数据库窗体复选框怎么设置
服务器系统有那些
网络服务器停电了
观澜软件开发培训