• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >虚拟化 > Hadoop编程基于MR程序实现倒排索引示例

Hadoop编程基于MR程序实现倒排索引示例

作者:liuyazhuang 字体:[增加 减小] 来源:互联网

本文主要包含hadoop,倒排索引,hadoop,编程实例等服务器相关知识,liuyazhuang 希望可以进行参考

相信接触过搜索引擎开发的同学对倒排索引并不陌生,谷歌、百度等搜索引擎都是用的倒排索引,关于倒排索引的有关知识,这里就不再深入讲解,有兴趣的同学到网上了解一下。这篇博文就带着大家一起学习下如何利用Hadoop的MR程序来实现倒排索引的功能。

一、数据准备

1、输入文件数据

这里我们准备三个输入文件,分别如下所示

a.txt

hello tom 
hello jerry 
hello tom 

b.txt

hello jerry 
hello jerry 
tom jerry 

c.txt

hello jerry 
hello tom 

2、最终输出文件数据

最终输出文件的结果为:

[plain] view plain copy
hello  c.txt-->2 b.txt-->2 a.txt-->3  
jerry  c.txt-->1 b.txt-->3 a.txt-->1  
tom c.txt-->1 b.txt-->1 a.txt-->2  

二、倒排索引过程分析

根据输入文件数据和最终的输出文件结果可知,此程序需要利用两个MR实现,具体流程可总结归纳如下:

-------------第一步Mapper的输出结果格式如下:-------------------- 
context.wirte("hello->a.txt", "1") 
context.wirte("hello->a.txt", "1") 
context.wirte("hello->a.txt", "1") 
context.wirte("hello->b.txt", "1") 
context.wirte("hello->b.txt", "1") 
context.wirte("hello->c.txt", "1") 
context.wirte("hello->c.txt", "1") 
-------------第一步Reducer的得到的输入数据格式如下:------------- 
<"hello->a.txt", {1,1,1}> 
<"hello->b.txt", {1,1}> 
<"hello->c.txt", {1,1}> 
-------------第一步Reducer的输出数据格式如下--------------------- 
context.write("hello->a.txt", "3") 
context.write("hello->b.txt", "2") 
context.write("hello->c.txt", "2") 
-------------第二步Mapper得到的输入数据格式如下:----------------- 
context.write("hello->a.txt", "3") 
context.write("hello->b.txt", "2") 
context.write("hello->c.txt", "2") 
-------------第二步Mapper输出的数据格式如下:-------------------- 
context.write("hello", "a.txt->3") 
context.write("hello", "b.txt->2") 
context.write("hello", "c.txt->2") 
-------------第二步Reducer得到的输入数据格式如下:----------------- 
<"hello", {"a.txt->3", "b.txt->2", "c.txt->2"}> 
-------------第二步Reducer输出的数据格式如下:----------------- 
context.write("hello", "a.txt->3 b.txt->2 c.txt->2") 
最终结果为: 
hello  a.txt->3 b.txt->2 c.txt->2 

三、程序开发

3.1、第一步MR程序与输入输出

package com.lyz.hdfs.mr.ii; 
import java.io.IOException; 
import org.apache.commons.lang.StringUtils; 
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.input.FileSplit; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
/** 
 * 倒排索引第一步Map Reduce程序,此处程序将所有的Map/Reduce/Runner程序放在一个类中 
 * @author liuyazhuang 
 * 
 */ 
