倒序索引

倒排索引:

1.倒排索引也称为反向索引,是一种索引的状态
2.被用来存储,在全文中搜索某个单词在一个文件或一组文档中的映射
3.先查找单词或字符串或更具体的字符串,获取位置,然后通知单词出现的次数和字符串出现的次数

4.下面有3个文件:index.html page.html content.html
三个文件中有一些数据,通过倒排索引的方式统计单词在每个文件中出现的次数

例如:

hadoop index.html:3 ;page.html:1;content.html:1;
and page.html :1;

DescCombiner.java

import java.io.IOException;

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

/**
 * Description: 自定义Combiner处理map端的逻辑<br/>
 * Copyright (c) , 2018, xlj <br/>
 * This program is protected by copyright laws. <br/>
 * Program Name:DescCombiner.java <br/>
 * 
 * @version : 1.0
 */
public class DescCombiner extends Reducer<Text, Text, Text, Text>{
	@Override
	protected void reduce(Text key, Iterable<Text> values, Context context)
			throws IOException, InterruptedException {
		//Combiner处理的时候,Mapper端已经处理完成
		//这个位置产生的数据应该是这个格式的:
		//输出格式:hadoop index.html:1
		//<index.html_hadoop,list(1,1,1)>
		String[] keys = key.toString().split("_");
		//index.html,hadoop
		//定义变量存储单词的个数
		int count = 0;
		for(Text t: values) {
			count += Integer.parseInt(t.toString());
		}
		context.write(new Text(keys[1]), new Text(keys[0]+":"+count));
	}
}

DescSort.java

import java.io.IOException;
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.InputSplit;
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;
/**
 * Description: 倒排索引<br/>
 * Copyright (c) , 2018, xlj <br/>
 * This program is protected by copyright laws. <br/>
 * Program Name:DescSort.java <br/>
 * 
 * @version : 1.0
 */
public class DescSort {
	 //实现MapReduce
		/*1.实现Mapper端的逻辑
		 * KEYIN:文件中读取的偏移量-->LongWritable(固定的) 
		 * VALUEIN:文件中实际读取的内容-->Text
		 * KEYOUT:Mapper处理完成后传递给Reducer中的KEYIN的数据类型-->不固定,根据需求来
		 * VALUEOUT:Mapper端处理完成后传递给Reducer中的VALUEIN的数据类型-->不固定,根据需求来
		 */
		public static class MyMapper extends  Mapper<LongWritable, Text, Text, Text>{
			/* 进入Map处理逻辑之前会执行一次的方法
			 */
			@Override
			protected void setup(Context context)
					throws IOException, InterruptedException {
			}
			/*
			 * 需要实现Mapper端的处理逻辑
			 * key:是文件中数据的偏移量,数据类型是由泛型中定义得来的KEYIN
			 * value:是文件中实际的内容,数据类型是泛型中定义得来的VALUEIN
			 * context:将处理过后产生的KV,写成文件输出
			 */
			@Override
			protected void map(LongWritable key, Text value, Context context)
					throws IOException, InterruptedException {
				String line = value.toString();;
				//获取文件名字
				InputSplit  is = context.getInputSplit();
				String fileName = ((FileSplit)is).getPath().getName();
				//拆分数据
				String [] words = line.split(" ");
				for (String word : words) {
					//value的值可以确定是1,统计个数相加就行了
					//因为key只放单词不知道来源于哪个文件,放文件名又不知道有哪些单词
					//所以将两个进行拼接
					context.write(new Text(fileName+"_"+word), new Text(1+""));
				}
				
				//个例:
				/*
				 * index.html_hadoop,1
				 * index.html_hadoop,1
				 * page.html_hadoop,1
				 * page.html_is,1
				 */
			}
			/* 
			 * 在Map处理逻辑之后会执行一次,可以处理一些逻辑
			 */
			@Override
			protected void cleanup(Context context)
					throws IOException, InterruptedException {
				
			}
		}
		
