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,370 points
2,573 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
• 69,220 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,350 points
1,219 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 4, 2018 in Big Data Hadoop by Neha
• 6,300 points
3,780 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,830 points
919 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 12, 2019 in Big Data Hadoop by Omkar
• 69,220 points
1,612 views
+1 vote
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
• 11,380 points
11,078 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
• 11,380 points
2,575 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
109,076 views
–1 vote
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,350 points
4,644 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 21, 2018 in Big Data Hadoop by Omkar
• 69,220 points
1,009 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
• 69,220 points
1,729 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP