Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:53:44+00:00 2026-06-04T05:53:44+00:00

public static class MapClass extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { private Text

  • 0
public static class MapClass extends MapReduceBase implements
        Mapper<LongWritable, Text, Text, IntWritable> {

    private Text word = new Text();

    public void map(LongWritable key, Text value, 
                    OutputCollector<Text, IntWritable> output, 
                    Reporter reporter) throws IOException {
      String line = value.toString();
      String num = Integer.parseInt(line);

       IntWritable one = new IntWritable(num);

        word.set(“key”);
        output.collect(word, one);

    }
}

public static class Reduce extends MapReduceBase implements
        Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterator<IntWritable> values,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        int sum = 0;
        int count = 0;
        int avg = 0;
        while (values.hasNext()) {
            sum += values.next().get();
            count++;
        }
        avg = sum / count;
        output.collect(key, new IntWritable(count));
    }
}

see the output.collect() specifically, i am printing key & count values..
For any input file, the output is
key 2
please help me…. (how the output is always 2 even if 100 nos as input??)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:53:46+00:00Added an answer on June 4, 2026 at 5:53 am

    If you want to test your code,

    here is a JUNIT test using the mockito framework, assuming you have your reducer and mapper in the example class.

    @RunWith(MockitoJUnitRunner.class)
     public class TestMapper {
    
     @Mock
     OutputCollector<Text, IntWritable> output;
    
     @Mock
     Reporter reporter;
    
     @Test
     public void testMap() throws Exception {
        ExampleClass.MapClass mapper = new ExampleClass.MapClass();
        mapper.map(new LongWritable(0), new Text("1"), output, reporter);
        mapper.map(new LongWritable(0), new Text("2"), output, reporter);
        mapper.map(new LongWritable(0), new Text("3"), output, reporter);
        verify(output, times(1)).collect(new Text("key"), new IntWritable(1));
        verify(output, times(1)).collect(new Text("key"), new IntWritable(2));
        verify(output, times(1)).collect(new Text("key"), new IntWritable(3));
     }
    
     @Test
     public void testReduce() throws Exception {
        ExampleClass.Reduce reducer = new ExampleClass.Reduce();
        List<IntWritable> list = Arrays.asList(new IntWritable(1), new IntWritable(2), new IntWritable(3));
    
        reducer.reduce(new Text("key"), list.iterator(), output, reporter);
    
        verify(output, times(1)).collect(new Text("key"), new IntWritable(3));
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static class Map extends MapReduceBase implements Mapper MapReduceBase , Mapper and JobConf are
public class C1 implements Iterable { private LinkedList list; public static class NC1 {
public class TowThreads { public static class FirstThread extends Thread { public void run()
I have this class: public static class CsvWriter { private static StreamWriter _writer =
Let's say I have a class called AppConfig: public static class AppConfig { private
I've got this public static class MyClassHelper { DataContex db = new DataContext(); public
Consider this code snippet: public static class ApplicationContext { private static Func<TService> Uninitialized<TService>() {
Consider: static class EntranceClass { public: static void RegisterSomething() { } static int main()
My code is public static class ContainerBootstrapper { public static void BootstrapStructureMap() { ObjectFactory.Initialize(x
This is a contrived example: public static class MyExtensions { public static void MyMethod(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.