2016-05-31 79 views
1

我正在使用Hadoop的映射reduce来解析xml文件。所以我有一个名为Parser的类,可以使用方法parse()来解析xml文件。所以我应该在Mapper的map()函数中使用它。Hadoop为每个映射器使用一个实例

但是这意味着每当我想调用Parser时,我需要创建一个Parser实例。但是这个实例应该对每个地图作业都是一样的。所以我想知道我是否可以只将这个Parser实例化一次?

只是另一个附加问题,为什么Mapper类总是静态的?

+1

在分布式计算环境中,共享变量的情况下是不可能的...也不能肯定是否映射器总是静态的。你在哪里看到? –

+1

Scratch那第二种说法...我最近没有写mapreduce,但这里是解释http://stackoverflow.com/questions/11570674/why-declaring-mapper-and-reducer-classes-as-static –

+0

在这示例代码,Mapper是静态类。 [链接](https://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html) –

回答

3

为确保每个Mapper都有一个解析器实例,请使用mappers设置方法来实例化解析器实例并使用清理方法进行清理。

同样的事情,我们申请了protobuf解析器,但需要确保解析器实例是线程安全的,并且没有共享数据。 注意:每个映射器只会调用一次setup和cleanup方法,所以我们可以在那里初始化私有变量。 为了明确所说cricket_007“在一个分布式计算环境中,一个可变的共享情况下是不可能的。”

we have a practice of reusing of writable classes instead of creating new writables every time we need. we can instantiate once and re-set the writable multiple times as described by Tip 6 同样解析器对象也可以被重新使用(提示-6-样式)。如下面的代码所述。 对于前:

private YourXMLParser xmlParser = null; 
    @Override 
     protected void setup(Context context) throws IOException, InterruptedException { 
      super.setup(context); 
      xmlParser= new YourXMLParser();   
     } 

    @Override 
     protected void cleanup(Mapper<ImmutableBytesWritable, Result, NullWritable, Put>.Context context) throws IOException, 
         InterruptedException { 
      super.cleanup(context); 
        xmlParser= null; 
    } 
相关问题