HDFS中JAVA API的使用
HDFS是一個(gè)分布式文件系統(tǒng),既然是文件系統(tǒng),就可以對(duì)其文件進(jìn)行操作,比如說(shuō)新建文件、刪除文件、讀取文件內(nèi)容等操作。下面記錄一下使用JAVA API對(duì)HDFS中的文件進(jìn)行操作的過(guò)程。
對(duì)分HDFS中的文件操作主要涉及一下幾個(gè)類(lèi):
Configuration類(lèi):該類(lèi)的對(duì)象封轉(zhuǎn)了客戶(hù)端或者服務(wù)器的配置。
FileSystem類(lèi):該類(lèi)的對(duì)象是一個(gè)文件系統(tǒng)對(duì)象,可以用該對(duì)象的一些方法來(lái)對(duì)文件進(jìn)行操作。FileSystem fs = FileSystem.get(conf);通過(guò)FileSystem的靜態(tài)方法get獲得該對(duì)象。
FSDataInputStream和FSDataOutputStream:這兩個(gè)類(lèi)是HDFS中的輸入輸出流。分別通過(guò)FileSystem的open方法和create方法獲得。
具體如何對(duì)文件操作清下下面例子:
package com.hdfs; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class HdfsTest { //創(chuàng)建新文件 public static void createFile(String dst , byte[] contents) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path dstPath = new Path(dst); //目標(biāo)路徑 //打開(kāi)一個(gè)輸出流 FSDataOutputStream outputStream = fs.create(dstPath); outputStream.write(contents); outputStream.close(); fs.close(); System.out.println("文件創(chuàng)建成功!"); } //上傳本地文件 public static void uploadFile(String src,String dst) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(src); //原路徑 Path dstPath = new Path(dst); //目標(biāo)路徑 //調(diào)用文件系統(tǒng)的文件復(fù)制函數(shù),前面參數(shù)是指是否刪除原文件,true為刪除,默認(rèn)為false fs.copyFromLocalFile(false,srcPath, dstPath); //打印文件路徑 System.out.println("Upload to "+conf.get("fs.default.name")); System.out.println("------------list files------------"+"\n"); FileStatus [] fileStatus = fs.listStatus(dstPath); for (FileStatus file : fileStatus) { System.out.println(file.getPath()); } fs.close(); } //文件重命名 public static void rename(String oldName,String newName) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path oldPath = new Path(oldName); Path newPath = new Path(newName); boolean isok = fs.rename(oldPath, newPath); if(isok){ System.out.println("rename ok!"); }else{ System.out.println("rename failure"); } fs.close(); } //刪除文件 public static void delete(String filePath) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path path = new Path(filePath); boolean isok = fs.deleteOnExit(path); if(isok){ System.out.println("delete ok!"); }else{ System.out.println("delete failure"); } fs.close(); } //創(chuàng)建目錄 public static void mkdir(String path) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(path); boolean isok = fs.mkdirs(srcPath); if(isok){ System.out.println("create dir ok!"); }else{ System.out.println("create dir failure"); } fs.close(); } //讀取文件的內(nèi)容 public static void readFile(String filePath) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(filePath); InputStream in = null; try { in = fs.open(srcPath); IOUtils.copyBytes(in, System.out, 4096, false); //復(fù)制到標(biāo)準(zhǔn)輸出流 } finally { IOUtils.closeStream(in); } } public static void main(String[] args) throws IOException { //測(cè)試上傳文件 //uploadFile("D:\\c.txt", "/user/hadoop/test/"); //測(cè)試創(chuàng)建文件 /*byte[] contents = "hello world 世界你好\n".getBytes(); createFile("/user/hadoop/test1/d.txt",contents);*/ //測(cè)試重命名 //rename("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt"); //測(cè)試刪除文件 //delete("test/dd.txt"); //使用相對(duì)路徑 //delete("test1"); //刪除目錄 //測(cè)試新建目錄 //mkdir("test1"); //測(cè)試讀取文件 readFile("test1/d.txt"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin語(yǔ)言編程Regex正則表達(dá)式實(shí)例詳解
這篇文章主要為大家介紹了Kotlin語(yǔ)言編程Regex正則表達(dá)式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08springboot使用外置tomcat啟動(dòng)方式
這篇文章主要介紹了springboot使用外置tomcat啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11使用JavaBean根據(jù)指定條件設(shè)置屬性值默認(rèn)值方式
這篇文章主要介紹了使用JavaBean根據(jù)指定條件設(shè)置屬性值默認(rèn)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Springboot四種事件監(jiān)聽(tīng)的實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Springboot四種事件監(jiān)聽(tīng)的實(shí)現(xiàn)方式,事件監(jiān)聽(tīng)是一種機(jī)制,可以定義和觸發(fā)自定義的事件,以及在應(yīng)用程序中注冊(cè)監(jiān)聽(tīng)器來(lái)響應(yīng)這些事件,需要的朋友可以參考下2022-06-06從Hello?World開(kāi)始理解GraphQL背后處理及執(zhí)行過(guò)程
這篇文章主要為大家介紹了從Hello?World開(kāi)始理解GraphQL背后處理過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08如何用Jfinal連接多個(gè)數(shù)據(jù)庫(kù)
這篇文章主要介紹了如何用Jfinal連接多個(gè)數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Jfinal,感興趣的朋友可以了解下2021-03-03IDEA2022版本創(chuàng)建maven?web項(xiàng)目的兩種方式詳解
創(chuàng)建maven?web項(xiàng)目有兩種方式,一種是使用骨架方式,一種是不使用骨架的方式,本文結(jié)合實(shí)例代碼給大家介紹了IDEA2022版本創(chuàng)建maven?web項(xiàng)目的兩種方式,需要的朋友可以參考下2023-02-02Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
正所謂拳不離手曲不離口,java學(xué)習(xí)的過(guò)程中,練習(xí)還是要多一點(diǎn)比較好。接下來(lái)分享給大家一個(gè)Java編程的小實(shí)例,供朋友們參考。2017-10-10SpringMVC修改返回值類(lèi)型后的消息轉(zhuǎn)換器處理方式
這篇文章主要介紹了SpringMVC修改返回值類(lèi)型后的消息轉(zhuǎn)換器處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09