2014-02-19 51 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