import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FileStatus; 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; public class HdfsClient { FileSystem fs = null; /** * 初始化FileSystem */ @Before public void init() throws Exception { // 構造一個配置參數對象,設置一個參數:我們要訪問的hdfs的URI // 從而FileSystem.get()方法就知道應該是去構造一個訪問hdfs文件系統的客戶端,以及hdfs的訪問地址 // new Configuration();的時候,它就會去加載jar包中的hdfs-default.xml // 然后再加載classpath下的hdfs-site.xml Configuration conf = new Configuration(); //conf.set("fs.defaultFS", "hdfs://hdp-node01:9000"); /** * 參數優先級: 1、客戶端代碼中設置的值 2、classpath下的用戶自定義配置文件 3、然后是服務器的默認配置 */ //conf.set("dfs.replication", "3"); // 獲取一個hdfs的訪問客戶端,根據參數,這個實例應該是DistributedFileSystem的實例 // fs = FileSystem.get(conf); // 如果這樣去獲取,那conf里面就可以不要配"fs.defaultFS"參數,而且,這個客戶端的身份標識已經是hadoop用戶 fs = FileSystem.get(new URI("hdfs://hdp-node01:9000"), conf, "hadoop"); } /** * 往hdfs上傳文件 */ @Test public void testAddFileToHdfs() throws Exception { // 要上傳的文件所在的本地路徑 Path src = new Path("c:/liuliang.jar"); // 要上傳到hdfs的目標路徑 Path dst = new Path("/"); fs.copyFromLocalFile(src, dst); fs.close(); } /** * 從hdfs中復制文件到本地文件系統 */ @Test public void testDownloadFileToLocal() throws IllegalArgumentException, IOException { fs.copyToLocalFile(new Path("/jdk-7u65-linux-i586.tar.gz"), new Path("d:/")); fs.close(); } /** * 在hfds中創建目錄、刪除目錄、重命名 */ @Test public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException { // 創建目錄 fs.mkdirs(new Path("/a1/b1/c1")); // 刪除文件夾 ,如果是非空文件夾,參數2必須給值true fs.delete(new Path("/aaa"), true); // 重命名文件或文件夾 fs.rename(new Path("/a1"), new Path("/a2")); } /** * 查看目錄信息,只顯示文件 */ @Test public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException { // 思考:為什么返回迭代器,而不是List之類的容器 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println(fileStatus.getPath().getName()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPermission()); System.out.println(fileStatus.getLen()); BlockLocation[] blockLocations = fileStatus.getBlockLocations(); for (BlockLocation bl : blockLocations) { System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset()); String[] hosts = bl.getHosts(); for (String host : hosts) { System.out.println(host); } } System.out.println("--------------分割線--------------"); } } /** * 查看文件及文件夾信息 */ @Test public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = "d-- "; for (FileStatus fstatus : listStatus) { if (fstatus.isFile()) flag = "f-- "; System.out.println(flag + fstatus.getPath().getName()); } } }