		//实现Reducer端的逻辑
	   /*
	    * Reducer相当于对Mapper端处理过后的数据进行一个实际的处理业务
	    * KEYIN-->Mapper处理过后输出key的数据类型,由Mapper的泛型中第三个参数决定
	    * VALUE-->Mapper处理过后输出value的数据类型,由Mapper的泛型中第四个参数决定
	    * KEYOUT-->Reducer端处理完数据之后要写出key的数据类
	    * VALUEOUT-->Reducer处理完数据之后,要写出value的 数据类
	    */
		public static class MyReduce extends Reducer<Text, Text, Text, Text>{
			/* 执行Reducer端先执行一次的方法
			 */
			@Override
			protected void setup(Context context) throws IOException, InterruptedException {
				
			}
			
			/*
			 *reduce方法是处理业务的核心逻辑 
			 *key: 是从Mapper端处理完成后,产生key的数据
			 *values-->是从 Mapper端处理完成之后相同key的values的数据集合
			 *context-->写出实际 处理完成后KV的方法 
			 */
			@Override
			protected void reduce(Text key, Iterable<Text> values, Context context)
					throws IOException, InterruptedException {
				//因为在Mapper端实现了Combiner,所以数据是一种有序状态
				//<hadoop,list(index.html:3;page.html):1;content.html:1>
				//因为key已经是单词了,所以不需要做任何处理
				//只需要将values的值拼接打印即可
				String str = "";
				for (Text t : values) {
					str += t.toString();
				}
				context.write(new Text(key), new Text(str));
			}
			/* 
			 * 执行完reduce方法执行的方法
			 * 
			 */
			@Override
			protected void cleanup(Context context)
					throws IOException, InterruptedException {
				
			}
		}
		/**
		 * 实现job,完成作业配置
		 */
		public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
			//1.获取配置对象
			Configuration conf = new Configuration();
			//2.创建Job对象(创建作业)
			/*
			 * 这个方法一共有两个参数版本
			 * getInstance(conf) --------直接传入配置对象
			 * getInstance(conf,"WordCountCombiner")---传入配置对象和类的名字
			 */
			Job job = Job.getInstance(conf);
			//3.设置运行job的class
			job.setJarByClass(DescSort.class);
			
			//4.设置Mapper端的运行类和输出key,输出value的数据类型
			job.setMapperClass(MyMapper.class);
			job.setMapOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			
			//5.读取数据来源
			
			//这两个方法处理是一样的,只是最后一个参数不同,
			/*
			 * FileInputFormat.addInputPath(job, new Path("input/data1"));
			 * 
			 * add:证明只有 一个路径,
			 * 
			 * FileInputFormat.setInputPaths(job, new Path("input/data1"));
			 * set证明后面是可变参数,多个
			 * 
			 * 因为当前运行的是本地MR,所以数据是 从本地读取,若需要在集群中运行,这个位置的参数应该是args[0]
			 */
			//FileInputFormat.addInputPath(job, new Path("input/data2"));
			FileInputFormat.setInputPaths(job, new Path("input/data2"));
			
			//优化设置
			//一般可以写分区设置,多文件输出设置,Combiner设置
			/*
			 * 并不是所有job都适用于Combiner,只有操作满足结合规律才可以进行设置
			 * 如 求和,求最大值,topN 等可以使用Combiner
			 * 
			 * Combiner不一定需要存在,只有数据量较大,需要做优化的时候可以使用
			 */
			job.setCombinerClass(DescCombiner.class);
			
			//6.社会Reducer端的运行类和输出key和输出value的数据类型
			job.setReducerClass(MyReduce.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			
			//7.处理完文件之后输出的路径
			//ps:因为当前运行的是本地MR,所以数据是写到本地的,若需要再集群中运行,这个位置的参数应该是args[1]
			//数据是存储到HDFS中
			FileOutputFormat.setOutputPath(job, new Path("output1"));
			
			//8.提交作业
			int isok = job.waitForCompletion(true)?0:-1;
			System.exit(isok);
		}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值