美文网首页
倒排索引案例(多Job串联)

倒排索引案例(多Job串联)

作者: bullion | 来源:发表于2019-03-09 16:03 被阅读0次

需求

需求分析

第一次处理

OneIndexMapper

public class OneIndexMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    String name;

    @Override

    protected void setup(Context context) throws IOException, InterruptedException {

        // 获取文件名称

        FileSplit inputSplit = (FileSplit) context.getInputSplit();

        name = inputSplit.getPath().getName();

    }

    Text k = new Text();

    IntWritable v = new IntWritable(1);

    @Override

    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 1 获取一行

        String line = value.toString();

        // 2 切割

        String[] fields = line.split(" ");

        // 3 写出

        for (String word : fields) {

            k.set(word + "--" + name);

            context.write(k, v);

        }

    }

}

OneIndexReducer

public class OneIndexReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    @Override

    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        // 1 累加求和

        int sum = 0;

        for (IntWritable value : values) {

            sum += value.get();

        }

        IntWritable v = new IntWritable();

        v.set(sum);

        // 2 写出

        context.write(key, v);

    }

}

OneIndexDriver

public class OneIndexDriver {

    public static void main(String[] args) throws Exception {

        args = new String[]{"e:/input/inputoneindex", "e:/output5"};

        Configuration configuration = new Configuration();

        Job job = Job.getInstance(configuration);

        job.setJarByClass(OneIndexDriver.class);

        job.setMapperClass(OneIndexMapper.class);

        job.setReducerClass(OneIndexReducer.class);

        job.setMapOutputKeyClass(Text.class);

        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);

        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));

        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);

    }

}


第二次处理

TwoIndexMapper

public class TwoIndexMapper extends Mapper<LongWritable, Text, Text, Text> {

    Text k = new Text();

    Text v = new Text();

    @Override

    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // 1 获取一行

        String line = value.toString();

        // 2 切割

        String[] fields = line.split("--");

        // 3 封装

        k.set(fields[0]);

        v.set(fields[1]);

        // 4 写出

        context.write(k, v);

    }

}

TwoIndexReducer

public class TwoIndexReducer extends Reducer<Text, Text, Text, Text> {

    Text v = new Text();

    @Override

    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        // 1 拼接字符串

        StringBuffer sb = new StringBuffer();

        for (Text value : values) {

            sb.append(value.toString().replace("\t", "-->") + "\t");

        }

        v.set(sb.toString());

        // 2 写出

        context.write(key, v);

    }

}

TwoIndexDriver

public class TwoIndexDriver {

    public static void main(String[] args) throws Exception {

        args = new String[]{"e:/input/inputtwoindex", "e:/output6"};

        Configuration configuration = new Configuration();

        Job job = Job.getInstance(configuration);

        job.setJarByClass(TwoIndexDriver.class);

        job.setMapperClass(TwoIndexMapper.class);

        job.setReducerClass(TwoIndexReducer.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 result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);

    }

}

相关文章

  • 倒排索引案例(多Job串联)

    需求 需求分析 第一次处理 OneIndexMapper public class OneIndexMapper ...

  • IMI 倒排多索引

    倒排多索引 倒排多索引体现在倒排索引的的时候,使用PQ M=2来代替倒排的K-means,把整个数据集划分为两个子...

  • Elasticsearch(一):概念与基本API

    安装 Elasticsearch 常用 API index Document 倒排索引与分词 倒排索引 倒排索引与...

  • ElasticSearch(基础)

    1.1 倒排索引 倒排索引原理?? ElasticSearch使用一种称为 ==倒排索引== 的结构,它适用于快...

  • ElasticSearch 倒排索引简析

    内容概要 倒排索引是什么?为什么需要倒排索引? 倒排索引是怎么工作的? 1. 倒排索引是什么? 假设有一个交友网站...

  • 搜索引擎索引-倒排索引

    倒排索引基础 倒排索引示范 Elasticsearch中使用一种称为倒排索引的结构,适用于快速的全文搜索。一个倒排...

  • ElasticSearch知识库

    一、原理篇 Elasticsearch 的倒排索引是什么? 倒排索引=term字典+docId倒排表,term字典...

  • Elasticsearch学习笔记(06) - 倒排索引简介

    Elasticsearch的核心是基于倒排索引。因此,我们有必要了解一下倒排索引算法。 简单的例子 既然有倒排索引...

  • MapReduce 案例之倒排索引

    1. 倒排索引 倒排索引是文档检索系统中最常用的数据结构,被广泛地应用于全文搜索引擎。 它主要是用来存储某个单词(...

  • MapReduce 案例之倒排索引

    1. 倒排索引 倒排索引是文档检索系统中最常用的数据结构,被广泛地应用于全文搜索引擎。 它主要是用来存储某个单词(...

网友评论

      本文标题:倒排索引案例(多Job串联)

      本文链接:https://www.haomeiwen.com/subject/amaspqtx.html