Java 如何访问mapreduce中扩展reducer的静态内部类中的静态变量?
下面是我的程序主要方法的代码:Java 如何访问mapreduce中扩展reducer的静态内部类中的静态变量?,java,hadoop,mapreduce,bigdata,Java,Hadoop,Mapreduce,Bigdata,下面是我的程序主要方法的代码: public class Path { public static void main(String[] args) throws Exception { ArrayList<String> input = new ArrayList<String> (); input.add(args[0]); String output0="/output/path2"; Route r1 =new Route(inp
public class Path {
public static void main(String[] args) throws Exception {
ArrayList<String> input = new ArrayList<String> ();
input.add(args[0]);
String output0="/output/path2";
Route r1 =new Route(input,output0,2);
r1.main(args);
input.add(output0);
String output1="/output/path3";
Route r2 =new Route(input,output1,3);
r2.main(args);
}}
公共类路径{
公共静态void main(字符串[]args)引发异常{
ArrayList输入=新的ArrayList();
input.add(args[0]);
字符串output0=“/output/path2”;
路由r1=新路由(输入,输出0,2);
r1.主(args);
输入。添加(输出0);
字符串output1=“/output/path3”;
路由r2=新路由(输入、输出1,3);
r2.主要设备(args);
}}
类路由包含一个静态内部类扩展映射器和一个静态内部类扩展缩减器。
以下是与我的问题相关的课程路线定义的一部分:
public class Route {
public static int len;
public static String output;
public static ArrayList<String> input = new ArrayList<String> ();
public static class RouteMapper extends Mapper<Object, Text, Text, Text> {
public void map(Object key,Text value,Context context) throws IOException,InterruptedException {
//do map
}
}
public static class RouteReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException,InterruptedException{
//do reduce
}
}
public Route(ArrayList<String> in,String out,int l){
len = l;
output = out;
Iterator itr = in.iterator();
while(itr.hasNext()){
input.add(itr.next().toString());
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
//some configs of mapreduce
}}
公共类路由{
公共静态接口;
公共静态字符串输出;
公共静态ArrayList输入=新ArrayList();
公共静态类RouteMapper扩展映射器{
公共void映射(对象键、文本值、上下文上下文)引发IOException、InterruptedException{
//做地图
}
}
公共静态类路由减速器扩展减速器{
公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{
//减少
}
}
公共路由(ArrayList in、String out、int l){
len=l;
输出=输出;
迭代器itr=in.Iterator();
while(itr.hasNext()){
input.add(itr.next().toString());
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
//mapreduce的一些配置
}}
正如您在我的main方法中所看到的,静态变量len的值在两个map/reduce短语中应该是2和3。然而,当我试图在reduce短语中将len的值写入上下文时,得到了一个值0,这是变量len的默认值。因此,我无法得到我希望得到的正确结果。我很困惑为什么内部静态类RouteReducer不能访问外部类Route的静态变量。Hadoop是分布式系统。mapper和reducer实现的编译类被复制到其他节点,它们在自己的JVM中工作。因此,主类和每个任务都可以看到相同变量的不同实例。您需要手动将数据从主类传递到任务 轻量级数据可以通过
配置
从主类传递到map和reduce任务:
// in main
public static final String CONF_LEN = "conf.len";
...
conf.setInt(CONF_LEN, 3);
// in mapper
@Override
protected void setup(Mapper<Object, Text, Text, Text>.Context context) throws IOException, InterruptedException {
super.setup(context);
this.len = context.getConfiguration().getInt(CONF_LEN, -1);
}
//在main中
公共静态最终字符串CONF_LEN=“CONF.LEN”;
...
conf.setInt(conf_LEN,3);
//在映射器中
@凌驾
受保护的无效设置(Mapper.Context Context)引发IOException、InterruptedException{
超级设置(上下文);
this.len=context.getConfiguration().getInt(CONF_len,-1);
}
繁重的数据可以通过。这是一种观察,但我认为在MapReduce中使用静态类和静态变量只会让事情变得混乱。我猜标准wordcount示例使用了它们,但我不会将其作为实际编写MR.Make you's Maps/Reducer自己的(非静态)类并使用配置将配置传递给它们的模型,这就是它的用途。默认情况下,我不会使任何东西静止。