2014-07-22 61 views
2

作为配置单元查询的结果,我获得了多个输出文件(按排序分配),现在我想合并它们以生成单个文件。所以我试过hdfs dfs -getmerge命令。现在我想了解-getmerge是否在连接之前对文件进行排序,或者它只是连接在一起?hdfs dfs -getmerge命令有什么作用?

+0

我不认为'getmerge'排序。它只是连接在一起。 –

+0

@PhaniRahul我认为它可以分类,如果你看到实现,它会在代码中执行一个Arrays.sort,并按默认顺序升序排列这些文件。但到目前为止我还没有测试过它。 –

回答

4
public static boolean More ...copyMerge(FileSystem srcFS, Path srcDir, 
277         FileSystem dstFS, Path dstFile, 
278         boolean deleteSource, 
279         Configuration conf, String addString) throws IOException { 
280 dstFile = checkDest(srcDir.getName(), dstFS, dstFile, false); 
281 
282 if (!srcFS.getFileStatus(srcDir).isDirectory()) 
283  return false; 
284 
285 OutputStream out = dstFS.create(dstFile); 
286  
287 try { 
288  FileStatus contents[] = srcFS.listStatus(srcDir); 
289  Arrays.sort(contents); 
290  for (int i = 0; i < contents.length; i++) { 
291  if (contents[i].isFile()) { 
292   InputStream in = srcFS.open(contents[i].getPath()); 
293   try { 
294   IOUtils.copyBytes(in, out, conf, false); 
295   if (addString!=null) 
296    out.write(addString.getBytes("UTF-8")); 
297     
298   } finally { 
299   in.close(); 
300   } 
301  } 
302  } 
303 } finally { 
304  out.close(); 
305 } 
306  
307 
308 if (deleteSource) { 
309  return srcFS.delete(srcDir, true); 
310 } else { 
311  return true; 
312 } 
313 } 

它排序文件阵列(默认升序),源的hadoop 0.23