为什么将 Mapper 和 Reducer 类声明为静态的?

Why declaring Mapper and Reducer classes as static?(为什么将 Mapper 和 Reducer 类声明为静态的?)
本文介绍了为什么将 Mapper 和 Reducer 类声明为静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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?

推荐答案

当将 mapper 和 reducer 类声明为另一个类的内部类时,必须将它们声明为 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 使用反射为每个运行的 map 或 reduce 任务创建类的实例.创建的新实例需要一个零参数构造函数(否则它怎么知道要传递什么).

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).

通过在没有 static 关键字的情况下声明内部 mapper 或 reduce 类,java 编译实际上创建了一个构造函数,该构造函数期望在构造时传入父类的实例.

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.

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

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

另外,static 关键字在父类声明中使用时无效(这就是为什么您在顶层看不到它,而只在子类中看到它的原因)

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 类声明为静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)