MapReduce倒排索引

倒排索引主要用来存储某个单词或者词组在一组文档中的存储位置的映射,提供了可以根据内容查找文档的方式,而不是根据文档确定内容。建立倒排索引的目的是更加方便的进行搜索

1.新建ii包和四个java类

package org.ii;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//输入:<"MapReduce:file1.txt",1>
//输出:<"MapReduce","file1.txt:1">
public class InvertedIndexCombiner extends Reducer <Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (Text i:values){
            sum += Integer.parseInt(i.toString());
        }
        int index = key.toString().indexOf(":");
        Text keyInfo = new Text();
        Text valueInfo = new Text();
        keyInfo.set(key.toString().substring(0,index));//"MapReduce"
        valueInfo.set(key.toString().substring(index+1)+":"+ sum);
        context.write(keyInfo,valueInfo);
    }
}
package org.ii;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class InvertedIndexDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);
        job.setJarByClass(InvertedIndexDriver.class);
        job.setMapperClass(InvertedIndexMapper.class);
        job.setCombinerClass(InvertedIndexCombiner.class);
        job.setReducerClass(InvertedIndexReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        boolean res = job.waitForCompletion(true);
        System.exit(res ? 0:1);
    }
}

package org.ii;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class InvertedIndexMapper extends Mapper<LongWritable, Text,Text, Text> {
    private static Text keyInfo =new Text();
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String line =value.toString();
        String[] word =line.split("");
        FileSplit fileSplit=(FileSplit) context.getInputSplit();
        String filename=fileSplit.getPath().getName();
        for(String w:word){
            keyInfo.set(w+":"+filename);//"MapReduce:file.txt"
            context.write(keyInfo,new Text("1"));


        }
    }
}
package org.ii;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//输入:<"MapReduce","file1.txt:1"><"MapReduce","file2.txt:1"><"MapReduce","file3.txt:1">
//输出:<"MapReduce","file1.txt:1;file2.txt:1;file3.txt:1">
public class InvertedIndexReducer extends Reducer<Text, Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String fileList = new String();

        for (Text value:values){
            fileList += value.toString() +";";
        }
        context.write(key,new Text(fileList));
    }
}

 3.打包上传


5.测试 新建三个文本文档

vi file1.txt
vi file2.txt
vi file3.txt

 

hdfs dfs -mkdir /invertIndex
hdfs dfs -mkdir /invertIndex/input

hdfs dfs -put file1.txt  /invertIndex/input
hdfs dfs -put file2.txt  /invertIndex/input
hdfs dfs -put file3.txt  /invertIndex/input
 hadoop jar HadoopDemo-1.0-SNAPSHOT.jar org.ii.InvertedIndexDriver /invertIndex/input /invertIndex/output

6.查看结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值