MapReduce:Index索引案例

news2024/10/7 8:26:52

案例需求

a.html
hello world
hello lucy
hello jack
hello liuyan

b.html
hello aaa
aaa bbb
bbb ccc
hello liuyan 
liuyan  tangyan

c.html
world hello 
liuyan tangyan
tangyan aaa
bbb    ccc


计算每个单词在每个文件中出现的次数 
aaa    b.html-2 c.html-1 
bbb    b.html-2 c.html-1 
ccc    b.html-1 
hello    a.html-4 b.html-2 c.html-1 
jack    a.html-1 
liuyan    b.html-2 c.html-1 
lucy    a.html-1 
tangyan    c.html-2 b.html-1 
world    a.html-1 

 

 

需求分析

如图所示,需两次mapreduce

 

 代码

 

package day36.com.doit.demo3;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;

import java.io.IOException;

public class Test {

    private static class IndexMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
        private String fileName;
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit)context.getInputSplit();
             fileName = fileSplit.getPath().getName();
        }

        Text k2 = new Text();
        IntWritable v2 = new IntWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] arr = value.toString().split("\\s+");
            for (String s : arr) {
                k2.set(s+"-"+fileName);
                v2.set(1);
                context.write(k2,v2);
            }



        }
    }


    private static class IndexReducer extends Reducer<Text,IntWritable,Text,IntWritable> {

        IntWritable v3 = new IntWritable();
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            int sum = 0;
            for (IntWritable value : values) {
                sum+=value.get();
            }
            v3.set(sum);

            context.write(key,v3);
        }
    }

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

        //创建任务
        Job job = Job.getInstance(conf, "index");
        //设置Mapper类
        job.setMapperClass(IndexMapper.class);
        //设置Reduce类
        job.setReducerClass(IndexReducer.class);
        //设置map的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //设置reduce的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置输入文件位置
        FileInputFormat.setInputPaths(job,new Path("d:\\ideawork\\bbb\\input"));
        //设置输出文件位置
        FileOutputFormat.setOutputPath(job,new Path("d:\\ideawork\\bbb\\output2"));

        //将任务提交 并等待完成
        job.waitForCompletion(true);
    }
}

 

 将第一次输出结果作为第二次的输入

package day36.com.doit.demo3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test2 {

    private static class IndexMapper extends Mapper<LongWritable, Text,Text,Text> {
        Text k2 =new Text();
        Text v2 = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split("-");
            k2.set(arr[0]);
            v2.set(arr[1].replaceAll("\\s+","-"));
            context.write(k2,v2);

        }
    }

    private static class IndexReducer extends Reducer<Text,Text,Text,Text> {
        Text v3 = new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> list = new ArrayList<>();


            StringBuilder sb = new StringBuilder();
            for (Text value : values) {
//                sb.append(value.toString()).append(" ");
                list.add(value.toString());

            }
            Collections.sort(list, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return  o2.split("-")[1].compareTo(o1.split("-")[1]);
                }
            });
//            v3.set(sb.toString());
            for (String s : list) {
                sb.append(" "+s);
                v3.set(sb.toString());

            }
            context.write(key,v3);

        }
    }

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

        //创建任务
        Job job = Job.getInstance(conf, "index");
        //设置Mapper类
        job.setMapperClass(IndexMapper.class);
        //设置Reduce类
        job.setReducerClass(IndexReducer.class);
        //设置map的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //设置reduce的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        //设置输入文件位置
        FileInputFormat.setInputPaths(job,new Path("d:\\ideawork\\bbb\\output2"));
        //设置输出文件位置
        FileOutputFormat.setOutputPath(job,new Path("d:\\ideawork\\bbb\\output2_6"));

        //将任务提交 并等待完成
        job.waitForCompletion(true);
    }

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/529600.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文本三剑客awk

awk 工作原理&#xff1a; 逐行读取文本&#xff0c;默认以空格或tab键为分隔符进行分隔&#xff0c;将分隔所得的各个字段保存到内建变量中&#xff0c;并按模式或者条件执行编辑命令。 sed命令常用于一整行的处理&#xff0c;而awk比较倾向于将一行分成多个“字段”然后再进…

Visual Studio Code 插件的开发、调试及发布完整详细教程

本篇文章主要讲解:Vscode的拓展插件,从环境安装到生成项目文件再到调试及部署发布的完整开发教程。 日期:2023年5月10日 vscode 1.78.1 一、准备node环境及安装yo 项目初始化,优先安装yo、再通过yo创建code及插件项目。 基础条件 需要先安装node,且node环境已经正确安装…

5.项目管理(测试)工具

目录 一、禅道 1.为什么需要禅道&#xff1f;&#xff08;仅从测试角度看&#xff09; 2.管理工具的对比 3.禅道的设计理念 4.软件开发的生命周期有哪些&#xff1f;&#xff08;软件开发的流程&#xff09; 二、selenium 1、什么是自动化测试&#xff1f; 2.UI自动化的…

2023年,最新linux c/c++后台开发学习路线分享

摘自零声教育课程大纲&#xff0c;8个技术维度项目实战&#xff0c;为你打通linux c/c后台开发的技术栈。 部分往期视频 c八股文重点&#xff0c;网络的posix api实现原理 8个方面讲解io_uring&#xff0c;重塑对异步io的理解 c后端开发中数据库异步连接、异步连接池的原理…

浅聊一下PTP

浅聊一下ptp 最近做了点时间同步相关工作&#xff0c;浅浅聊一下学习和了解的东西吧。 大概了解了一下PTP&#xff0c;NTP&#xff0c;PTPD&#xff0c;LinuxPTP 1.时钟同步 网络时钟同步包括频率同步和相位同步两个概念。 频率同步&#xff08;Frequency synchronization…

