当前位置: 移动技术网 > IT编程>开发语言>Java > java实现将ftp和http的文件直接传送到hdfs

java实现将ftp和http的文件直接传送到hdfs

2019年07月22日  | 移动技术网IT编程  | 我要评论

之前实现了使用流来讲http和ftp的文件下载到本地,也实现了将本地文件上传到hdfs上,那现在就可以做到将
ftp和http的文件转移到hdfs上了,而不用先将ftp和http的文件拷贝到本地再上传到hdfs上了。其实这个东西的原理
很简单,就是使用流,将ftp或http的文件读入到流中,然后将流中的内容传送到hdfs上,这样子就不用让数据存到
本地的硬盘上了,只是让内存来完成这个转移的过程,希望这个工具,能够帮到有这样需求的同学~
这里先附上之前的几个工具的链接:



链接描述

代码如下:

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有所帮助。

请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网