MapReduce练习-----倒排索引

[plain]  view plain  copy
  1. 数据1:  
  2. huangbo love xuzheng  
  3. huangxiaoming love baby huangxiaoming love mimi  
  4. liangchaowei love liujialing  
  5.   
  6. 数据2:  
  7. hello huangbo  
  8. hello xuzheng  
  9. hello huangxiaoming  

题目一:编写 MapReduce 求出以下格式的结果数据:统计每个关键词在每个文档中当中的第几行出现了多少次。
例如,huangxiaoming 关键词的格式:

huangixaoming  mapreduce-4-1.txt:2,2; mapreduce-4-1.txt:4,1;mapreduce-4-2.txt:3,1

首先是进行文件的额切分,拼接添加行号,以单词为key,文件名和行号进行拼接做为value,然后通过第二个MapRudece程序将数据组合成我们需要的。样式。

[java]  view plain  copy
  1. 第一个MapReduce程序  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.FileSystem;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.LongWritable;  
  10. import org.apache.hadoop.io.NullWritable;  
  11. import org.apache.hadoop.io.Text;  
  12. import org.apache.hadoop.mapreduce.Job;  
  13. import org.apache.hadoop.mapreduce.Mapper;  
  14. import org.apache.hadoop.mapreduce.Reducer;  
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  16. import org.apache.hadoop.mapreduce.lib.input.FileSplit;  
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  18.   
  19. public class Question3_1_1 {  
  20.         public static class MRMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
  21.   
  22.             Text k = new Text();  
  23.             IntWritable v = new IntWritable(1);  
  24.             int num = 0;  
  25.   
  26.             @Override  
  27.             protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  28.   
  29.                 String line = value.toString();  
  30.                 //行号  
  31.                 num++;  
  32.   
  33.                 String[] words = line.split(" ");  
  34.   
  35.                 //huangixaoming  mapreduce-4-1.txt:2,2; mapreduce-4-1.txt:4,1;mapreduce-4-2.txt:3,1  
  36.                 FileSplit inputSplit = (FileSplit) context.getInputSplit();  
  37.                 //通过切片获取文件的名称  
  38.                 String fileName = inputSplit.getPath().getName();  
  39.                 for (String word : words) {  
  40.                     //单词+文件名+行号  作为key输出  
  41.                     k.set(word + ":" + fileName+ ":" + (num));  
  42.                     System.out.println(word + "--" + fileName+ "--" + (num));  
  43.                     context.write(k, v);  
  44.   
  45.                 }  
  46.                   
  47.             }  
  48.   
  49.         }  
  50.   
  51.         public static class MRReducer extends Reducer<Text, IntWritable, Text, NullWritable> {  
  52.   
  53.             Text t = new Text();  
  54.             @Override  
  55.             protected void reduce(Text key, Iterable<IntWritable> values, Context context)   
  56.                     throws IOException, InterruptedException {  
  57.                 //获取到key 单词+文件名+行号;  
  58.                 //根据key相同,进行累加相同的word出现了几次  
  59.                 int count = 0;  
  60.                 for (IntWritable value : values) {  
  61.   
  62.                     count += value.get();  
  63.                 }  
  64.                   
  65.                 //转化输出  
  66.                 t.set(key.toString()+","+count);  
  67.                 context.write(t,NullWritable.get());  
  68.   
  69.             }  
  70.   
  71.         }  
  72.   
  73.         public static void main(String[] args) throws Exception {  
  74.   
  75.             Configuration conf = new Configuration();  
  76.             FileSystem fs = FileSystem.get(conf);  
  77.             Job job = Job.getInstance(conf);  
  78.               
  79.             job.setJarByClass(Question3_1_1.class);  
  80.   
  81.             job.setMapOutputKeyClass(Text.class);  
  82.             job.setMapOutputValueClass(IntWritable.class);  
  83.             job.setOutputKeyClass(Text.class);  
  84.             job.setOutputValueClass(NullWritable.class);  
  85.   
  86.             FileInputFormat.setInputPaths(job, new Path("G:/test/q3/input"));  
  87.               
  88.             if(fs.exists(new Path("G:/test/q3/output_3_1"))){  
  89.                 fs.delete(new Path("G:/test/q3/output_3_1"), true);  
  90.             }  
  91.             FileOutputFormat.setOutputPath(job, new Path("G:/test/q3/output_3_1"));  
  92.   
  93.             job.setMapperClass(MRMapper.class);  
  94.             job.setReducerClass(MRReducer.class);  
  95.   
  96.             System.exit(job.waitForCompletion(true) ? 1:0);  
  97.   
  98.         }  
  99.   
  100.   
  101. }  

