在mapper中逐行读取HDFS中的文本文件
以下是Mappers的代码,从HDFS读取文本文件对吗? 如果是:
- 如果不同节点中的两个映射器几乎同时尝试打开文件会发生什么?
- 是不是需要关闭
InputStreamReader
? 如果是这样,如何在不关闭文件系统的情况下执行此操作?
我的代码是:
Path pt=new Path("hdfs://pathTofile"); FileSystem fs = FileSystem.get(context.getConfiguration()); BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt))); String line; line=br.readLine(); while (line != null){ System.out.println(line);
这将是有效的,有一些修改 – 我假设您粘贴的代码被截断:
Path pt=new Path("hdfs://pathTofile"); FileSystem fs = FileSystem.get(context.getConfiguration()); BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt))); try { String line; line=br.readLine(); while (line != null){ System.out.println(line); // be sure to read the next line otherwise you'll get an infinite loop line = br.readLine(); } } finally { // you should close out the BufferedReader br.close(); }
您可以使用多个映射器读取同一个文件,但是使用分布式缓存更有意义(不仅减少了承载文件块的数据节点的负载,而且效率更高)如果你的工作任务数量大于任务节点数
- Pipeling hadoop map减少了工作量
- 调用工作的差异
- 从Java写入HDFS,“只能复制到0个节点而不是minReplication”
- 使用Hadoop分布式缓存时出现FileNotFoundException
- Map-Reduce中的二级排序
- 多输出路径(Java – Hadoop – MapReduce)
- Apache Hadoop setXIncludeAware UnsupportedOperationException
- java.lang.NoSuchFieldError:IBM_JAVA,用于Eclipse中的简单hbase Java客户端
- 不推荐使用MapReduceBase和Mapper