public class InverseIndexStepOne { 
  /** 
   * 完成倒排索引第一步的mapper程序 
   * @author liuyazhuang 
   * 
   */ 
  public static class StepOneMapper extends Mapper<LongWritable, Text, Text, LongWritable>{ 
    @Override 
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) 
        throws IOException, InterruptedException { 
      //获取一行数据 
      String line = value.toString(); 
      //切分出每个单词 
      String[] fields = StringUtils.split(line, " "); 
      //获取数据的切片信息 
      FileSplit fileSplit = (FileSplit) context.getInputSplit(); 
      //根据切片信息获取文件名称 
      String fileName = fileSplit.getPath().getName(); 
      for(String field : fields){ 
        context.write(new Text(field + "-->" + fileName), new LongWritable(1)); 
      } 
    } 
  } 
  /** 
   * 完成倒排索引第一步的Reducer程序 
   * 最终输出结果为: 
   * hello-->a.txt  3 
    hello-->b.txt  2 
    hello-->c.txt  2 
    jerry-->a.txt  1 
    jerry-->b.txt  3 
    jerry-->c.txt  1 
    tom-->a.txt 2 
    tom-->b.txt 1 
    tom-->c.txt 1 
   * @author liuyazhuang 
   * 
   */ 
  public static class StepOneReducer extends Reducer<Text, LongWritable, Text, LongWritable>{ 
    @Override 
    protected void reduce(Text key, Iterable<LongWritable> values, 
        Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException { 
      long counter = 0; 
      for(LongWritable value : values){ 
        counter += value.get(); 
      } 
      context.write(key, new LongWritable(counter)); 
    } 
  } 
  //运行第一步的MR程序 
  public static void main(String[] args) throws Exception{ 
    Configuration conf = new Configuration(); 
    Job job = Job.getInstance(conf); 
    job.setJarByClass(InverseIndexStepOne.class); 
    job.setMapperClass(StepOneMapper.class); 
    job.setReducerClass(StepOneReducer.class); 
    job.setMapOutputKeyClass(Text.class); 
    job.setMapOutputValueClass(LongWritable.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(LongWritable.class); 
    FileInputFormat.addInputPath(job, new Path("D:/hadoop_data/ii")); 
    FileOutputFormat.setOutputPath(job, new Path("D:/hadoop_data/ii/result")); 
    job.waitForCompletion(true); 
  } 
} 

3.1.1 输入数据

a.txt

hello tom 
hello jerry 
hello tom 

b.txt

hello jerry 
hello jerry 
tom jerry 

c.txt

hello jerry 
hello tom 

3.1.2

输出结果:

hello-->a.txt  3 
hello-->b.txt  2 
hello-->c.txt  2 
jerry-->a.txt  1 
jerry-->b.txt  3 
jerry-->c.txt  1 
tom-->a.txt 2 
tom-->b.txt 1 
tom-->c.txt 1 

3.2 第二步MR程序与输入输出

package com.lyz.hdfs.mr.ii; 
import java.io.IOException; 
import org.apache.commons.lang.StringUtils; 
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; 
/** 
 * 倒排索引第二步Map Reduce程序,此处程序将所有的Map/Reduce/Runner程序放在一个类中 
 * @author liuyazhuang 
 * 
 */ 
public class InverseIndexStepTwo { 
  /** 
   * 完成倒排索引第二步的mapper程序 
   * 
   * 从第一步MR程序中得到的输入信息为: 
   * hello-->a.txt  3 
    hello-->b.txt  2 
    hello-->c.txt  2 
    jerry-->a.txt  1 
    jerry-->b.txt  3 
    jerry-->c.txt  1 
    tom-->a.txt 2 
    tom-->b.txt 1 
    tom-->c.txt 1 
   * @author liuyazhuang
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • myeclipse中建web项目上传文件到hadoop,出现文件上传上去了,但是文件大小为0或小于当前文件解决方案,
  • 为什么出现Hadoop,出现Hadoop
  • 虚拟机下Linux系统Hadoop单机/伪分布式配置:Hadoop2.5.2+Ubuntu14.04(半原创),hadoophadoop2.5.2
  • 深入讲解Hadoop管道,讲解hadoop管道
  • Hadoop安全性,hadoop安全机制
  • Hadoop Streaming 获取mapreduce_map_input_file遇到的版本问题,hadoopmapreduce
  • 【甘道夫】Apache Hadoop 2.5.0-cdh5.2.0 HDFS Quotas 配额控制,hadoophdfs
  • Hadoop 2.x与3.x 22点比较,Hadoop 3.x比2.x的改进
  • ubuntu docker搭建Hadoop集群环境的方法
  • hadoop动态增加和删除节点方法介绍

相关文章

  • VMware Workstation如何创建加密虚拟机
  • 详解Openstack组件部署 — Overview和前期环境准备
  • Docker 教程之镜像创建及修改详细介绍
  • docker利用selenium+testng实现web自动化的方法
  • 使用Docker compose编排Laravel应用的方法
  • Ubuntu虚拟机多网卡配置
  • Docker中Dockerfile之容器中运行MyEclipse搭建的JavaWeb项目
  • vmware克隆Centos6.4虚拟机网卡无法启动问题的解决方法
  • Docker 教程之私有仓库详解
  • CentOS 7下YUM 本地仓库的搭建详细步骤

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • VMware安装Centos7超详细过程(图文)
    • Windows7 64位 旗舰版下VirtualBox 4.3.12安装教程
    • MacBookPro下docker的安装与使用教程
    • docker cgroup 资源监控的详解
    • 基于Kubernetes和Docke实现留言簿案例
    • Openstack 创建项目和虚拟机详细介绍
    • 构建一个简单的CaaS系统
    • 浅谈Docker基础之数据管理
    • php redis扩展支持scan命令实现方法
    • VMware workstation 9安装配置图文教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有