java는 ftp와 http 파일을 hdfs로 직접 전송합니다

5660 단어 javaftphttphdfs
이전에 흐름을 사용하여 http와 ftp의 파일을 로컬로 다운로드하고 로컬 파일을hdfs에 업로드하는 것을 실현했다. 그러면 이제
ftp와 http의 파일을hdfs로 옮겼습니다. 먼저 ftp와 http의 파일을 로컬로 복사해서 hdfs에 업로드할 필요가 없습니다.사실 이 물건의 원리는
간단합니다. 흐름을 사용하여 ftp나 http의 파일을 흐름에 읽고 흐르는 내용을hdfs에 전송하는 것입니다. 이렇게 하면 데이터를 저장할 필요가 없습니다.
로컬 하드디스크에 있습니다. 메모리로 이 이동 과정을 완성할 뿐입니다. 이 도구가 이런 수요를 가진 학생들에게 도움을 줄 수 있기를 바랍니다.
여기에 앞서 몇 가지 도구에 대한 링크를 첨부합니다.
http 도구
ftp 도구
링크 설명
코드는 다음과 같습니다.

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;


public class FileTrans {
  private String head = "";
  private String hostname = "";
  private String FilePath = "";
  private String hdfsFilePath = "";
  private HDFSUtil hdfsutil = null;
  private FtpClient ftp;
  private HttpUtil http;

  public void setFilePath(String FilePath){
    this.FilePath = FilePath;
  }

  public String getFilePath(String FilePath){
    return this.FilePath;
  }

  public void sethdfsFilePath(String hdfsFilePath){
    this.hdfsFilePath = hdfsFilePath;
  }

  public String gethdfsFilePath(String hdfsFilePath){
    return this.hdfsFilePath;
  }

  public void setHostName(String hostname){
    this.hostname = hostname;
  }

  public String getHostName(){
    return this.hostname;
  }

  public void setHead(String head){
    this.head = head;
  }

  public String getHead(){
    return this.head;
  }

  public FileTrans(String head, String hostname, String filepath, String hdfsnode,String hdfsFilepath){
    this.head = head;
    this.hostname = hostname;
    this.FilePath = filepath;
    this.hdfsFilePath = hdfsFilepath;
    if (head.equals("ftp") && hostname != ""){
      this.ftp = new FtpClient(this.hostname);
    }
    if ((head.equals("http") || head .equals("https")) && hostname != ""){
      String httpurl = head + "://" + hostname + "/" + filepath;
      this.http = new HttpUtil(httpurl);
    }
    if (hdfsnode != ""){
      this.hdfsutil = new HDFSUtil(hdfsnode);
    }
    this.hdfsutil.setHdfsPath(this.hdfsFilePath);
    this.hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfsutil.getHdfsPath());
    this.hdfsutil.setHadoopSite("./hadoop-site.xml");
    this.hdfsutil.setHadoopDefault("./hadoop-default.xml");
    this.hdfsutil.setConfigure(false);
  }

  public static void main(String[] args) throws IOException{
    String head = "";
    String hostname = "";
    String filepath = "";
    String hdfsfilepath = "";
    String hdfsnode = "";
    String localpath = "";
    InputStream inStream = null;
    int samplelines = 0;
    try{
      head = args[0];         // ,http ftp
      hostname = args[1];       // hostname
      filepath = args[2];       // 
      hdfsnode = args[3];       //hdfs , hdfs 
      hdfsfilepath = args[4];     //hdfs 
      localpath = args[5];       // , , , samplelines 0
      samplelines = Integer.parseInt(args[6]); // , N , , 0
    }catch (Exception e){
      System.out.println("[FileTrans]:input args error!");
      e.printStackTrace();
    }
    FileTrans filetrans = new FileTrans(head, hostname, filepath, hdfsnode,hdfsfilepath);
    if (filetrans == null){
      System.out.println("filetrans null");
      return;
    }
    if (filetrans.ftp == null && head.equals("ftp")){
      System.out.println("filetrans ftp null");
      return;
    }
    if (filetrans.http == null && (head.equals("http") || head.equals("https"))){
      System.out.println("filetrans ftp null");
      return;
    }
    try{
      if (head.equals("ftp")){
        inStream = filetrans.ftp.getStream(filepath);
        if (samplelines > 0){
          filetrans.ftp.writeStream(inStream, localpath, samplelines);
        }
      }
      else{
        inStream = filetrans.http.getStream(head + "://" + hostname + "/" + filepath);
        if (samplelines > 0){
          filetrans.http.downLoad(head + "://" + hostname + "/" + filepath, localpath, samplelines);
        }
      }
      filetrans.hdfsutil.upLoad(inStream, filetrans.hdfsutil.getFilePath()); 
      if (head == "ftp"){
        filetrans.ftp.disconnect();
      }
    }catch (IOException e){
      System.out.println("[FileTrans]: file trans failed!");
      e.printStackTrace();
    }
    System.out.println("[FileTrans]: file trans success!");
  }

}
번역에 문제가 있으면hadoop 도구의 그 글에서 언급되었으니 참고할 수 있습니다
주: 다른 세 도구의 파일을 같은 디렉터리에 놓는 것이 좋습니다. 함께 놓지 않으면 직접 인용하십시오.
이 도구는 ftp나 http를 hdfs로 옮길 수도 있고, 이전 N줄을 로컬로 저장하여 분석할 수도 있다
이상은 본고에서 기술한 모든 내용입니다. 여러분들이 자바를 배우는 데 도움이 되었으면 합니다.
글을 친구에게 공유하거나 댓글을 남기는 데 시간이 좀 걸리세요.우리는 당신의 지지에 진심으로 감사할 것입니다.

좋은 웹페이지 즐겨찾기