Junit test for Map Reduce Hadoop

0 votes

Can u please share a sample code ,how to do the Junit test for Map reduce ??

Dec 17, 2018 in Big Data Hadoop by slayer
• 29,050 points
366 views

1 answer to this question.

0 votes

Hi. This is the code I used and it worked. Go ahead and try this:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

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.*;
import org.apache.hadoop.mrunit.types.Pair;
import org.junit.Before;
import org.junit.Test;

public class DataMaper extends TestCase {

public static class myMap extends
Mapper<LongWritable, Text, Text, IntWritable> {
Text day = new Text();

public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] line = value.toString().split(",");
int val = Integer.parseInt(line[0]);
day.set(line[1]);
context.write(day, new IntWritable(val));
}
}

public static class myreducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> val, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable i : val) {
sum += i.get();
}
context.write(key, new IntWritable(sum));
}
}

MapDriver<LongWritable, Text, Text, IntWritable> mapDriver;
ReduceDriver<Text, IntWritable, Text, IntWritable> reducerdriver;
MapReduceDriver<LongWritable, Text, Text, IntWritable, Text, IntWritable> mapreducedriver;

@Before
public void setUp() {
myMap mapper = new myMap();
mapDriver = MapDriver.newMapDriver(new myMap());

myreducer reducer = new myreducer();
reducerdriver = ReduceDriver.newReduceDriver(new myreducer());
mapreducedriver = MapReduceDriver.newMapReduceDriver(mapper, reducer);

}

@Test
public void testSimple() throws Exception {

mapreducedriver.withMapper(new myMap());
mapreducedriver.withInput(new LongWritable(1), new Text("1,sunday"));
mapreducedriver.withInput(new LongWritable(1), new Text("2,sunday"));
mapreducedriver.withReducer(new myreducer());
mapreducedriver.withOutput(new Text("sunday"), new IntWritable(3));
mapreducedriver.runTest();

}

}

answered Dec 17, 2018 by Omkar
• 67,380 points

Related Questions In Big Data Hadoop

0 votes
1 answer

Map and Reduce task memory settings in Hadoop YARN

It's preferable and generally, it is recommended ...READ MORE

answered Apr 18, 2018 in Big Data Hadoop by kurt_cobain
• 9,240 points
79 views
0 votes
1 answer

Which is the most preferable language for Hadooop Map-Reduce programs?

MapReduce is a programming model to perform ...READ MORE

answered Aug 3, 2018 in Big Data Hadoop by Neha
• 6,280 points
55 views
0 votes
1 answer

What is the best functional language to do Hadoop Map-Reduce?

down voteacceptedBoth Clojure and Haskell are definitely ...READ MORE

answered Sep 4, 2018 in Big Data Hadoop by Frankie
• 9,810 points
40 views
0 votes
1 answer

Error while hitting the hadoop jar command to execute my map reduce

Since your jar file was inside the ...READ MORE

answered Feb 11 in Big Data Hadoop by Omkar
• 67,380 points
59 views
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,670 points
2,642 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,670 points
275 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
13,202 views
0 votes
1 answer

Hadoop dfs -ls command?

In your case there is no difference ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by kurt_cobain
• 9,240 points
972 views
0 votes
1 answer

In Hadoop MapReduce, how can i set an Object as the Value for Map output?

Try this and see if it works: public ...READ MORE

answered Nov 20, 2018 in Big Data Hadoop by Omkar
• 67,380 points
36 views
+1 vote
1 answer

Hadoop Map Reduce: java.lang.reflect.InvocationTargetException

I executed the same code and it ...READ MORE

answered Dec 17, 2018 in Big Data Hadoop by Omkar
• 67,380 points
158 views