2011-09-16 84 views
3

我在Hadoop中更改公共靜態變量時遇到問題。 我想從命令行將一些值作爲參數傳遞給jar文件。通過參數在Hadoop中初始化公共靜態變量

這裏是我的代碼:

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 & myvaribale2總是有100 我用的Hadoop 0.20.203與Ubuntu 10.04

回答

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 
} 
+0

謝謝,這就是我正在尋找.. – Ahmed