【问题标题】:What does hdfs dfs -getmerge command do?hdfs dfs -getmerge 命令有什么作用?
【发布时间】:2017-01-10 15:31:47
【问题描述】:

作为 hive 查询的结果,我得到了多个输出文件(按排序方式进行了分发),现在我想将它们合并以生成一个文件。所以我尝试了 hdfs dfs -getmerge 命令。现在我想了解 -getmerge 是在连接之前对文件进行排序还是只是连接?

【问题讨论】:

  • 我不认为getmerge 排序。它只是连接起来。
  • @PhaniRahul 我认为如果你看到实现它会排序,它在代码中执行 Arrays.sort 并且默认情况下按升序对文件进行排序。但目前我还没有测试过。

标签: hadoop hdfs


【解决方案1】:
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  }  

对文件数组进行排序(默认升序),source hadoop 0.23

【讨论】:

    【解决方案2】:

    这是文档(适用于 Hadoop 2.7.1): https://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-common/FileSystemShell.html#getmerge

    基本上: 1 - 将文件连接到一个 2 - 可以在连接的文件之间插入一个新行 (-nl)。

    例如: $ hadoop fs -getmerge [-nl] src1 [ src2 [src3]]

    【讨论】:

      猜你喜欢
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多