JavaAPI實現hdfs檔案增刪改查
阿新 • • 發佈:2019-01-27
package com.aimuti.hadoop.hdfs; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.junit.Before; import org.junit.Test; /* * 用客戶端的JavaAPI實現增刪改查 */ public class HdfsClientDemo { Configuration conf; FileSystem fs; @Before public void init() throws IOException, InterruptedException, URISyntaxException { /** * 初始化操作應該獲得一個分散式系統的客戶端 * 利用分散式系統的JavaAPI拿到客戶端實現通訊 * 構造一個配置引數物件,設定引數:我們訪問HDFS的uri * 從而FileSystem.get()方法就知道應該去構造一個 * 訪問HDFS檔案系統的客戶端,以及new Configuration() * 的時候,它會去載入jar包的hdfs-default.xml然後載入 * classpath下的hdfs-site.xml */ conf = new Configuration(); /* * 引數優先順序: 1.客戶端程式碼中設定的值 2.classpath下使用者自定義配置 * 3.伺服器預設配置 */ conf.set("dfs.replication", "2"); conf.set("dfs.block.size", "64m"); fs = FileSystem.get(new URI("hdfs://cdh:9000"), conf, "root"); } // 本地檔案上傳hdfs @Test public void testUpload() throws IllegalArgumentException, IOException { fs.copyFromLocalFile(new Path("C:\\Hello.java"), new Path("/Hello_copy")); fs.close(); } // hdfs檔案下載到本地 @Test public void testDownload() throws IllegalArgumentException, IOException { // true是指呼叫本地Java的IO流進行寫資料 fs.copyToLocalFile(false, new Path("/Hello_copy"), new Path("D:\\"), true); fs.close(); } // 檔案的建立 多級檔案目錄的建立 @Test public void testMkdir() throws IllegalArgumentException, IOException { boolean mkdirs = fs.mkdirs(new Path("/BBB/bbb/ccc")); System.out.println(mkdirs); } // 檔案的刪除 @Test public void testDelete() throws IllegalArgumentException, IOException { // true:指的是是否遞迴刪除 boolean delete = fs.delete(new Path("/BBB/bbb"), false); System.out.println(delete); } // 查詢指定目錄下的所有子檔案 @Test public void testList() throws FileNotFoundException, IllegalArgumentException, IOException { RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), false); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println("blockSize:"+fileStatus.getBlockSize()); System.out.println("owner:"+fileStatus.getOwner()); System.out.println("Replication:"+fileStatus.getReplication()); System.out.println("permission:"+fileStatus.getPermission()); System.out.println("name:"+fileStatus.getPath().getName()); } } }
package com.aimuti.hadoop.hdfs; /* * 用流的方式來操作hdfs上的檔案,可以實現讀取指定偏移量範圍內的資料 */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.URI; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.Before; import org.junit.Test; public class HdfsStreamAccess { Configuration conf; FileSystem fs; @Before public void init() throws Exception { // 初始化操作應該獲取一個分散式系統客戶端 // 利用分散式系統的JavaAPI獲取客戶端實現通訊 conf = new Configuration(); fs = FileSystem.get(new URI("hdfs://cdh:9000"), conf, "root"); } // 通過流的方式進行檔案上傳 @SuppressWarnings("resource") @Test public void testUpload() throws Exception{ // true:代表是否覆蓋(重寫) FSDataOutputStream outputStream = fs.create(new Path("/luban"), true); // 要想將資料寫到輸出流,首先要在本地寫個輸入流 FileInputStream inputStream = new FileInputStream("D:\\後裔.txt"); // 用commons.io.utils比較方便,邊讀邊寫 IOUtils.copy(inputStream, outputStream); } // 通過流的方式指定下載部分檔案 @SuppressWarnings("resource") @Test public void testRandomAcess() throws Exception { FSDataInputStream inputStream = fs.open(new Path("/luban")); // 將指標撥到6個位元組處 從此開始讀 inputStream.seek(6); FileOutputStream outputStream = new FileOutputStream("D:\\後裔"); IOUtils.copy(inputStream, outputStream); } // 顯示hdfs上檔案的內容,相當於cat功能 @Test public void test() throws Exception { FSDataInputStream inputStream = fs.open(new Path("/luban")); IOUtils.copy(inputStream,System.out); } }