第二个MapReduce程序

[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.LongWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.Job;  
  9. import org.apache.hadoop.mapreduce.Mapper;  
  10. import org.apache.hadoop.mapreduce.Reducer;  
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  13.   
  14. public class Question3_1_2 {  
  15.       
  16.     public static class MRMapper extends Mapper<LongWritable, Text, Text, Text>{  
  17.         @Override  
  18.         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
  19.             String line = value.toString();  
  20.             String[] files = line.split(":");  
  21.             //k.set(word + ":" + fileName+ ":" + (num));  
  22.             //baby:mapreduce-4-1.txt:2,1  
  23.             String str = files[1]+":"+files[2];  
  24.             context.write(new Text(files[0]), new Text(str));  
  25.         }  
  26.     }  
  27.       
  28.     public static class MRReducer extends Reducer<Text, Text, Text, Text>{  
  29.         @Override  
  30.         protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {  
  31.             StringBuffer sb = new StringBuffer();  
  32.             for (Text text : values) {  
  33.                 sb.append(text.toString()+";");  
  34.             }  
  35.             context.write(key, new Text(sb.toString()));  
  36.         }  
  37.     }  
  38.       
  39.     public static void main(String[] args) throws Exception {  
  40.           
  41.         Configuration conf = new Configuration();  
  42.         FileSystem fs = FileSystem.get(conf);  
  43.         Job job = Job.getInstance(conf);  
  44.         job.setJarByClass(Question3_1_2.class);  
  45.           
  46.         job.setMapperClass(MRMapper.class);  
  47.         job.setReducerClass(MRReducer.class);  
  48.         job.setOutputKeyClass(Text.class);  
  49.         job.setOutputValueClass(Text.class);  
  50.           
  51.         FileInputFormat.setInputPaths(job, new Path("G:/test/q3/output_3_1"));  
  52.           
  53.         if(fs.exists(new Path("G:/test/q3/output_3_2"))){  
  54.             fs.delete(new Path("G:/test/q3/output_3_2"), true);  
  55.         }  
  56.         FileOutputFormat.setOutputPath(job, new Path("G:/test/q3/output_3_2"));  
  57.           
  58.         System.exit(job.waitForCompletion(true) ? 1:0);  
  59.     }  
  60.   
  61.   
  62. }  
题目二:编写 MapReduce 程序求出每个关键词在每个文档出现了多少次,并且按照出现次数降序排序。
例如:
huangixaoming  mapreduce-4-1.txt,3;mapreduce-4-2.txt,1
以上答案的含义:
关键词 huangxiaoming 在第一份文档 mapreduce-4-1.txt 中出现了 3 次,在第二份文档mapreduce-4-2.txt 中出现了 1 次。

方案:先统计出每个关键词在某个文件中的出现次数,然后再进行排序。

