Hadoop-Invert-Index

本文介绍倒排索引的基本概念及其在文档检索系统中的应用,并通过MapReduce实现了一个简单的倒排索引示例。

倒排索引是文档检索系统中最常见的数据结构,被广泛用于全文索引引擎。它主要是用来存储某个单词(或词组)在一个文档或一组文档那该的存储位置的映射,即提供了一种根据内容来查找文档的方式。由于不是根据文档来确定文档所包含的内容,而是进行了相反的操作(即根据关键字来查找文档),故称为倒排索引。

源码

import java.io.IOException;  
import java.util.StringTokenizer;  

import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.fs.Path;  
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.input.FileSplit;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  

public class InversedIndex {  

    /** 
     * 将输入文件拆分, 
     * 将关键字和关键字所在的文件名作为map的key输出, 
     * 该组合的频率作为value输出 
     * */  

    public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {  

        private Text outKey = new Text();  
        private Text outVal = new Text();  

        @Override  
        public void map (Object key,Text value,Context context) {  
            StringTokenizer tokens = new StringTokenizer(value.toString());  
            FileSplit split = (FileSplit) context.getInputSplit();  
            while(tokens.hasMoreTokens()) {  
                String token = tokens.nextToken();  
                try {  
                    outKey.set(token + ":" + split.getPath());  
                    outVal.set("1");  
                    context.write(outKey, outVal);  
                } catch (IOException e) {  
                    e.printStackTrace();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  

    /** 
     * map的输出进入到combiner阶段,此时来自同一个文件的相同关键字进行一次reduce处理, 
     * 将输入的key拆分成关键字和文件名,然后关键字作为输出key, 
     * 将文件名与词频拼接,作为输出value, 
     * 这样就形成了一个关键字,在某一文件中出现的频率的 key--value 对 
     * */  
    public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {  

        private Text outKey = new Text();  
        private Text outVal = new Text();  

        @Override  
        public void reduce(Text key,Iterable<Text> values,Context context) {  
            String[] keys = key.toString().split(":");  
            int sum = 0;  
            for(Text val : values) {  
                sum += Integer.parseInt(val.toString());  
            }  
            try {  
                outKey.set(keys[0]);  
                int index = keys[keys.length-1].lastIndexOf('/');  
                outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);  
                context.write(outKey, outVal);  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  

    }  

    /** 
     * 将combiner后的key value对进行reduce, 
     * 由于combiner之后,一个关键字可能对应了多个value,故需要将这些value进行合并输出 
     * */  

    public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {  

        @Override  
        public void reduce (Text key,Iterable<Text> values,Context context) {  
            StringBuffer sb = new StringBuffer();  
            for(Text text : values) {  
                sb.append(text.toString() + " ,");  
            }  
            try {  
                context.write(key, new Text(sb.toString()));  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {  
        Configuration conf = new Configuration();  
        Job job = new Job(conf,"index inversed");  

        job.setJarByClass(InversedIndex.class);  
        job.setMapperClass(InversedIndexMapper.class);  
        job.setCombinerClass(InversedIndexCombiner.class);  
        job.setReducerClass(InversedIndexReducer.class);  
        job.setMapOutputKeyClass(Text.class);  
        job.setMapOutputValueClass(Text.class);  
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(Text.class);  

        job.setNumReduceTasks(3);  

        FileInputFormat.addInputPath(job, new Path("input"));  
        FileOutputFormat.setOutputPath(job, new Path("output"));  

        System.exit(job.waitForCompletion(true)?0:1);  

    }  

} 

Example

原文本文件

text1.txt
MapReduce is sample

text2.txt
MapReduce is powerful is sample

text3.txt
Hello MapReduce hello world


运行结果文件

Hello text3.txt:1
MapReduce text3.txt:1 text1.txt:1 , text2.txt:1
hello text2.txt:2 , text1.txt:1
powerful text2.txt:1 ,text1.txt:1
world text3.txt:1

过程分析

Map过程:
将输入文件关键字和关键字所在的文件名组合作为map的key输出,map的value为1
如 MapReduce这个关键字map后结果为:
< MapReduce:text1.tx1 ,1>
< MapReduce:text2.txt ,1>
< MapReduce:text3.txt ,1>

Combiner过程:
对Map过程中产生的key相同的做Reduce处理
如< hello:text3.txt , 1> < hello:text3.txt ,1 >合并成< hello ,text3.txt:2>

Reduce过程:

对于combiner过程中产生的具有相同的key的value进行合并输出,最终结果为运行结果文件

摘自Hadoop之道–MapReduce简单应用倒排索引(InvertedIndex)

作品题目:共享单车数据分析与可视化 作品目标: 本考核依托真实共享单车数据,培养学生三项核心能力:掌握HDFS数据管理及Hive分区表构建技能,实现从原始数据到分析结果的ETL流程;运用HiveQL完成用户分类与路线热度等多维聚合分析,输出精准业务洞察;通过Matplotlib绘制专业图表并提出运营优化方案,最终形成涵盖数据溯源、可视化成果及决策建议的结构化报告,全面提升数据价值转化实战能力。 说明: 数据集:202401-capitalbikeshare-tripdata.zip 核心字段说明: ​​字段名​​ ​​类型​​ ​​说明​​ ride_id STRING 行程唯一ID started_at TIMESTAMP 起始时间 ended_at TIMESTAMP 结束时间 start_station_name STRING 起始站点名称 end_station_name STRING 终点站点名称 rideable_type STRING 自行车类型(classic/electric) member_casual STRING 用户类型(member/casual) 任务步骤: 步骤1:数据集获取与预处理​​ 1.1下载数据集​ 1.2解压与清洗数据 步骤2:HDFS数据存储​ 2.1 启动Hadoop服务 2.2 创建HDFS目录​ 2.3 上传数据到HDFS​ 步骤3:Hive数据分析​​ 3.1 启动Hive 3.2 创建原始表​ 3.3 创建分区表​ 3.3.1 创建分区表结构​ 3.3.2 开启动态分区 3.3.3 导入数据 3.3.4 验证导入 3.4 执行数据分析 3.4.1 创建最热门路线视图 3.4.2 查询TOP10 3.4.3 保存结果到本地文件 步骤4:数据可视化​​ 4.1 准备Python脚本 4.2 输入完整可视化代码 4.3 执行脚本​​ 4.3.1 安装依赖库(首次运行) 4.3.2 运行可视化 4.3.3 查看结果 步骤5:报告撰写,内容包括 (1)数据集描述:数据来源、数据结构等 (2)数据分析过程:包括HiveQL查询的设计和执行结果 (3)可视化结果展示:展示Matplotlib生成的图表,并对结果进行解释 (4)总结与反思:总结分析的主要发现,讨论可能的改进方向和遇到的挑战 步骤6:作品提交 (1)提交一份完整的报告(word格式) (2)提交所有使用的代码文件,包括HiveQL脚本和Python脚本 (3)提交生成的可视化图表文件(也可将可视化图表截图放进word报告里面) (4)报告命名格式(压缩包命名一样):班级+学号+名字(如23大数据1班+202317030152+张朝阳)可以使用本电脑里的一切配置以及软件,按照上述步骤来执行,完成我所发布的指令,显示最后结果。
最新发布
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值