java實現將ftp和http的檔案直接傳送到hdfs_java

來源:互聯網
上載者:User

之前實現了使用流來講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行儲存到本地,進行分析

以上就是本文所述的全部內容了,希望能夠對大家學習java有所協助。

請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.