zoukankan      html  css  js  c++  java
  • MapReduce UnitTest

    通常情况下,我们需要用小数据集来单元测试我们写好的map函数和reduce函数。而一般我们可以使用Mockito框架来模拟OutputCollector对象(Hadoop版本号小于0.20.0)和Context对象(大于等于0.20.0)。

    下面是一个简单的WordCount例子:(使用的是新API)

    在开始之前,需要导入以下包:

    1.Hadoop安装目录下和lib目录下的所有jar包。

    2.JUnit4

    3.Mockito

     

    map函数:

    Java代码 
    1. public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
    2.   
    3.     private static final IntWritable one = new IntWritable(1);  
    4.     private Text word = new Text();  
    5.       
    6.     @Override  
    7.     protected void map(LongWritable key, Text value,Context context)  
    8.             throws IOException, InterruptedException {  
    9.           
    10.         String line = value.toString();     // 该行的内容  
    11.         String[] words = line.split(";");   // 解析该行的单词  
    12.           
    13.         for(String w : words) {  
    14.             word.set(w);  
    15.             context.write(word,one);  
    16.         }  
    17.     }  
    18. }  

     reduce函数:

    Java代码 
    1. public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {  
    2.   
    3.     @Override  
    4.     protected void reduce(Text key, Iterable<IntWritable> values,Context context)  
    5.             throws IOException, InterruptedException {  
    6.           
    7.         int sum = 0;  
    8.         Iterator<IntWritable> iterator = values.iterator();       // key相同的值集合  
    9.         while(iterator.hasNext()) {  
    10.             int one = iterator.next().get();  
    11.             sum += one;  
    12.         }  
    13.         context.write(key, new IntWritable(sum));  
    14.     }  
    15.   
    16. }  

     测试代码类:

    Java代码 
    1. public class WordCountMapperReducerTest {  
    2.   
    3.     @Test  
    4.     public void processValidRecord() throws IOException, InterruptedException {  
    5.         WordCountMapper mapper = new WordCountMapper();  
    6.         Text value = new Text("hello");  
    7.         org.apache.hadoop.mapreduce.Mapper.Context context = mock(Context.class);  
    8.         mapper.map(null, value, context);  
    9.         verify(context).write(new Text("hello"), new IntWritable(1));  
    10.     }  
    11.       
    12.     @Test  
    13.     public void processResult() throws IOException, InterruptedException {  
    14.         WordCountReducer reducer = new WordCountReducer();  
    15.         Text key = new Text("hello");  
    16.         // {"hello",[1,1,2]}  
    17.         Iterable<IntWritable> values = Arrays.asList(new IntWritable(1),new IntWritable(1),new IntWritable(2));  
    18.         org.apache.hadoop.mapreduce.Reducer.Context context = mock(org.apache.hadoop.mapreduce.Reducer.Context.class);  
    19.           
    20.         reducer.reduce(key, values, context);  
    21.           
    22.         verify(context).write(key, new IntWritable(4));     // {"hello",4}  
    23.     }  
    24. }  

     

    具体就是给map函数传入一行数据-"hello"

    map函数对数据进行处理,输出{"hello",0}

    reduce函数接受map函数的输出数据,对相同key的值求和,并输出。

  • 相关阅读:
    Eval版的ASP木马原理解析
    cmd命令
    Eval版的ASP木马原理解析
    Vbs脚本实现radmin终极后门
    迅雷是如何识别并偷偷上传文件的?
    迅雷是如何识别并偷偷上传文件的?
    广外男生病毒代码剖析
    cmd命令
    Vbs脚本实现radmin终极后门
    广外男生病毒代码剖析
  • 原文地址:https://www.cnblogs.com/aukle/p/3230836.html
Copyright © 2011-2022 走看看