hadoop 倒排索引

倒排索引是文檔檢索系統中最常用的數據結構,被廣泛的應用於全文搜索引擎。它主要用來存儲某個單詞(或詞組),在一個文檔或一組文檔中的存儲位置的映射,即提供了一種根據內容來查找文檔的方式,由於不是根據文檔來確定文檔所包含的內容,而是進行了相反的操作,因而被稱爲倒排索引。
假設在inversed.files中有file1,file2,file3文件,其內容分別如下:
file1:
dog cat
dog rabbit
tiger mice
goose chicken
rabbit fox
file2:
tiger donkey
lion fish
duck wolf
dog bird
cat bear
file3:
snake pig
lion
cat
elephant
則進行倒排索引之後,其結果爲:

bear file2:1 ,

bird file2:1 ,

cat file2:1 ,file1:1 ,file3:1 ,

chicken file1:1 ,

dog file1:2 ,file2:1 ,duck file2:1 ,dungkey file2:1 ,elephant file3:1 ,fish file2:1 ,fox file1:1 ,goose file1:1 ,lion file2:1 ,file3:1 ,mice file1:1 ,pig file3:1 ,rabbit file1:2 ,snake file3:1 ,tiger file2:1 ,file1:1 ,wolf file2:1 ,
import java.io.IOException;
import java.util.StringTokenizer;

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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class InversedIndex {
	
	/**
	 * 將輸入文件拆分,
	 * 將關鍵字和關鍵字所在的文件名作爲map的key輸出,
	 * 該組合的頻率作爲value輸出
	 * */
	
	public static class InversedIndexMapper extends Mapper<Object, Text, Text, Text> {
		
		private Text outKey = new Text();
		private Text outVal = new Text();
		
		@Override
		public void map (Object key,Text value,Context context) {
			StringTokenizer tokens = new StringTokenizer(value.toString());
			FileSplit split = (FileSplit) context.getInputSplit();
			while(tokens.hasMoreTokens()) {
				String token = tokens.nextToken();
				try {
					outKey.set(token + ":" + split.getPath());
					outVal.set("1");
					context.write(outKey, outVal);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * map的輸出進入到combiner階段,此時來自同一個文件的相同關鍵字進行一次reduce處理,
	 * 將輸入的key拆分成關鍵字和文件名,然後關鍵字作爲輸出key,
	 * 將文件名與詞頻拼接,作爲輸出value,
	 * 這樣就形成了一個關鍵字,在某一文件中出現的頻率的 key--value 對
	 * */
	public static class InversedIndexCombiner extends Reducer<Text, Text, Text, Text> {
		
		private Text outKey = new Text();
		private Text outVal = new Text();
		
		@Override
		public void reduce(Text key,Iterable<Text> values,Context context) {
			String[] keys = key.toString().split(":");
			int sum = 0;
			for(Text val : values) {
				sum += Integer.parseInt(val.toString());
			}
			try {
				outKey.set(keys[0]);
				int index = keys[keys.length-1].lastIndexOf('/');
				outVal.set(keys[keys.length-1].substring(index+1) + ":" + sum);
				context.write(outKey, outVal);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 將combiner後的key value對進行reduce,
	 * 由於combiner之後,一個關鍵字可能對應了多個value,故需要將這些value進行合併輸出
	 * */
	
	public static class InversedIndexReducer extends Reducer<Text, Text, Text, Text> {
		
		@Override
		public void reduce (Text key,Iterable<Text> values,Context context) {
			StringBuffer sb = new StringBuffer();
			for(Text text : values) {
				sb.append(text.toString() + " ,");
			}
			try {
				context.write(key, new Text(sb.toString()));
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		Job job = new Job(conf,"index inversed");
		
		job.setJarByClass(InversedIndex.class);
		job.setMapperClass(InversedIndexMapper.class);
		job.setCombinerClass(InversedIndexCombiner.class);
		job.setReducerClass(InversedIndexReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		FileInputFormat.addInputPath(job, new Path("inversed.files"));
		FileOutputFormat.setOutputPath(job, new Path("inversed.result"));
		
		System.exit(job.waitForCompletion(true)?0:1);
		
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章