Hudi集成Spark与hudi表的创建

集成Hudi包 Spark-shell 方式启动 Spark SQL方式启动 SQL方式创建hudi建表 集成Hudi包 以下为hudi与spark版本的兼容 Hudi Supported Spark 3 version 0.12.x 3.3.x&#xff0c;3.2.x&#xff0c;3.1.x 0.11.x 3.2.x&#xff08;default build, Spark bundle only&…

浪漫行星,不浪漫你打我

先上效果图&#xff1a; 再上代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>import "https://fonts.googleapis.com/css2?familyMegrim&…

tftp文件传输协议报文解析

1&#xff0c;接着上一篇描述bootp,我们现在了解一下tftp协议。 先来看看tftp的五种报文格式 操作码补充说明&#xff1a; 读文件请求包&#xff1a;Read request&#xff0c;简写为RRQ&#xff0c;对应Opcode字段值为1 写文件请求包&#xff1a;Write requst&#xff0c;简…

练习SSM微博项目

微博项目 项目概述 ​ 该项目是一款社交媒体应用&#xff0c;用户可以在平台上发表短文、图片等信息&#xff0c;分享自己的想法、心情和生活。微博的用户群体广泛&#xff0c;包括个人、娱乐明星、公司、政府官方等。 项目功能 用户管理 用户可以注册微博账号&#xff0c;登…

​ES elasticsearch-analysis-dynamic-synonym​连接数据库动态更新synonym近义词

前言 在很多搜索场景中&#xff0c;我们希望能够搜索出搜索词相关的目标&#xff0c;同时也希望能搜索出其近义词相关的目标。例如在商品搜索中&#xff0c;搜索“瓠瓜”&#xff0c;也希望能够搜索出“西葫芦”&#xff0c;但“西葫芦”商品名称因不含有“瓠瓜”&#xff0c;导…

今年流行的人工智能AI 人脸生成器,你懂的。

使用人工智能 (AI) 继续革新我们与技术和环境互动的方式。AI 最令人兴奋的应用之一是人脸生成器&#xff0c;它可以根据输入数据创建逼真、高质量的图像。 在这篇博文中&#xff0c;我们将讨论当今可用的 10 大AI 人脸生成器&#xff0c;探索它们的功能和性能&#xff0c;并发…

DDD重构中台业务

大家好&#xff0c;我是易安&#xff01;今天我们谈一谈如何使用DDD重构中台业务。 DDD有两把利器&#xff0c;那就是它的战略设计和战术设计方法。中台在企业架构上更多偏向业务模型&#xff0c;形成中台的过程实际上也是业务领域不断细分的过程。在这个过程中我们会将同类通用…

selenium+Java环境搭建

目录 ①下载Chrome浏览器并查看浏览器版本 ②下载解压Chrome浏览器驱动 ③配置Java环境 ④将驱动文件放到jdk的bin文件目录下 ⑤验证环境是否搭建成功 1、创建java&#xff08;Maven&#xff09;项目&#xff0c;在pom.xml中添加依赖 2、在java文件创建Main类 &am…

[刷题]贪心入门

文章目录 贪心区间问题区间选点区间合并区间覆盖 哈夫曼树&#xff08;堆&#xff09;合并果子 排序不等式排队打水 绝对值不等式货仓选址 推出来的不等式耍杂技的牛 以前的题 贪心 贪心&#xff1a;每一步行动总是按某种指标选取最优的操作来进行&#xff0c; 该指标只看眼前&…

SpringBoot 整合ElasticSearch实现模糊查询,批量CRUD,排序,分页,高亮

准备工作 准备一个空的SpringBoot项目 写入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>注意你的SpringBoot和你的es版本&#xff0…

都说测试行业饱和了,为啥我们公司给初级测试还能开到了12K?

故事起因&#xff1a; 最近我有个刚毕业的学生问我说&#xff1a;我感觉现在测试行业已经饱和了&#xff0c;也不是说饱和了&#xff0c;是初级的测试根本就没有公司要&#xff0c;哪怕你不要工资也没公司要你&#xff0c;测试刚学出来&#xff0c;没有任何的项目经验和工作经验…

2023年门店管理系统如何选?简单好用的门店管理系统有哪些?

开单收银效率低、商品管理混乱、记账对账耗时耗力还易出错...... 是我们在进行门店管理过程中常见的问题。 为了改善门店管理遇到的这几大问题&#xff0c;提高门店管理效率&#xff0c;越来越多的门店开始使用门店管理系统。 但如何选择简单实用、性价比高的门店管理系统&…

肠道核心菌属——Lachnoclostridium

谷禾健康 Lachnoclostridium属是一类革兰氏阳性菌&#xff0c;专性厌氧、形成孢子、属于Clostridiales目、Lachnospiraceae科、Firmicutes门。该属最初被描述为Clostridium phytofermentans&#xff0c;后来被重新分类为Lachnoclostridium属。 Lachnoclostridium属包括来自Lach…

00后真的是内卷王中王,真的想离职了....

都说00后躺平了&#xff0c;但是有一说一&#xff0c;该卷的还是卷。这不&#xff0c;前段时间我们公司来了个00年的&#xff0c;工作没两年&#xff0c;跳槽到我们公司起薪18K&#xff0c;都快接近我了。后来才知道人家是个卷王&#xff0c;从早干到晚就差搬张床到工位睡觉了。…

微服务通讯:gRPC入门教程

微服务通讯&#xff1a;个、RPC入门教程 gRPC是一个RPC框架&#xff0c;用于服务器之间服务的相互通讯&#xff0c;常见微服务项目开发中。市面上的RPC有很多&#xff0c;例如&#xff1a;dubbo、SpringCloud底层封装的等 1 概念 1.1 gRPC gRPC是一个高性能、开源的通用RPC&am…