Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在map reduce中使用util类中的静态变量_Java_Hadoop_Static_Mapreduce - Fatal编程技术网

Java 在map reduce中使用util类中的静态变量

Java 在map reduce中使用util类中的静态变量,java,hadoop,static,mapreduce,Java,Hadoop,Static,Mapreduce,我有一个映射器、还原器和一个util类 - The util class has a static class variable int a=0 and a static method doSomething() which calls another static method add() which increment a by 1. - I call the UtilClass.doSomething() from the reducer. - When I try to pr

我有一个映射器、还原器和一个util类

- The util class has a static class variable int a=0 
  and a static method doSomething() which calls another static method 
  add() which increment a by 1.
- I call the UtilClass.doSomething() from the reducer.
- When I try to print the final value of "a" in 
  doSomething() using 
  context.getCounter("doSomethign method","value of a").increment(1);,
  the value says 1229 which is weird since the reduce task received only 240 keys
- A total of 30 reducers are spawned. 
下面是一个示例代码块。我在避免一些样板代码。 注意:这是以分布式方式运行的,而不是独立运行的

class MyMapper extends Reducer{
 public reduce(<params>){
   UtilClass.doSomething(context);
 }
}

class UtilClass{
  public static int a=0;
  public static void add(){
    a=a+1;
  }
  public static void doSomething(Reducer.Context context){
    add();
    // This is printing 1229 in the end, instead of 240 which is the number of key given to the reducer
    context.getCounter("doSomethign method","value of a").increment(1);
  }
}
类MyMapper扩展了Reducer{
公共资源(减少){
UtilClass.doSomething(上下文);
}
}
类UtilClass{
公共静态int a=0;
公共静态void add(){
a=a+1;
}
公共静态void doSomething(Reducer.Context){
添加();
//最后打印的是1229,而不是240,240是提供给减速器的键数
getCounter(“doSomethign方法”,“a的值”)。增量(1);
}
}
这就引出了以下问题

  • 我了解到每个Map/reduce任务都在单独的JVM上运行。但如果是这样,为什么变量a会得到1229这个奇怪的值?每个reduce任务应该只为每个键调用doSomething(),并且“a”的最终值应该是240
  • 如果我尝试创建一个util类的对象并调用object.doSomething()(使方法非静态但保持“静态”),那么我得到240

  • 我在这里的静态基本原理中遗漏了什么吗

  • 您使用的是哪个版本的hadoop?@BinaryNerd hadoop 2.6.0.2.2.0.0.-2041您使用的是哪个版本的hadoop?@BinaryNerd hadoop 2.6.0.2.0.0.-2041