MapReduce之倒排序

MapReduce之倒排序

倒排序索引

简单来讲,倒排序索引就是根据属性的值来进行查找操作,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射

倒排序应用

倒排序的目的是产生一个数据集的索引以提供更快搜索说或数据丰富能力,适用于需要快速搜索查询响应的场景

问题描述

通过检索到的网站查找用户ID

样例输入

关于数据集,在《MapReduce设计模式》中并没有明确的给出,所以在这里用一个普通的文件操作代码生成了一个数据集,代码如下
创建数据集程序 create.java如下

import java.io.*;
import java.util.Random;

public class create {
    public static String getRandomChar(int length) { //生成随机字符串
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }
    public static void main(String[] args) throws IOException{
        String path="input/file.txt";
        File file=new File(path);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw=new BufferedWriter(fw);

        for(int i=0;i<500;i++){    //设置数据集的行数
            int id=(int)(Math.random()*1000+1000);   //设置ID的取值范围
            bw.write("< id="+id+"  www."+getRandomChar(4)+".com>\n");    
        }
        bw.flush();
        bw.close();
        fw.close();
    }
}

运行结果如下:
生成的数据集

当然生成的数据集有将近1000行,这个大家可以自己设置生成的数据数量

输出结果

运行结果展示

问题思路

根据倒排序的定义,将文本中的网址设置为键,ID编号设为值即可

mapper阶段任务

在这个问题中,map阶段负责切割文本并整理出网址和ID编号,将文本中的网址设置为键,ID编号设为值

mapper阶段编码如下
public static class InverseMapper extends Mapper<Object,Text,Text,Text>{
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            String line=value.toString();
            String id= line.substring(5,9),link=line.substring(11,22);
            context.write(new Text(link),new Text(id));
        }
    }
reducer阶段任务

整理输入的键并映射到唯一的ID上

reducer阶段编码如下
public static class InverseReduce extends Reducer<Text,Text,Text,Text>{
        public void reduce(Text key,Iterator<Text> values,Context context) throws IOException,InterruptedException{
            String result=new String();
            while(values.hasNext()){
                result=result+"   "+values.toString();
            }
            context.write(new Text(key),new Text(result));
        }
    }

完整代码如下

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.output.FileOutputFormat;
import java.io.IOException;
import java.util.Iterator;
public class Inverse {
    public static class InverseMapper extends Mapper<Object,Text,Text,Text>{
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
            String line=value.toString();
            String id= line.substring(5,9),link=line.substring(11,22);
            context.write(new Text(link),new Text(id));
        }
    }
    public static class InverseReduce extends Reducer<Text,Text,Text,Text>{
        public void reduce(Text key,Iterator<Text> values,Context context) throws IOException,InterruptedException{
            String result=new String();
            while(values.hasNext()){
                result=result+"   "+values.toString();
            }
            context.write(new Text(key),new Text(result));
        }
    }
    public static void main(String[] args) throws Exception{
        FileUtil.deleteDir("output");
        Configuration configuration=new Configuration();
        String[] otherArgs=new String[]{"input/file.txt","output"};
        if(otherArgs.length!=2){
            System.err.println("参数错误");
            System.exit(2);
        }
        Job job=new Job(configuration,"Inverse");
        job.setJarByClass(Inverse.class);
        job.setMapperClass(InverseMapper.class);
        job.setReducerClass(InverseReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
}
关于FileUtil类

FileUtil编码如下

import java.io.File;

public class FileUtil{
    public static boolean deleteDir(String path){
        File dir=new File(path);
        if(dir.exists()){
            for(File f:dir.listFiles()){
                if(f.isDirectory()){
                    deleteDir(f.getName());
                }else {
                    f.delete();
                }
            }
            dir.delete();
            return true;
        }else{
            System.out.println("output文件(夹)不存在");
            return false;
        }
    }
}

写这段代码的目的主要是让程序运行前自动删除output,毕竟比较懒,能用代码解决的问题绝对不自己动手

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值