需求: 爲a, b, c 3個文本文件中的單詞建倒排索引java
輸出格式: <word,"a:2,b:3,c:1">apache
a:app
hello world
hello hadoop
hello worldide
b:oop
spark hadoop
hello hadoop
world hadoop測試
c:ui
spark world
hello world
hello sparkspa
map階段code
context.write("hello:a","1") context.write("hello:a","1") context.write("hello:a","1")
map階段輸出: <"hello:a",{1,1,1}>orm
combine階段
context.write("hello","a:3"); context.write("hello","b:1"); context.write("hello","c:2");
combine階段輸出: <"hello",{"a:3","b:1","c:2"}>
reduce階段
context.write("hello","a:3,b:1,c:2");
reduce階段輸出: <"hello","a:3,b:1,c:2">
定義Mapper類, 該類繼承org.apache.hadoop.mapreduce.Mapper
並重寫map()方法
public class IIMapper extends Mapper<LongWritable, Text, Text, Text> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = StringUtils.split(line, " "); // 從context中獲取文件切片inputSplit FileSplit inputSplit = (FileSplit) context.getInputSplit(); // 從inputSplit中獲取文件的絕對路徑path String path = inputSplit.getPath().toString(); int index = path.lastIndexOf("/"); // 從path中截取文件名 String fileName = path.substring(index + 1); for (String word : words) { context.write(new Text(word + ":" + fileName), new Text("1")); } // map輸出結果 <"hello:a",{1,1,1}> } }
定義Combiner類, 該類繼承org.apache.hadoop.mapreduce.Reducer
combine階段是map階段和reduce階段的中間過程
並重寫reduce()方法
public class IICombiner extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String[] data = key.toString().split(":"); String word = data[0]; String fileName = data[1]; int count = 0; for (Text value : values) { count += Integer.parseInt(value.toString()); } context.write(new Text(word), new Text(fileName + ":" + count)); // combine輸出結果 <"hello",{"a:3","b:1","c:2"}> } }
定義Reducer類, 該類繼承org.apache.hadoop.mapreduce.Reducer
並重寫reduce()方法
public class IIReducer extends Reducer<Text, Text, Text, Text> { @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { StringBuilder sb = new StringBuilder(); for (Text value : values) { sb.append(value.toString() + "\t"); } context.write(key, new Text(sb.toString())); // reduce輸出結果 <"hello","a:3,b:1,c:2"> } }
測試倒排索引
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job job = Job.getInstance(new Configuration()); job.setJarByClass(InverseIndexRunner.class); // 設置job的主類 job.setMapperClass(IIMapper.class); // 設置Mapper類 job.setCombinerClass(IICombiner.class); // 設置Combiner類 job.setReducerClass(IIReducer.class); // 設置Reducer類 job.setMapOutputKeyClass(Text.class); // 設置map階段輸出Key的類型 job.setMapOutputValueClass(Text.class); // 設置map階段輸出Value的類型 job.setOutputKeyClass(Text.class); // 設置reduce階段輸出Key的類型 job.setOutputValueClass(Text.class); // 設置reduce階段輸出Value的類型 // 設置job輸入路徑(從main方法參數args中獲取) FileInputFormat.setInputPaths(job, new Path(args[0])); // 設置job輸出路徑(從main方法參數args中獲取) FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); // 提交job }
job輸出的結果文件:
hadoop a:1 b:3 hello b:1 c:2 a:3 spark b:1 c:2 world c:2 b:1 a:2