hadop 2.6 소스 코드 판독 업로드 파일 (- put) 내부 구현 FsShell init () 편
                                            
 12111 단어  빅 데이터hdfshadoophadop 소스 코드
                    
debug 디 버 깅 에 편리 하도록 테스트 프로그램 시 뮬 레이 션 - put 작업 을 작성 합 니 다. 예제 코드 는 다음 과 같 습 니 다.
public class TestShell {
    public static void main(String argv[]) throws Exception {
        System.setProperty("hadoop.home.dir", "D:\\aws\\hadoop-2.6.0");
        System.setProperty("HADOOP_USER_NAME", "root");
        FsShell shell = new FsShell();
        Configuration conf = new Configuration();
        conf.setQuietMode(false);
        shell.setConf(conf);
        String[] args = {"-put","sdsd"};
        int res;
        try {
            res = ToolRunner.run(shell, args);
        } finally {
            shell.close();
        }
        System.exit(res);
    }
}  이 예제 코드 는 FsShell 류 의 main 방법 을 가 져 와 수정 한 ToolRunner. run () 에 뛰 어 들 었 습 니 다.
 public static int run(Tool tool, String[] args) 
    throws Exception{
    return run(tool.getConf(), tool, args);
  }  더욱 뛴다
  public static int run(Configuration conf, Tool tool, String[] args) 
    throws Exception{
    if(conf == null) {
      conf = new Configuration();
    }
    GenericOptionsParser parser = new GenericOptionsParser(conf, args);
    //set the configuration back, so that Tool can configure itself
    tool.setConf(conf);
    //get the args w/o generic hadoop args
    String[] toolArgs = parser.getRemainingArgs();
    return tool.run(toolArgs);
  }  tool. run (toolArgs), 이 코드 는 마지막 에 FsShell 류 의 run 으로 돌 아 왔 습 니 다.
@Override
  public int run(String argv[]) throws Exception {
    // initialize FsShell
    init();
    int exitCode = -1;
    if (argv.length < 1) {
      printUsage(System.err);
    } else {
      String cmd = argv[0];
      Command instance = null;
      try {
        instance = commandFactory.getInstance(cmd);
        if (instance == null) {
          throw new UnknownCommandException();
        }
        exitCode = instance.run(Arrays.copyOfRange(argv, 1, argv.length));
      } catch (IllegalArgumentException e) {
        displayError(cmd, e.getLocalizedMessage());
        if (instance != null) {
          printInstanceUsage(System.err, instance);
        }
      } catch (Exception e) {
        // instance.run catches IOE, so something is REALLY wrong if here
        LOG.debug("Error", e);
        displayError(cmd, "Fatal internal error");
        e.printStackTrace(System.err);
      }
    }
    return exitCode;
  }  매우 중요 한 init 방법 입 니 다. 이 안 은 매우 수 다스 럽 고 초기 화 작업 입 니 다.주로 해당 명령 행 파라미터 와 해당 처리 류 예제 의 맵 관 계 를 구축한다.독자
 protected void init() throws IOException {
    getConf().setQuietMode(true);
    if (commandFactory == null) {
      commandFactory = new CommandFactory(getConf());
      commandFactory.addObject(new Help(), "-help");
      commandFactory.addObject(new Usage(), "-usage");
      registerCommands(commandFactory);
    }
  }  가장 중요 한 것 은 register Commands (commandFactory) 입 니 다.
 protected void registerCommands(CommandFactory factory) {
    // TODO: DFSAdmin subclasses FsShell so need to protect the command
    // registration.  This class should morph into a base class for
    // commands, and then this method can be abstract
    if (this.getClass().equals(FsShell.class)) {
      factory.registerCommands(FsCommand.class);
    }
  }  factory. registerCommands () 로 이동
 public void registerCommands(Class> registrarClass) {
    try {
      registrarClass.getMethod(
          "registerCommands", CommandFactory.class
      ).invoke(null, this);
    } catch (Exception e) {
      throw new RuntimeException(StringUtils.stringifyException(e));
    }
  }  CommandFactory 방법 register Commands 의 디자인 방향 을 분석 하고,
  public static void registerCommands(CommandFactory factory) {
    factory.registerCommands(AclCommands.class);
    factory.registerCommands(CopyCommands.class);
    factory.registerCommands(Count.class);
    factory.registerCommands(Delete.class);
    factory.registerCommands(Display.class);
    factory.registerCommands(FsShellPermissions.class);
    factory.registerCommands(FsUsage.class);
    factory.registerCommands(Ls.class);
    factory.registerCommands(Mkdir.class);
    factory.registerCommands(MoveCommands.class);
    factory.registerCommands(SetReplication.class);
    factory.registerCommands(Stat.class);
    factory.registerCommands(Tail.class);
    factory.registerCommands(Test.class);
    factory.registerCommands(Touch.class);
    factory.registerCommands(SnapshotCommands.class);
    factory.registerCommands(XAttrCommands.class);
  }  순환 하 는 느낌 이 듭 니 다. factory. registerCommands, Fs Command 하위 클래스 의 registerCommands 방법 을 계속 실행 합 니 다.
Copy Commands 를 골 라 서 들 어가 서 실례 와 명령 행 인자 의 매 핑 관 계 를 볼 수 있 습 니 다
  public static void registerCommands(CommandFactory factory) {
    factory.addClass(Merge.class, "-getmerge");
    factory.addClass(Cp.class, "-cp");
    factory.addClass(CopyFromLocal.class, "-copyFromLocal");
    factory.addClass(CopyToLocal.class, "-copyToLocal");
    factory.addClass(Get.class, "-get");
    factory.addClass(Put.class, "-put");
    factory.addClass(AppendToFile.class, "-appendToFile");
  }
  총결산
반사 적 인 방식 으로 인 스 턴 스 와 명령 행 매개 변수 에 대한 매 핑 관 계 를 층 층 이 등록 하고 인 스 턴 스 등록 트 리 를 구축 하여 공정 화 와 공정 확장 에 편리 하 므 로 여러분 이 축적 할 가치 가 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
spark 의 2: 원리 소개Google Map/Reduce 를 바탕 으로 이 루어 진 Hadoop 은 개발 자 에 게 map, reduce 원 어 를 제공 하여 병렬 일괄 처리 프로그램 을 매우 간단 하고 아름 답 게 만 들 었 습 니 다.S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.