通过参数在Hadoop中初始化公共静态变量

时间:2011-09-16 10:04:11

标签: variables static hadoop

我在Hadoop中更改公共静态变量时遇到问题。 我试图从命令行将一些值作为参数传递给jar文件。

这是我的代码:

public class MyClass {
  public static long myvariable1 = 100;
  public static class Map extends Mapper<Object, Text, Text, Text> {
    public static long myvariabl2 = 200;
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

    }
  }
  public static class Reduce extends Reducer<Text, Text, Text, Text> {
    public void reduce(Text key, Iterable<Text> values, Context context)
    throws IOException, InterruptedException {

    }
  }
  public static void main(String[] args) throws Exception {
    col_no = Long.parseLong(args[0]);
    Map.myvariable1 = Long.parseLong(args[1]);
    Map.myvariable2 = Long.parseLong(args[1]);
    other stuff here
  }
}

但它不起作用,myvariable1&amp; myvaribale2总是有100&amp; 200。 我使用Hadoop 0.20.203和Ubuntu 10.04

1 个答案:

答案 0 :(得分:4)

您可以采取的措施是将变量存储在用于启动作业的配置中。

public static class Map extends Mapper<Object, Text, Text, Text> {
  public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

    Configuration conf = context.getConfiguration();
    String var2String = conf.get("myvariable2");
    long myvariable2 = Long.parseLong(var2String);
    //etc.
  }
}

public static void main(String[] args) throws Exception {
  col_no = Long.parseLong(args[0]);
  String myvariable1 = args[1];
  String myvariable2 = args[1];

  // add values to configuration
  Configuration conf = new Configuration();
  conf.set("myvariable1", myvariable1);
  conf.set("myvariable2", myvariable2);

  //other stuff here
}