2014-02-11 26 views
0

值我試圖做一個chaning工作。訪問ARGS [0]中的MapReduce

因此,爲了某些時候,我想訪問ARGS(public static void main(String[] args))。

說ARGS [0]映射器。

是否有訪問映射這些值,而不是把他們的功能和訪問的方式? 替代解決方案

conf.set("args", args[1]); 
job1.setJarByClass(BinningDriver.class); 
FileSystem fs1 = FileSystem.get(conf); 
job1.setOutputKeyClass(Text.class); 
job1.setOutputValueClass(Text.class); 
job1.setMapperClass(BinningInput.class); 
job1.setInputFormatClass(TextInputFormat.class); 
job1.setOutputFormatClass(TextOutputFormat.class); 
Path out = new Path(args[1]+"/Indexing"); //Output goes to user output location/indexing 
if(fs1.exists(out)){ 
    fs1.delete(out,true); 
} 

FileInputFormat.addInputPath(job1, new Path(args[0])); 
FileOutputFormat.setOutputPath(job1, out); 
} 

映射器

public void setup(Context context){ 
Configuration conf = context.getConfiguration(); 
String param = conf.get("args"); 
System.out.println("args:"+param); 
    } 

這適用

+0

什麼是ARGS [0]?你指的是'公共靜態無效的主要(字符串[] ARGS)'? –

+0

是exactly.I是說你有沒有意識到,有一個高的機會,映射器是在一個完全不同的JVM運行到運行的主要一個關於重視 –

+0

? –

回答

1

參數數量[]是驅動器類的主要功能的輸入參數。訪問此參數的唯一方法是在驅動程序內(此參數的範圍僅爲主要功能)。因此,如果您想將這些信息傳遞給映射器,您需要將它們作爲參數傳遞(例如,將此信息添加到分佈式緩存並從映射器的配置中獲取它)。

如果你只是想傳遞一些參數,檢查this article,並取代「123」與ARGS [2],或任何ARG你感興趣的內容。

如果你想傳遞一個整個文件進行處理,執行以下操作:

實施例:

在Driver類

主要方法:

public static void main(String[] args) { 
    ... 
    FileInputFormat.setInputPaths(conf, new Path(args[0]));  
    FileOutputFormat.setOutputPath(conf, new Path(args[1])); 
    ... 
    try { 
     DistributedCache.addCacheFile(new URI(args[2]), conf);   
    } catch (URISyntaxException e) { 
     System.err.println(e.toString()); 
    } 
    .... 
} 

在映射器,地圖()之前甲基OD,定義配置方法(我使用Hadoop 1.2.0):

Set<String> lines; 
public void configure(JobConf job){ 
    lines = new HashSet<>(); 

    BufferedReader SW; 
    try { 
     localFiles = DistributedCache.getLocalCacheFiles(job);  
     SW = new BufferedReader(new FileReader(localFiles[0].toString())); 
     lines.add(SW.readLine());    
     SW.close(); 
    } catch (FileNotFoundException e) { 
     System.err.println(e.toString()); 
    } catch (IOException e) { 
     System.err.println(e.toString()); 
    }  
} 

有關如何使用分佈式緩存的詳細信息,請參閱API: http://hadoop.apache.org/docs/stable/api/org/apache/hadoop/filecache/DistributedCache.html

+0

感謝您的回覆Vefthym.can你給我看一個小例子 –

+0

我更新了我的答案。我希望它有幫助 – vefthym

+0

請參閱我的更新 –