Before the implementation of the use of streaming HTTP and FTP files to download to the local, but also to achieve the local file upload to the HDFs, it can now do
FTP and HTTP files are transferred to HDFs without first copying FTP and HTTP files to the local HDFs. Actually, the principle of this thing
The simple thing is to use the stream, read FTP or HTTP files into the stream, and then transfer the contents of the stream to the HDFs, so that the data is not saved to
Local hard drive, just let the memory to complete the transfer process, I hope this tool, can help to have such a demand for students ~
Here is a link to some of the previous tools:
HTTP Tools
FTP Tools
Link description
The code is as follows:
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; 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);
The public static void main (string[] args) is throws ioexception{String head = "";
String hostname = "";
String filepath = "";
String Hdfsfilepath = ""; String Hdfsnode = "";
String localpath = "";
InputStream instream = null;
int samplelines = 0; try{head = args[0]; Remote server type, HTTP or ftp hostname = args[1]; Remote server hostname filepath = args[2]; Remote file path Hdfsnode = args[3]; HDFs machine name, with HDFs opening hdfsfilepath = args[4]; HDFs file path localpath = args[5]; If you need to save a copy locally, enter the local path, do not save, pass in a space or samplelines incoming 0 samplelines = Integer.parseint (args[6));
Save in the local words, save the first n lines, if not saved, fill in 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, Sampleline
s);
} 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!");
}
}
If there is a problem with the compilation, as mentioned in the Hadoop tool article, you can refer to the
Note: It is best to put the files of the other three tools in the same directory, if not put together, then please quote
This tool can either transfer FTP or HTTP to HDFs, or save the first n rows locally for analysis
The above is the entire content described in this article, I hope to be able to learn Java help.
Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!