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,040 points
210 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 65,850 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,260 points
48 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,140 points
32 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,570 points
26 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
• 65,850 points
42 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
• 9,030 points
1,668 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
• 9,030 points
130 views
0 votes
10 answers

hadoop fs -put command?

copy command can be used to copy files ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Sujay
8,093 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,260 points
568 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
• 65,850 points
18 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
• 65,850 points
94 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.