MRUnit测试WordCount

pom

<dependency>
	<groupId>org.apache.mrunit</groupId>
	<artifactId>mrunit</artifactId>
	<version>1.1.0</version>
	<!--<scope>test</scope>-->
	<!--不加导包可能失败-->
	<classifier>hadoop2</classifier>
</dependency>

 

wordcount代码见:

https://blog.csdn.net/sinat_36710456/article/details/88127155

 

WordCountTest.java


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.mapreduce.MapDriver;
import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
import org.apache.hadoop.mrunit.types.Pair;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class WordCountTest {

    /**
     * @Author: w
     * @Description: 测试Mapper
     */
    @Test
    public void WordCountMapperTest() throws IOException {
        /** 构造输入值 */
        Pair<LongWritable, Text> line1 = new Pair<LongWritable, Text>(new LongWritable(0), new Text("zhangsan lisi wangwu"));
        Pair<LongWritable, Text> line2 = new Pair<LongWritable, Text>(new LongWritable(0), new Text("zhangsan lisi"));
        List<Pair<LongWritable, Text>> inList = new ArrayList<Pair<LongWritable, Text>>();
        inList.add(line1);
        inList.add(line2);

        /** 构造输出值 */
        Pair<Text, IntWritable> outputRecord1 = new Pair<Text, IntWritable>(new Text("zhangsan"), new IntWritable(1));
        Pair<Text, IntWritable> outputRecord2 = new Pair<Text, IntWritable>(new Text("lisi"), new IntWritable(1));
        Pair<Text, IntWritable> outputRecord3 = new Pair<Text, IntWritable>(new Text("wangwu"), new IntWritable(1));
        Pair<Text, IntWritable> outputRecord4 = new Pair<Text, IntWritable>(new Text("zhangsan"), new IntWritable(1));
        Pair<Text, IntWritable> outputRecord5 = new Pair<Text, IntWritable>(new Text("lisi"), new IntWritable(1));
        List<Pair<Text, IntWritable>> list = new ArrayList<Pair<Text, IntWritable>>();
        /* 特别注意:*/
        //(本人亲测)需要注意输出值顺序,否则测试不通过
        list.add(outputRecord1);
        list.add(outputRecord2);
        list.add(outputRecord3);
        list.add(outputRecord4);
        list.add(outputRecord5);

        new MapDriver<LongWritable, Text, Text, IntWritable>()
                // 配置mapper
                .withMapper(new WordCountMapper())
                //单行输入
//                .withInput(new LongWritable(0), new Text("zhangsan lisi wangwu"))
                //多行输入
                .withAll(inList)
                //单行输出
//                .withOutput(new Text("zhangsan"), new IntWritable(1))
                //多行输出
                .withAllOutput(list)
                .runTest();
    }


    /**
     * @Author: w
     * @Description: 测试Reducer
     */
    @Test
    public void WordCountReducerTest() throws IOException {
        /** 构造输入值 */
        List<IntWritable> zhangsanList = new ArrayList<IntWritable>();
        zhangsanList.add(new IntWritable(1));
        zhangsanList.add(new IntWritable(1));
        List<IntWritable> lishiList = new ArrayList<IntWritable>();
        lishiList.add(new IntWritable(1));
        lishiList.add(new IntWritable(1));
        List<IntWritable> wangwuList = new ArrayList<IntWritable>();
        wangwuList.add(new IntWritable(1));
        Pair<Text, List<IntWritable>> line1 = new Pair<Text, List<IntWritable>>(new Text("zhangsan"), zhangsanList);
        Pair<Text, List<IntWritable>> line2 = new Pair<Text, List<IntWritable>>(new Text("lisi"), lishiList);
        Pair<Text, List<IntWritable>> line3 = new Pair<Text, List<IntWritable>>(new Text("wangwu"), wangwuList);
        List<Pair<Text, List<IntWritable>>> inList = new ArrayList<Pair<Text, List<IntWritable>>>();
        inList.add(line1);
        inList.add(line2);
        inList.add(line3);

        /** 构造输出值 */
        Pair<Text, IntWritable> outputRecord1 = new Pair<Text, IntWritable>(new Text("zhangsan"), new IntWritable(2));
        Pair<Text, IntWritable> outputRecord2 = new Pair<Text, IntWritable>(new Text("lisi"), new IntWritable(2));
        Pair<Text, IntWritable> outputRecord3 = new Pair<Text, IntWritable>(new Text("wangwu"), new IntWritable(1));
        List<Pair<Text, IntWritable>> list = new ArrayList<Pair<Text, IntWritable>>();
        /* 特别注意:*/
        //(本人亲测)需要注意输出值顺序,否则测试不通过
        list.add(outputRecord1);
        list.add(outputRecord2);
        list.add(outputRecord3);

        new ReduceDriver<Text, IntWritable, Text, IntWritable>()
                //设置Reducer
                .withReducer(new WordCountReducer())
                //设置输入key和List
                .withAll(inList)
                //设置期望输出
                .withAllOutput(list)
                //运行测试
                .runTest();
    }


    /**
     * @Author: w
     * @Description: 测试Driver
     */
    @Test
    public void WordCountDriverTest() throws IOException {
        /** 构造输入值 */
        Pair<LongWritable, Text> line1 = new Pair<LongWritable, Text>(new LongWritable(0), new Text("zhangsan lisi wangwu"));
        Pair<LongWritable, Text> line2 = new Pair<LongWritable, Text>(new LongWritable(0), new Text("zhangsan lisi"));
        List<Pair<LongWritable, Text>> inList = new ArrayList<Pair<LongWritable, Text>>();
        inList.add(line1);
        inList.add(line2);

        /** 构造输出值 */
        Pair<Text, IntWritable> outputRecord1 = new Pair<Text, IntWritable>(new Text("zhangsan"), new IntWritable(2));
        Pair<Text, IntWritable> outputRecord2 = new Pair<Text, IntWritable>(new Text("lisi"), new IntWritable(2));
        Pair<Text, IntWritable> outputRecord3 = new Pair<Text, IntWritable>(new Text("wangwu"), new IntWritable(1));
        List<Pair<Text, IntWritable>> list = new LinkedList<Pair<Text, IntWritable>>();
        /* 特别注意:*/
        //(本人亲测)需要注意输出值顺序,否则测试不通过
        list.add(outputRecord2);
        list.add(outputRecord3);
        list.add(outputRecord1);

        new MapReduceDriver<LongWritable,Text,Text, IntWritable, Text, IntWritable>()
                //设置Mapper
                .withMapper(new WordCountMapper())
                //设置Reducer
                .withReducer(new WordCountReducer())
                //设置输入key和List
                .withAll(inList)
                //设置期望输出
                .withAllOutput(list)
                //运行测试
                .runTest();
    }


}

 


 

/* 特别注意:*/
//(本人亲测)需要注意输出值顺序,否则测试不通过。

eg:错误信息:

java.lang.AssertionError: 3 Error(s): (Missing expected output (zhangsan, 2) at position 0, got (lisi, 2)., Missing expected output (lisi, 2) at position 1, got (wangwu, 1)., Missing expected output (wangwu, 1) at position 2, got (zhangsan, 2).)

 字面意思:

期待输出:
   (zhangsan, 2)
   (lisi, 2)
   (wangwu, 1)


实际输出:
   (lisi, 2)
   (wangwu, 1)
   (zhangsan, 2)

根据错误提示,按照实际输出顺序,修改期待输出顺序即可。

 

完整代码见GitHub:https://github.com/666wg/mapreduce

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinat_36710456

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值