MapReduce中怎么实现倒排索引
发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,今天就跟大家聊聊有关MapReduce中怎么实现倒排索引,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。需求: 为a, b, c 3个文本文件中
千家信息网最后更新 2025年02月08日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安全错误
数据库的锁怎样保障安全
网信办网络安全视频
服务器管理器共享文件协议
酷开网络安全黑板报
数据库pool
比5G更牛的网络技术
安卓系统手机软件开发
乐视互联网科技股份有限公司
数据库嵌套语句查询语句吗
村级网络安全知识培训
小米更新云端清理数据库
数据库系统原理学什么
软件开发去哪里发展
mc服务器掉线
网络安全审查结果多久
易语言打开数据库整列的内容
女孩学计算机网络安全
自组网络技术
软件开发部文档
c网络安全
怎么看战地5什么服务器
中北众成软件开发平台有哪些
游戏直播app软件开发方案介绍
挂机宝vps服务器租用
数据库嵌套语句查询语句吗
聊城有实力的联想服务器
门禁管理服务器 海康
智眸星互联网科技
如何选择运营商登陆服务器
下列关于数据数据库
上海pdu服务器电源厂商哪家好