[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.NullWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.input.FileSplit;  
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  16.   
  17. public class Question3_2_1 {  
  18.         public static class MRMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
  19.   
  20.             Text k = new Text();  
  21.             IntWritable v = new IntWritable(1);  
  22.   
  23.             @Override  
  24.             protected void map(LongWritable key, Text value, Context context)   
  25.                     throws IOException, InterruptedException {  
  26.   
  27.                 String line = value.toString();  
  28.   
  29.                 String[] words = line.split(" ");  
  30.   
  31.                 //huangixaoming  mapreduce-4-1.txt:2,2; mapreduce-4-1.txt:4,1;mapreduce-4-2.txt:3,1  
  32.                 FileSplit inputSplit = (FileSplit) context.getInputSplit();  
  33.                 String fileName = inputSplit.getPath().getName();  
  34.                 for (String word : words) {  
  35.                     k.set(word + ":" + fileName);  
  36.                     context.write(k, v);  
  37.   
  38.                 }  
  39.                   
  40.             }  
  41.   
  42.         }  
  43.   
  44.         public static class MRReducer extends Reducer<Text, IntWritable, Text, NullWritable> {  
  45.   
  46.             Text t = new Text();  
  47.             @Override  
  48.             protected void reduce(Text key, Iterable<IntWritable> values, Context context)   
  49.                     throws IOException, InterruptedException {  
  50.   
  51.                 int count = 0;  
  52.                 for (IntWritable value : values) {  
  53.   
  54.                     count += value.get();  
  55.                 }  
  56.                 t.set(key.toString()+","+count);  
  57.                 context.write(t,NullWritable.get());  
  58.   
  59.             }  
  60.   
  61.         }  
  62.   
  63.         public static void main(String[] args) throws Exception {  
  64.   
  65.             Configuration conf = new Configuration();  
  66.             FileSystem fs = FileSystem.get(conf);  
  67.             Job job = Job.getInstance(conf);  
  68.               
  69.             job.setJarByClass(Question3_2_1.class);  
  70.   
  71.             job.setMapOutputKeyClass(Text.class);  
  72.             job.setMapOutputValueClass(IntWritable.class);  
  73.             job.setOutputKeyClass(Text.class);  
  74.             job.setOutputValueClass(NullWritable.class);  
  75.   
  76.             FileInputFormat.setInputPaths(job, new Path("G:/test/q3/input"));  
  77.               
  78.             if(fs.exists(new Path("G:/test/q3/output_3_3"))){  
  79.                 fs.delete(new Path("G:/test/q3/output_3_3"), true);  
  80.             }  
  81.             FileOutputFormat.setOutputPath(job, new Path("G:/test/q3/output_3_3"));  
  82.   
  83.             job.setMapperClass(MRMapper.class);  
  84.             job.setReducerClass(MRReducer.class);  
  85.   
  86.             System.exit(job.waitForCompletion(true) ? 1:0);  
  87.   
  88.         }  
  89.   
  90.   
  91. }  
使用自定义对象,将上面的结果组合成一个自定义对象,然后根据关键词分组,根据出现次数排序;
[java]  view plain  copy
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.NullWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15.   
  16. public class Question3_2_2 {  
  17.     //huangixaoming  mapreduce-4-1.txt,3;mapreduce-4-2.txt,1  
  18.     //yangmi:mapreduce-4-1.txt,1  
  19.     public static class MRMapper extends Mapper<LongWritable, Text, TestBean, NullWritable>{  
  20.         @Override  
  21.         protected void map(LongWritable key, Text value, Context context)   
  22.                 throws IOException, InterruptedException {  
  23.               
  24.             String[] line =  value.toString().split(":");  
  25.             TestBean tb = new TestBean(line[0],line[1].split(",")[0],Integer.parseInt(line[1].split(",")[1]));  
  26.             context.write(tb,NullWritable.get());  
  27.         }  
  28.     }  
  29.       
  30.     public static class MRReducer extends Reducer<TestBean, NullWritable, Text, Text>{  
  31.         Text k = new Text();  
  32.         Text v = new Text();  
  33.         @Override  
  34.         protected void reduce(TestBean key, Iterable<NullWritable> values, Context context)  
  35.                 throws IOException, InterruptedException {  
  36.               
  37.             StringBuffer sb = new StringBuffer();  
  38.             for (NullWritable nv : values) {  
  39.                 sb.append(key.getFileName()+","+key.getNum()+";");  
  40.             }  
  41.             k.set(key.getName());  
  42.             v.set(sb.toString());  
  43.             context.write(k, v);  
  44.         }  
  45.     }  
  46.       
  47.     public static void main(String[] args) throws Exception {  
  48.           
  49.         Configuration conf = new Configuration();  
  50.         FileSystem fs = FileSystem.get(conf);  
  51.         Job job = Job.getInstance(conf);  
  52.         job.setJarByClass(Question3_2_2.class);  
  53.           
  54.         job.setMapperClass(MRMapper.class);  
  55.         job.setReducerClass(MRReducer.class);  
  56.         job.setMapOutputKeyClass(TestBean.class);  
  57.         job.setMapOutputValueClass(NullWritable.class);  
  58.         job.setOutputKeyClass(Text.class);  
  59.         job.setOutputValueClass(Text.class);  
  60.         job.setGroupingComparatorClass(UserGC.class);  
  61.           
  62.         FileInputFormat.setInputPaths(job, new Path("G:/test/q3/output_3_3"));  
  63.           
  64.         if(fs.exists(new Path("G:/test/q3/output_3_4"))){  
  65.             fs.delete(new Path("G:/test/q3/output_3_4"), true);  
  66.         }  
  67.         FileOutputFormat.setOutputPath(job, new Path("G:/test/q3/output_3_4"));  
  68.           
  69.         System.exit(job.waitForCompletion(true) ? 1:0);  
  70.     }  
  71.   
  72.   
  73. }  
