【问题标题】:Accessing files in hadoop distributed cache访问hadoop分布式缓存中的文件
【发布时间】:2012-12-06 15:10:09
【问题描述】:

我想使用分布式缓存来允许我的映射器访问数据。我主要使用命令

DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf);

其中 /user/peter/cacheFile/testCache1 是存在于 hdfs 中的文件

然后,我的设置函数如下所示:

public void setup(Context context) throws IOException, InterruptedException{
    Configuration conf = context.getConfiguration();
    Path[] localFiles = DistributedCache.getLocalCacheFiles(conf);
    //etc
}

但是,这个 localFiles 数组始终为空。

我最初在单主机集群上运行以进行测试,但我读到这将阻止分布式缓存工作。我尝试了一个伪分布式,但这也没有用

我正在使用 hadoop 1.0.3

谢谢 彼得

【问题讨论】:

标签: hadoop


【解决方案1】:

这里的问题是我正在执行以下操作:

Configuration conf = new Configuration();
Job job = new Job(conf, "wordcount");
DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf);

由于 Job 构造函数会生成 conf 实例的内部副本,因此之后添加缓存文件不会影响任何事情。相反,我应该这样做:

Configuration conf = new Configuration();
DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf);
Job job = new Job(conf, "wordcount");

现在它可以工作了。感谢 Harsh on hadoop 用户列表的帮助。

【讨论】:

  • 苛刻确实是天赐之物! ..浪费了相当多的时间。谢谢!
【解决方案2】:
Configuration conf = new Configuration();  
Job job = new Job(conf, "wordcount");
DistributedCache.addCacheFile(new URI("/userpetercacheFiletestCache1"),job.getConfiguration());

你也可以这样做。

【讨论】:

    【解决方案3】:

    一旦使用配置对象分配作业, 即Configuration conf = new Configuration();

    Job job = new Job(conf, "wordcount");
    

    然后如果处理conf的属性如下图,例如

    conf.set("demiliter","|");
    

    DistributedCache.addCacheFile(new URI("/user/peter/cacheFile/testCache1"), conf);
    

    此类更改不会反映在伪集群或集群中,无论它如何与本地环境一起工作。

    【讨论】:

      【解决方案4】:

      这个版本的代码(与上述结构略有不同)一直对我有用。

      //in main(String [] args)
      Job job = new Job(conf,"Word Count"); 
      ...
      DistributedCache.addCacheFile(new URI(/user/peter/cacheFile/testCache1), job.getConfiguration());
      

      我没有在 Mapper 代码中看到完整的 setup() 函数

      public void setup(Context context) throws IOException, InterruptedException {
      
          Configuration conf = context.getConfiguration();
          FileSystem fs = FileSystem.getLocal(conf);
      
          Path[] dataFile = DistributedCache.getLocalCacheFiles(conf);
      
          // [0] because we added just one file.
          BufferedReader cacheReader = new BufferedReader(new InputStreamReader(fs.open(dataFile[0])));
          // now one can use BufferedReader's readLine() to read data
      
      }
      

      【讨论】:

      • 谢谢@Somum,它对我有用。我检查了 hadoop 1.2.1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多