java實(shí)現(xiàn)將ftp和http的文件直接傳送到hdfs
之前實(shí)現(xiàn)了使用流來(lái)講http和ftp的文件下載到本地,也實(shí)現(xiàn)了將本地文件上傳到hdfs上,那現(xiàn)在就可以做到將
ftp和http的文件轉(zhuǎn)移到hdfs上了,而不用先將ftp和http的文件拷貝到本地再上傳到hdfs上了。其實(shí)這個(gè)東西的原理
很簡(jiǎn)單,就是使用流,將ftp或http的文件讀入到流中,然后將流中的內(nèi)容傳送到hdfs上,這樣子就不用讓數(shù)據(jù)存到
本地的硬盤(pán)上了,只是讓內(nèi)存來(lái)完成這個(gè)轉(zhuǎn)移的過(guò)程,希望這個(gè)工具,能夠幫到有這樣需求的同學(xué)~
這里先附上之前的幾個(gè)工具的鏈接:
代碼如下:
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]; //遠(yuǎn)端服務(wù)器類(lèi)型,http還是ftp hostname = args[1]; //遠(yuǎn)端服務(wù)器hostname filepath = args[2]; //遠(yuǎn)端文件路徑 hdfsnode = args[3]; //hdfs的機(jī)器名,不帶hdfs開(kāi)頭 hdfsfilepath = args[4]; //hdfs的文件路徑 localpath = args[5]; //如果需要在本地保存一份的話(huà),輸入本地的路徑,不保存,傳入空格或者samplelines傳入0 samplelines = Integer.parseInt(args[6]); //保存在本地的話(huà),保存前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!"); } }
編譯有問(wèn)題的話(huà),在hadoop工具的那篇文章中有提到,可以參考
注:最好將其他三個(gè)工具的文件放在同一個(gè)目錄下,如果不放在一起,那么請(qǐng)自行引用
這個(gè)工具既可以將ftp或者h(yuǎn)ttp轉(zhuǎn)移到hdfs,也能將前N行保存到本地,進(jìn)行分析
以上就是本文所述的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹覍W(xué)習(xí)java有所幫助。
請(qǐng)您花一點(diǎn)時(shí)間將文章分享給您的朋友或者留下評(píng)論。我們將會(huì)由衷感謝您的支持!
- 使用Java Api操作HDFS過(guò)程詳解
- 編寫(xiě)Java代碼對(duì)HDFS進(jìn)行增刪改查操作代碼實(shí)例
- HDFS的Java API的訪問(wèn)方式實(shí)例代碼
- JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn)
- HDFS中JAVA API的使用
- java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs
- Java訪問(wèn)Hadoop分布式文件系統(tǒng)HDFS的配置說(shuō)明
- java連接hdfs ha和調(diào)用mapreduce jar示例
- JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案
相關(guān)文章
java Spring整合Freemarker的詳細(xì)步驟
本文對(duì)Spring整合Freemarker步驟做了詳細(xì)的說(shuō)明,按步驟操作一定可以整合通過(guò),這里提供給大家做參考2013-11-11springboot之Validation參數(shù)校驗(yàn)詳細(xì)解讀
這篇文章主要介紹了springboot之Validation參數(shù)校驗(yàn)詳細(xì)解讀,本篇是關(guān)于springboot的參數(shù)校驗(yàn)知識(shí),當(dāng)然也適用其它java應(yīng)用,讀完本篇將學(xué)會(huì)基本的參數(shù)校驗(yàn),自定義參數(shù)校驗(yàn)和分組參數(shù)校驗(yàn),需要的朋友可以參考下2023-10-10SpringBoot配置文件方式,在線(xiàn)yml文件轉(zhuǎn)properties
這篇文章主要介紹了SpringBoot配置文件方式,在線(xiàn)yml文件轉(zhuǎn)properties,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07MyBatis-Plus解決邏輯刪除與唯一索引的問(wèn)題
本文主要介紹了MyBatis-Plus解決邏輯刪除與唯一索引的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08java servlet獲得客戶(hù)端相關(guān)信息的簡(jiǎn)單代碼
這篇文章主要介紹了java servlet獲得客戶(hù)端相關(guān)信息的簡(jiǎn)單代碼,有需要的朋友可以參考一下2013-12-12java代碼塊之簡(jiǎn)易qq登錄界面及按鈕顏色設(shè)置代碼
這篇文章主要介紹了java代碼塊之簡(jiǎn)易qq登錄界面及按鈕顏色設(shè)置代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11spring event 事件異步處理方式(發(fā)布,監(jiān)聽(tīng),異步處理)
這篇文章主要介紹了spring event 事件異步處理方式(發(fā)布,監(jiān)聽(tīng),異步處理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02SpringMVC中@RequestMapping注解用法實(shí)例
通過(guò)@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06ReentrantReadWriteLock?讀寫(xiě)鎖分析總結(jié)
這篇文章主要介紹了ReentrantReadWriteLock 讀寫(xiě)鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫(xiě)鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下2022-05-05Spring boot通過(guò)AOP防止API重復(fù)請(qǐng)求代碼實(shí)例
這篇文章主要介紹了Spring boot通過(guò)AOP防止API重復(fù)請(qǐng)求代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12