java完成將ftp和http的文件直接傳送到hdfs。本站提示廣大學習愛好者:(java完成將ftp和http的文件直接傳送到hdfs)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成將ftp和http的文件直接傳送到hdfs正文
之前完成了應用流來說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有所贊助。
請您花一點時光將文章分享給您的同伙或許留下評論。我們將會由衷感激您的支撐!