Hadoop 为每个映射器使用一个实例
Hadoop use one instance for each mapper
我正在使用 Hadoop 的 map reduce 来解析 xml 文件。所以我有一个名为 Parser
的 class,它可以有一个方法 parse()
来解析 xml 文件。所以我应该在 Mapper 的 map()
函数中使用它。
然而这意味着每次当我想调用一个Parser
时,我需要创建一个Parser
实例。但是这个实例对于每个地图作业应该是相同的。所以我想知道我是否可以只实例化这个 Parser
一次?
另外一个附加问题,为什么 Mapper
class 总是静态的?
为确保每个 Mapper 一个解析器实例,请使用映射器设置方法来实例化您的解析器实例,并使用清理方法进行清理。
我们为已有的 protobuf 解析器申请了同样的东西,但需要确保您的解析器实例是线程安全的,并且没有共享数据。
注意:每个映射器只会调用一次设置和清理方法,因此我们可以在那里初始化私有变量。
澄清 cricket_007 在 "In a distributed computing environment, sharing instances of a variable isn't possible..."
中所说的内容
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
同样,解析器对象也可以重复使用(Tip-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;
}
我正在使用 Hadoop 的 map reduce 来解析 xml 文件。所以我有一个名为 Parser
的 class,它可以有一个方法 parse()
来解析 xml 文件。所以我应该在 Mapper 的 map()
函数中使用它。
然而这意味着每次当我想调用一个Parser
时,我需要创建一个Parser
实例。但是这个实例对于每个地图作业应该是相同的。所以我想知道我是否可以只实例化这个 Parser
一次?
另外一个附加问题,为什么 Mapper
class 总是静态的?
为确保每个 Mapper 一个解析器实例,请使用映射器设置方法来实例化您的解析器实例,并使用清理方法进行清理。
我们为已有的 protobuf 解析器申请了同样的东西,但需要确保您的解析器实例是线程安全的,并且没有共享数据。 注意:每个映射器只会调用一次设置和清理方法,因此我们可以在那里初始化私有变量。 澄清 cricket_007 在 "In a distributed computing environment, sharing instances of a variable isn't possible..."
中所说的内容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 同样,解析器对象也可以重复使用(Tip-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;
}