- package com.hadoop.base;
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mrunit.mapreduce.MapDriver;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * Mapper 端的单元测试
- */
- @SuppressWarnings("all")
- public class TemperatureMapperTest {
- private Mapper mapper;// 定义一个 Mapper 对象
- private MapDriver driver;// 定义一个 MapDriver 对象
- @Before
- public void init() {
- mapper = new Temperature.TemperatureMapper();// 实例化一个 Temperature 中的 TemperatureMapper 对象
- driver = new MapDriver(mapper);// 实例化 MapDriver 对象
- }
- @Test
- public void test() throws IOException {
- // 输入一行测试数据
- String line = "1980 12 01 17 150 -5 10229 230 72 0 0 -9999";
- driver.withInput(new LongWritable(), new Text(line))// 跟 TemperatureMapper 输入类型一致
- .withOutput(new Text("weatherStationId"), new IntWritable(150))// 跟 TemperatureMapper 输出类型一致
- .runTest();
- }
- }
- package com.hadoop.base;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mrunit.mapreduce.ReduceDriver;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * Reducer 单元测试
- */
- @SuppressWarnings("all")
- public class TemperatureReduceTest {
- private Reducer reducer;// 定义一个 Reducer 对象
- private ReduceDriver driver;// 定义一个 ReduceDriver 对象
- @Before
- public void init() {
- reducer = new Temperature.TemperatureReducer();// 实例化一个 Temperature 中的 TemperatureReducer 对象
- driver = new ReduceDriver(reducer);// 实例化 ReduceDriver 对象
- }
- @Test
- public void test() throws IOException {
- String key = "weatherStationId";// 声明一个 key 值
- List values = new ArrayList();
- values.add(new IntWritable(200));// 添加第一个 value 值
- values.add(new IntWritable(100));// 添加第二个 value 值
- driver.withInput(new Text("weatherStationId"), values)// 跟 TemperatureReducer 输入类型一致
- .withOutput(new Text("weatherStationId"), new IntWritable(150))// 跟 TemperatureReducer 输出类型一致
- .runTest();
- }
- }
- package com.hadoop.base;
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mrunit.mapreduce.MapReduceDriver;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * Mapper 和 Reducer 集成起来测试
- */
- @SuppressWarnings("all")
- public class TemperatureTest {
- private Mapper mapper;// 定义一个 Mapper 对象
- private Reducer reducer;// 定义一个 Reducer 对象
- private MapReduceDriver driver;// 定义一个 MapReduceDriver 对象
- @Before
- public void init() {
- mapper = new Temperature.TemperatureMapper();// 实例化一个 Temperature 中的 TemperatureMapper 对象
- reducer = new Temperature.TemperatureReducer();// 实例化一个 Temperature 中的 TemperatureReducer 对象
- driver = new MapReduceDriver(mapper, reducer);// 实例化 MapReduceDriver 对象
- }
- @Test
- public void test() throws RuntimeException, IOException {
- // 输入两行行测试数据
- String line = "1985 07 31 02 200 94 10137 220 26 1 0 -9999";
- String line2 = "1985 07 31 11 100 56 -9999 50 5 -9999 0 -9999";
- driver.withInput(new LongWritable(), new Text(line))// 跟 TemperatureMapper 输入类型一致
- .withInput(new LongWritable(), new Text(line2))
- .withOutput(new Text("weatherStationId"), new IntWritable(150))// 跟 TemperatureReducer 输出类型一致
- .runTest();
- }
- }
来源: http://www.bubuko.com/infodetail-3045583.html