第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Hadoop之MapReduce實(shí)戰(zhàn)-單元測(cè)試篇

標(biāo)簽:
Hadoop

原文地址:https://itweknow.cn/detail?id=62 ,欢迎大家访问。

在上一篇文章《Hadoop之MapReduce实战》中,我们已经完成了一个很简单的MapReduce程序,并且成功的在Hadoop集群上执行。下面我们将来简要的介绍一下如何在我们本地调试和测试我们的MapReduce程序。

MrUnit

MRUnit是Cloudera公司专为Hadoop MapReduce写的单元测试框架,其API非常简洁实用。该框架对不同的测试对象使用不同的Driver,因此分为了:MapDriver、ReduceDriver和MapReduceDriver。

项目依赖

在前一篇文章的基础之上我们还需要添加如下依赖:

<dependency>
    <groupId>org.apache.mrunit</groupId>
    <artifactId>mrunit</artifactId>
    <version>1.1.0</version>
    <classifier>hadoop2</classifier>
    <scope>test</scope></dependency>

Mapper测试

测试的方法很简单,其实就是我们模拟一个输入,然后判断输出结果是否符合预期。我们初始化一个MapDriver,然后通过withMapper指定要测试的Mapper,通过withInput和withOutput指定输入和期望输出,最后runTest()来执行测试方法。

public class CharCountMapperTest {    @Test
    public void mapperTest() throws IOException {
        Text value = new Text("hi hadoop");        new MapDriver<LongWritable, Text, Text, LongWritable>()
                .withMapper(new CharCountMapper())
                .withInput(new LongWritable(0), value)
                .withOutput(new Text("h"), new LongWritable(1))
                .withOutput(new Text("i"), new LongWritable(1))
                .withOutput(new Text(" "), new LongWritable(1))
                .withOutput(new Text("h"), new LongWritable(1))
                .withOutput(new Text("a"), new LongWritable(1))
                .withOutput(new Text("d"), new LongWritable(1))
                .withOutput(new Text("o"), new LongWritable(1))
                .withOutput(new Text("o"), new LongWritable(1))
                .withOutput(new Text("p"), new LongWritable(1))
                .runTest();
    }
}

在写这个MapperTest的时候遇到了一个坑,这里说明一下,就是这里需要注意一下我们的MapDriver是org.apache.hadoop.mrunit.mapreduce包下的而不是org.apache.hadoop.mrunit包下的,如果导错包的话会编译不通过的。

Reducer测试

public class CharCountReducerTest {    @Test
    public void reducerTest() throws IOException {        new ReduceDriver<Text, LongWritable, Text, LongWritable>()
                .withReducer(new CharCountReducer())
                .withInput(new Text("h"),
                        Arrays.asList(new LongWritable(1),                                new LongWritable(1),                                new LongWritable(1)))
                .withInput(new Text("i"), Arrays.asList(new LongWritable(1)))
                .withOutput(new Text("h"), new LongWritable(3))
                .withOutput(new Text("i"), new LongWritable(1))
                .runTest();
    }
}

驱动程序测试

对与驱动程序我们可以选择使用MapReduceDriver写单元测试来测试,也可以直接执行main方法完成测试。

  • MapReduceDriver
    需要指定mapper和reducer,然后输入和预期输出。这里的输出包含多个键值对,有个顺序问题,默认情况下是根据键来自然排序输出的。

@Test
    public void driverTest() throws IOException {
        String line1 = "hi";
        String line2 = "hello";        new MapReduceDriver(new CharCountMapper(), new CharCountReducer())
                .withInput(new LongWritable(0), new Text(line1))
                .withInput(new LongWritable(1), new Text(line2))
                .withOutput(new Text("e"), new LongWritable(1))
                .withOutput(new Text("h"), new LongWritable(2))
                .withOutput(new Text("i"), new LongWritable(1))
                .withOutput(new Text("l"), new LongWritable(2))
                .withOutput(new Text("o"), new LongWritable(1))
                .runTest();
    }
  • main方法
    也可以直接运行驱动程序中的main方法,Hadoop有一个本地作业运行库,是为测试而生的,我们可以通过设置mapreduce.framwork.name来配置,默认就是local(本地作业运行器)。

             




作者:名字想好没
链接:https://www.jianshu.com/p/26ef726e643b


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消