• 技术文章 >数据库 >mysql教程

    Hadoop之MapReduce单元测试

    2016-06-07 16:31:12原创643

    通常情况下,我们需要用小数据集来单元测试我们写好的map函数和reduce函数。而一般我们可以使用Mockito框架来模拟OutputCollector对象(Hadoop版本号小于0.20.0)和Context对象(大于等于0.20.0)。 下面是一个简单的WordCount例子:(使用的是新API) 在开始之

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

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

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

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

    2.JUnit4

    3.Mockito

    ?

    map函数:

    public class WordCountMapper extends Mapper {
    	private static final IntWritable one = new IntWritable(1);
    	private Text word = new Text();
    	@Override
    	protected void map(LongWritable key, Text value,Context context)
    			throws IOException, InterruptedException {
    		String line = value.toString();		// 该行的内容
    		String[] words = line.split(";");	// 解析该行的单词
    		for(String w : words) {
    			word.set(w);
    			context.write(word,one);
    		}
    	}
    }

    ?reduce函数:

    public class WordCountReducer extends Reducer {
    	@Override
    	protected void reduce(Text key, Iterable values,Context context)
    			throws IOException, InterruptedException {
    		int sum = 0;
    		Iterator iterator = values.iterator();		// key相同的值集合
    		while(iterator.hasNext()) {
    			int one = iterator.next().get();
    			sum += one;
    		}
    		context.write(key, new IntWritable(sum));
    	}
    }

    ?测试代码类:

    public class WordCountMapperReducerTest {
    	@Test
    	public void processValidRecord() throws IOException, InterruptedException {
    		WordCountMapper mapper = new WordCountMapper();
    		Text value = new Text("hello");
    		org.apache.hadoop.mapreduce.Mapper.Context context = mock(Context.class);
    		mapper.map(null, value, context);
    		verify(context).write(new Text("hello"), new IntWritable(1));
    	}
    	@Test
    	public void processResult() throws IOException, InterruptedException {
    		WordCountReducer reducer = new WordCountReducer();
    		Text key = new Text("hello");
    		// {"hello",[1,1,2]}
    		Iterable values = Arrays.asList(new IntWritable(1),new IntWritable(1),new IntWritable(2));
    		org.apache.hadoop.mapreduce.Reducer.Context context = mock(org.apache.hadoop.mapreduce.Reducer.Context.class);
    		reducer.reduce(key, values, context);
    		verify(context).write(key, new IntWritable(4));		// {"hello",4}
    	}
    }

    ?

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

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

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



    已有 0 人发表留言,猛击->> 这里<<-参与讨论


    ITeye推荐



    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:大数据架构hadoop 下一篇:Lnmp安装MongoDB以及PHP扩展
    php培训_php实战培训【立即报名】-php中文网第20期

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• mysql substring()函数怎么用• mysql中sum()函数怎么用• 一起聊聊MySQL全局锁• mysql怎么取字符串前几位• mysql中有if吗
    1/1

    PHP中文网