shimsham
shimsham

Reputation: 123

Should the Mapper and Reducer be inner classes?

In the O'Reilly Hadoop guide, implementations of the mapper and reducer classes are first introduced in separate files. Pages later, it shows them as inner classes.

With the jobs that I've written and worked on, I've found that implementations with the mapper and reducers bundled together in one class are much harder to work with. So my convention is to write separate, top-level classes. Is this correct?

Upvotes: 0

Views: 290

Answers (2)

Prabhu Moorthy
Prabhu Moorthy

Reputation: 177

Looking at a java program perspective there are certainly some differences between Inner classes and Outer classes and one of the main advantage being that

Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.

In a MapReduce program since the mapper and reducer run independent of each other using Inner classes certainly does not provide any programmatic advantage.

The main reason why Inner classes are used in the book is for readability and ease of use. Any newbie trying to copy the code from the book can copy paste(in case of e-book) the entire code to one single Java file and execute the program.

Just FYI: Refer this link to know when to use inner classes.

Upvotes: 2

Shravanya
Shravanya

Reputation: 97

You can write them both as inner classes or as separate classes. The second one is much better practice.

Upvotes: 0

Related Questions