2014-02-19 54 views
0

我想訪問我的映射器中分佈式文件的內容。以下是我編寫的用於生成分佈式緩存文件名稱的代碼。請幫我訪問文件通過分佈式緩存訪問映射器中的文件

public class DistCacheExampleMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text > 
    { 
     Text a = new Text(); 
    Path[] dates = new Path[0]; 
    public void configure(JobConf conf) { 

    try { 
      dates = DistributedCache.getLocalCacheFiles(conf); 
      String astr = dates.toString(); 
      a = new Text(astr); 

      } catch (IOException ioe) { 
      System.err.println("Caught exception while getting cached files: " + 
      StringUtils.stringifyException(ioe)); 
      } 


    } 

    @Override 
    public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, 
      Reporter reporter) throws IOException { 

      String line = value.toString(); 

      for(Path cacheFile: dates){ 

        output.collect(new Text(line), new Text(cacheFile.getName())); 

       } 



       } 


      } 
+0

我想要做的是我有一個文件,我傳遞給Mapper。我想比較該文件的每一行的第一列與分佈式緩存文件。如果它存在於該文件中,我想將它傳遞給reducer否則不。 – Pooja3101

+0

如何在您的文件中定義列?你想比較什麼?分佈式緩存中的整個文件? – vefthym

+0

是的,我想比較整個文件。我想將csv文件添加到分佈式緩存中,以便每個映射器都擁有它。請告訴我如何在我的映射器代碼中將其作爲csv文件讀取。 – Pooja3101

回答

0

的內容試試這不是你的configure()方法:

List<String []> lines; 
Path[] files = new Path[0]; 

public void configure(JobConf conf) { 
    lines = new ArrayList<>(); 
    BufferedReader SW; 
    try { 
     files = DistributedCache.getLocalCacheFiles(conf); 
     SW = new BufferedReader(new FileReader(files[0].toString())); 
     String line; 
     while ((line = SW.readLine()) != null) { 
      lines.add(line.split(",")); //now, each lines entry is a String array, with each element being a column 
     } 
     SW.close(); 

    } catch (IOException ioe) { 
     System.err.println("Caught exception while getting cached files: " + 
     StringUtils.stringifyException(ioe)); 
    } 
} 

這樣,您將有文件的內容(在這種情況下,第一文件)分佈式緩存中的變量lines中。每個lines條目表示一個字符串數組,它由','分隔。所以第一行的第一列是lines.get(0)[0],第二行的第三行是lines.get(1)[2]

+0

非常感謝。有效。您是否在Pig中開發分佈式緩存?如果是,請幫助我。我想在豬身上做這件事。我該怎麼做? – Pooja3101

+0

不幸的是,沒有。如果我的答案解決了您的問題,請將其標記爲已接受,以便其他人可以查看您的解決方案。 – vefthym

+0

我想比較整個文件。我想將csv文件添加到分佈式緩存中,以便每個映射器都擁有它。請告訴我如何在我的映射器代碼中將其作爲csv文件讀取。 – Pooja3101