首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >distcp java api退出应用程序

distcp java api退出应用程序
EN

Stack Overflow用户
提问于 2013-09-06 02:57:57
回答 1查看 2.1K关注 0票数 1

我需要在aws s3和本地hdfs之间复制文件,我尝试使用distcp java api,但它的问题是在System.exit()的末尾,它也停止了我的应用程序,所以如果我有多个文件夹/文件要复制,并且我使用了多个线程,每个线程执行一个distcp命令,完成distcp的第一个线程将停止应用程序,从而停止distcp的其余部分,有没有其他方法可以避免这种情况,我知道我可以编写自己的MR作业来执行复制,但想知道是否有其他选择。

我的代码:

代码语言:javascript
复制
List<Future<Void>> calls = new ArrayList<Future<Void>>();       
for (String dir : s3Dirs) {
    final String[] args = new String[4];
    args[0] = "-log";   
    args[1] = LOG_DIR;
    args[2] = S3_DIR;
    args[3] = LOCAL_HDFS_DIR

    calls.add(_exec.submit(new Callable<Void>() {
       @Override
       public Void call() throws Exception {                
         try {
        DistCp.main(args);      <-- Distcp command          
         } catch (Exception e) {
        System.out.println("Failed to copy files from " + args[2] + " to " + args[3]);
         }
         return null;
    }
    }));            
}

for (Future<Void> f : calls) {
    try {
        f.get();
    } catch (Exception e) {
        LOGGER.error("Error while distcp", e);
    }   
}

Distcp main()

代码语言:javascript
复制
public static void main(String argv[]) {

        int exitCode;
        try {
          DistCp distCp = new DistCp();
          Cleanup CLEANUP = new Cleanup(distCp);

          ShutdownHookManager.get().addShutdownHook(CLEANUP,
            SHUTDOWN_HOOK_PRIORITY);
          exitCode = ToolRunner.run(getDefaultConf(), distCp, argv);
        }
        catch (Exception e) {
          LOG.error("Couldn't complete DistCp operation: ", e);
          exitCode = DistCpConstants.UNKNOWN_ERROR;
        }
        System.exit(exitCode);        <--- exit here
      }
EN

回答 1

Stack Overflow用户

发布于 2014-08-14 01:08:01

我以前使用过distcp,从来没有遇到过System.exit()问题,即使是在多线程的情况下也是如此。尝试使用ToolRunner来调用Distcp调用(就像使用on the Distcp Test cases from the hadoop tools package一样),而不是像那样使用distcp。Distcp测试用例使用ToolRunner来运行distcp,它允许您使用多个线程运行它。我从上面的链接中复制了代码片段:

代码语言:javascript
复制
public void testCopyFromLocalToLocal() throws Exception {
  Configuration conf = new Configuration();
  FileSystem localfs = FileSystem.get(LOCAL_FS, conf);
  MyFile[] files = createFiles(LOCAL_FS, TEST_ROOT_DIR+"/srcdat");
  ToolRunner.run(new DistCp(new Configuration()),
                         new String[] {"file:///"+TEST_ROOT_DIR+"/srcdat",
                                       "file:///"+TEST_ROOT_DIR+"/destdat"});
  assertTrue("Source and destination directories do not match.",
             checkFiles(localfs, TEST_ROOT_DIR+"/destdat", files));
  deldir(localfs, TEST_ROOT_DIR+"/destdat");
  deldir(localfs, TEST_ROOT_DIR+"/srcdat");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18643917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档