Why is the declaration of the Mapper and Reducer classes static?

This most likely shows my lack of understanding of Java, but I wonder why, in most MapReduce programs, maps and reducer classes are declared as static?

+6
source share
2 answers

When declaring matching and reducer classes as inner classes to another class, they must be declared static such that they are independent of the parent class.

Hadoop uses reflection to create an instance of the class for each map or to reduce the task. The newly created instance expects the constructor of null arguments (otherwise how does it know what to pass).

By declaring an internal mapper or decreasing a class without a static keyword, java compilation actually creates a constructor that expects an instance of the parent class to be passed during construction.

You should be able to see this by running the javap command against the generated class file.

In addition, the static keyword is not valid when using the parent class in the declaration (so you never see it at the top level, but only in child classes)

+14
source

I can think of two reasons:

  • When performing map reduction methods , the state that must be stored in the object is not required . Thus, all the necessary information is transmitted to the method, there is no need to store additional data in the object. If the lifetime of an object does not exceed one method call, why do you have to deal with instantiation?
  • It does not make sense to have more than one object, similar reasons why you would have performed a Singleton Pattern .
+1
source

Source: https://habr.com/ru/post/920926/


All Articles