自定义数据类型:TestBean 
[java]  view plain  copy
  1. import java.io.DataInput;  
  2. import java.io.DataOutput;  
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.io.WritableComparable;  
  6.   
  7. public class TestBean implements WritableComparable<TestBean>{  
  8.     private String name;  
  9.     private String fileName;  
  10.     private int num;  
  11.       
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public String getFileName() {  
  19.         return fileName;  
  20.     }  
  21.     public void setFileName(String fileName) {  
  22.         this.fileName = fileName;  
  23.     }  
  24.     public int getNum() {  
  25.         return num;  
  26.     }  
  27.     public void setNum(int num) {  
  28.         this.num = num;  
  29.     }  
  30.     public TestBean() {  
  31.         super();  
  32.         // TODO Auto-generated constructor stub  
  33.     }  
  34.     public TestBean(String name, String fileName, int num) {  
  35.         super();  
  36.         this.name = name;  
  37.         this.fileName = fileName;  
  38.         this.num = num;  
  39.     }  
  40.     @Override  
  41.     public void write(DataOutput out) throws IOException {  
  42.         out.writeUTF(name);  
  43.         out.writeUTF(fileName);  
  44.         out.writeInt(num);  
  45.     }  
  46.     @Override  
  47.     public void readFields(DataInput in) throws IOException {  
  48.         name = in.readUTF();  
  49.         fileName = in.readUTF();  
  50.         num = in.readInt();       
  51.     }  
  52.     @Override  
  53.     public int compareTo(TestBean o) {  
  54.           
  55.         if(o.getName().compareTo(this.getName()) == 0){  
  56.             int flag = o.getNum()-this.getNum();  
  57.             if(flag == 0){  
  58.                 return 0;  
  59.             }else if(flag > 0){  
  60.                 return 1;  
  61.             }else{  
  62.                 return -1;  
  63.             }  
  64.         }else{  
  65.             return o.getName().compareTo(this.getName());  
  66.         }  
  67.           
  68.     }  
  69.       
  70.       
  71. }  

自定义分组组件:UserGC

[java]  view plain  copy
  1. import org.apache.hadoop.io.WritableComparable;  
  2. import org.apache.hadoop.io.WritableComparator;  
  3.   
  4. public class UserGC extends WritableComparator{  
  5.   
  6.     public UserGC() {  
  7.         super(TestBean.class,true);  
  8.     }  
  9.   
  10.     @Override  
  11.     public int compare(WritableComparable a, WritableComparable b) {  
  12.         TestBean pa = (TestBean) a;   
  13.         TestBean pb = (TestBean) b;   
  14.           
  15.           
  16.         return pa.getName().compareTo(pb.getName());  
  17.     }  
  18.       
  19. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值