io异常?

huwehgph  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(364)

我在这里学习使用分布式缓存的教程。我对代码做了一些细微的修改,使其与hadoop2.2兼容。
我发现当 loadStopWords 方法,引发io异常:
我确认stop_words.txt已复制到 HDFS . 为了简单起见,我省略了mapper和reducer代码。
这是我的密码:

public static final String LOCAL_STOPWORD_LIST =
              "/Users/sridhar/Documents/hadoop/stop_words.txt";

    public static final String HDFS_STOPWORD_LIST = "/data/stop_words.txt";

    //copies local file to HDFS and adds to Job's cache file
    static  void cacheStopWordList(Configuration conf, Job job) throws IOException, URISyntaxException {
        FileSystem fs = FileSystem.get(conf);
        URI hdfsPath = new URI(HDFS_STOPWORD_LIST);

        System.out.println("coping files to HDFS");

        // upload the file to hdfs. Overwrite any existing copy.
        fs.copyFromLocalFile(false, true, new Path(LOCAL_STOPWORD_LIST),
            new Path(hdfsPath));

        System.out.println("done copying HDFS");
        job.addCacheFile(hdfsPath);
      }

    protected void setup(Context context) {
            try {
              String stopwordCacheName = new Path(HDFS_STOPWORD_LIST).toString();
              URI[] cacheFiles = context.getCacheFiles();

              System.out.println(Arrays.toString(cacheFiles));

              if (null != cacheFiles && cacheFiles.length > 0) {
                for (URI cacheURI : cacheFiles) {
                    System.out.println(cacheURI.toString());
                    System.out.println(stopwordCacheName);
                     System.out.println("-----------------");
                  if (cacheURI.toString().equals(stopwordCacheName)) {
                      System.out.println("****************************************");
                    loadStopWords(new Path(cacheURI)); // IT BREAKS HERE
                    System.out.println(stopWords);
                    break;
                  }
                }
              }
            } catch (IOException ioe) {
              System.err.println("IOException reading from distributed cache");
              System.err.println(ioe.toString());
            }
          }

        void loadStopWords(Path cachePath) throws IOException {
            // note use of regular java.io methods here - this is a local file now
            BufferedReader wordReader = new BufferedReader(
                new FileReader(cachePath.toString()));
            try {
              String line;
              this.stopWords = new HashSet<String>();
              while ((line = wordReader.readLine()) != null) {
                this.stopWords.add(line.toLowerCase());
              }
            } finally {
              wordReader.close();
            }
          }

public static void main(String[] args) throws IllegalArgumentException, IOException, InterruptedException, ClassNotFoundException, URISyntaxException {

Job job = new Job();
job.setJarByClass(LineIndexer.class);
job.setJobName("LineIndexer");
Configuration conf = job.getConfiguration();
cacheStopWordList(conf,job);
}
atmip9wb

atmip9wb1#

在您提供的链接中,提到了使用distributedcache.addcachefile()。下面是使用分布式缓存来分发文件的段落,在设置作业时创建distributedcache类的示例。使用distributedcache.addcachefile()方法添加应发送到系统上所有节点的文件名。
而不是写作

job.addCacheFile(hdfsPath);

试着写作

DistributedCache.addCacheFile(hdfsPath, job.getConfiguration());
6ioyuze2

6ioyuze22#

我想你应该试试 Path[] localPaths = context.getLocalCacheFiles(); 而不是 context.getCacheFiles(); 如果有用就告诉我

相关问题