为什么要将Mapper和Reducer类声明为静态? [英] Why declaring Mapper and Reducer classes as static?

查看:141
本文介绍了为什么要将Mapper和Reducer类声明为静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能表明我缺乏对Java的理解,但我想知道为什么大多数MapReduce程序中mapper和reducer类被声明为static?

解决方案

当将映射器和reducer类声明为另一个类的内部类时,它们必须声明为静态的,以使它们不依赖于。

Hadoop使用反射为每个映射或减少运行的任务创建类的实例。创建的新实例需要一个零参数构造函数(否则它将如何知道要传递什么)。



通过声明不带static关键字的内部映射器或reduce类,java编译实际上创建了一个构造函数,该构造函数需要在构造时传入父类的实例。



你应该能够通过对生成的类文件运行javap命令来看到这一点。



另外,static关键字在父类声明中使用时无效(这就是为什么你永远不会在顶层看到它,而只能在子类中看到它)


This is likely showing my lack of Java understanding but I am wondering why in most MapReduce programs mapper and reducer classes are declared as static?

解决方案

When declaring mapper and reducer classes as inner classes to another class, they have to be declared static such that they are not dependent on the parent class.

Hadoop uses reflection to create an instance of the class for each map or reduce task that runs. The new instance created expects a zero argument constructor (otherwise how would it know what to pass).

By declaring the inner mapper or reduce class without the static keyword, the java compile actually creates a constructor which expects an instance of the parent class to be passed in at construction.

You should be able to see this by running the javap command against the generated classfile

Also, the static keyword is not valid when used in a parent class declaration (which is why you never see it at the top level, but only in the child classes)

这篇关于为什么要将Mapper和Reducer类声明为静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