您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了Java代碼對(duì)HDFS進(jìn)行增刪改查操作的具體代碼,供大家參考,具體內(nèi)容如下
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import org.apache.commons.compress.utils.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class FileOpreation { public static void main(String[] args) throws IOException { //CreateFile(); //DeleteFile(); //CopyFileToHDFS(); //MkDirs(); //DelDirs(); ListDirectory(); DownLoad(); } public static void CreateFile() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration =new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); byte[] file_content_buff="hello hadoop world, test write file !\n".getBytes(); Path dfs = new Path("/home/test.txt"); FSDataOutputStream outputStream = fSystem.create(dfs); outputStream.write(file_content_buff.length); } public FileOpreation() { // TODO Auto-generated constructor stub }public static void DeleteFile() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration =new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); Path deletf = new Path("/home/test.txt"); boolean delResult = fSystem.delete(deletf,true); System.out.println(delResult==true?"刪除成功":"刪除失敗"); } public static void CopyFileToHDFS() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration =new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); Path src = new Path("E:\\SerializationTest\\APITest.txt"); Path dest_src = new Path("/home"); fSystem.copyFromLocalFile(src, dest_src); } public static void MkDirs() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration =new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); Path src = new Path("/Test"); fSystem.mkdirs(src); } public static void DelDirs() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration = new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); Path src = new Path("/Test"); fSystem.delete(src); } public static void ListDirectory() throws IOException { String uri = "hdfs://Alvis:9000"; Configuration configuration = new Configuration(); FileSystem fSystem = FileSystem.get(URI.create(uri), configuration); FileStatus[] fStatus = fSystem.listStatus(new Path("/output")); for(FileStatus status : fStatus) if (status.isFile()) { System.out.println("文件路徑:"+status.getPath().toString()); System.out.println("文件路徑 getReplication:"+status.getReplication()); System.out.println("文件路徑 getBlockSize:"+status.getBlockSize()); BlockLocation[] blockLocations = fSystem.getFileBlockLocations(status, 0, status.getBlockSize()); for(BlockLocation location : blockLocations){ System.out.println("主機(jī)名:"+location.getHosts()[0]); System.out.println("主機(jī)名:"+location.getNames()[0]); } } else { System.out.println("directory:"+status.getPath().toString()); } } public static void DownLoad() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://Alvis:9000"); FileSystem fSystem =FileSystem.get(configuration); FSDataInputStream inputStream =fSystem.open( new Path("/input/wc.jar")); FileOutputStream outputStream = new FileOutputStream(new File("E:\\LearnLife\\DownLoad\\wc.jar")); IOUtils.copy(inputStream, outputStream); System.out.println("下載成功!"); } }
思想:
一、定義虛擬機(jī)接口
二、先拿到HDFS遠(yuǎn)程調(diào)用接口對(duì)象Configuration
三、定義分布式文件系統(tǒng)FileSystem對(duì)象獲取對(duì)象
四、給定路徑
五、用FileSystem對(duì)象調(diào)用操作
以上所述是小編給大家介紹的Java代碼對(duì)HDFS進(jìn)行增刪改查操作詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。