【问题标题】:Get files names and content , and then merge into another file with mapreduce获取文件名和内容,然后用 mapreduce 合并到另一个文件中
【发布时间】:2017-01-12 10:26:34
【问题描述】:
我有几个包含数据的文件。
例如:file01.csv 里面有 x 线,file02.csv 里面有 y 线。
我想用 mapreduce 处理和合并它们,以便得到一个文件,其中 x 行以 file01 开头,然后是行内容,y 文件以 file02 开头,然后是行内容。
我有两个问题:
- 我知道如何通过设置
FileInputFormat.setInputPath(job, new Path(inputFile)); 使用 mapreduce 从文件中获取行
但我不明白如何获取文件夹中每个文件的行。
- 一旦我的映射器中有这些行,我如何访问相应的文件名,以便创建我想要的数据?
感谢您的考虑。
琥珀色
【问题讨论】:
标签:
file
hadoop
merge
path
mapreduce
【解决方案1】:
在您的情况下,您不需要 map-reduce。那是因为您想保留结果文件中的行顺序。在这种情况下,单线程处理会更快。
只需使用如下代码运行 java 客户端:
FileSystem fs = FileSystem.get();
OutputStream os = fs.create(outputPath); // stream for result file
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
for (String inputFile : inputs) { // reading input files
InputStream is = fs.open(new Path(inputFile));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
br.close();
}
pw.close();