将变量发送到 Mapper 类
我试图从用户那里获取输入并将其传递给我创建的映射器类,但每当该值总是初始化为零而不是使用用户输入的实际值时。
如何确保每当我获取变量时它始终保持相同的值。我注意到 job1.setMapperClass(Parallel_for.class);创建该类的实例,从而强制变量重新初始化为其原始值。下面是两个课程的链接。我试图从 RunnerTool 类中获取时间值。
<一href="https://docs.google.com/leaf?id=0BwSPoDdRDme8NzVmYWRlODctNmEzNC00ZjcwLWEzZDEtZTJiODVmNDI0NWZk&hl=en_U S%20https://docs.google.com/leaf?id=0BwSPoDdRDme8NDE1Yzc4N2MtYTM0Yy00M2MxLWIzOWMtMmE1YjhlNmU4NmM3&hl=en_US" rel="nofollow">链接到 Java TestFor 类
// Mapper 中的设置方法
@Override
public void setup(Context context) {
int defaultValue = 1;
times = context.getConfiguration().getInt("parallel_for_iteration", defaultValue );
LOG.info(context.getConfiguration().get("parallel_for_iteration") + " Actually name from the commandline");
LOG.info(times + " Actually number of iteration from the commandline");
}
// RunnerTools 类
conf.setInt(ITERATION, times);
I am trying to get an input from the user and pass it to my mapper class that I have created but whenever the value always initialises to zero instead of using the actual value the user input.
How can make sure that whenever I get the variable it always maintain the same value. I have noticed that job1.setMapperClass(Parallel_for.class); creates an instance of the class hence forcing the variable to reinitialize to its original value. Below is the link to the two classes. I am trying to get the value of times from RunnerTool class.
// setup method in the Mapper
@Override
public void setup(Context context) {
int defaultValue = 1;
times = context.getConfiguration().getInt("parallel_for_iteration", defaultValue );
LOG.info(context.getConfiguration().get("parallel_for_iteration") + " Actually name from the commandline");
LOG.info(times + " Actually number of iteration from the commandline");
}
// RunnerTools class
conf.setInt(ITERATION, times);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该注意,映射器类将在许多集群节点上重新创建,因此运行作业时对映射器类实例所做的任何初始化都不会影响其他节点。技术相关的 jar 文件将分布在节点之间,然后在那里创建映射器。
因此,正如上面的答案所指出的,将信息传递给映射器的唯一方法是使用 Configuration 类。
You should note that mapper class will be recreated on many cluster nodes so any initalization done to the instance of the mapper class when running the job will not affect other nodes. Technically relevant jar file/s will be distributed among nodes and then mappers will be created there.
So as pointed in the answer above, the only way to pass information to the mappers is using Configuration class.
Mapper
get是通过反射初始化的,所以不能让用户与mapper类交互。相反,您拥有
Configuration
对象,如果您要设置作业,则必须提供该对象。您可以使用conf.set("YOUR KEY", "YOUR VALUE")
进行设置。在您的 Mapper 类中,您可以重写名为 setup(Context context) 的方法,您可以使用 context.getConfiguration().get("YOUR密钥”)。也许可以保存到您的映射器局部变量中。Mapper
get's initialized by reflection, so you can not let the user interact with the mapper class.Instead you have your
Configuration
object, which you have to provide if you're setting up your job. There you can set this usingconf.set("YOUR KEY", "YOUR VALUE")
. In yourMapper
class you can override a method calledsetup(Context context)
, there you can get the value usingcontext.getConfiguration().get("YOUR KEY")
. And maybe save to your mapper local variable.