![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
class
ShortestPath {
static Map < String, Integer > map = new HashMap < String, Integer > ();
public static class TokenizerMapper extends Mapper < LongWritable, Text, Text, Text > {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
map.put( " test " , 123 " );
}
}
public static class ShortestPathReducer extends Reducer < Text, Text, Text, Text > {
public void reduce(Text key, Iterable < Text > values, Context context)
throws IOException, InterruptedException {
map.put( " test " , 123 " );
}
}
public static void main(String[] args) throws Exception {
System.out.println(map.toString());
static Map < String, Integer > map = new HashMap < String, Integer > ();
public static class TokenizerMapper extends Mapper < LongWritable, Text, Text, Text > {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
map.put( " test " , 123 " );
}
}
public static class ShortestPathReducer extends Reducer < Text, Text, Text, Text > {
public void reduce(Text key, Iterable < Text > values, Context context)
throws IOException, InterruptedException {
map.put( " test " , 123 " );
}
}
public static void main(String[] args) throws Exception {
System.out.println(map.toString());
}
}
}
虽然编译成功,但在main中的输出显示,map为null。
求教如何使mapper和reducer能共享变量?