1. 程式人生 > >三十五、HDFS的FileSystem常用操作

三十五、HDFS的FileSystem常用操作

package com.yang.hdfs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

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 HDFSTest {
	
	private FileSystem fileSystem;
	private Configuration configuration;
	
	@Before
	public void initHDFS() throws IOException, InterruptedException, URISyntaxException {
		// 1 建立配置資訊物件
		configuration = new Configuration();
		//設定副本數
		//configuration.set("dfs.replication", "2");
		/*引數優先順序: 1、客戶端程式碼中設定的值  2、classpath下的使用者自定義配置檔案 3、然後是伺服器的預設配置*/
		
		// 2 獲取檔案系統
		fileSystem = FileSystem.get(new URI("hdfs://hadoop14:9000"),configuration, "admin");
		
		// 3 列印檔案系統
		System.out.println("==========================="+fileSystem.toString());
	}
	
	
	/**
	 * 上傳
	 * @throws Exception
	 */
	@Test
	public void HDFSUpLoad() throws Exception{
		
		
		//上傳檔案
		fileSystem.copyFromLocalFile(new Path("G:/yang.jpg")
				, new Path("/user/admin/mapreduce/wordcount/input/yang3.jpg"));
		
		//關閉資源
		fileSystem.close();
		System.out.println("over==================================");
	}
	
	
	/**
	 * 下載
	 * @throws Exception
	 */
	@Test
	public void getFileFromHDFS() throws Exception{
			
		
		// boolean delSrc 指是否將原檔案刪除
		// Path src 指要下載的檔案路徑
		// Path dst 指將檔案下載到的路徑
		// boolean useRawLocalFileSystem 是否開啟檔案效驗
	    // 2 下載檔案
		fileSystem.copyToLocalFile(false, new Path("hdfs://hadoop14:9000//user/admin/mapreduce/wordcount/input/yang3.jpg"), 
				                   new Path("G:/yang3.jpg"), true);
		fileSystem.close();
	}
	
	
	/**
	 * 建立目錄
	 * @throws Exception
	 */
	@Test
	public void mkdirAtHDFS() throws Exception{
		
		//建立目錄
		fileSystem.mkdirs(new Path("hdfs://hadoop14:9000/admin"));
	}
	
	
	/**
	 * 刪除資料夾
	 * @throws Exception
	 */
	@Test
	public void deleteAtHDFS() throws Exception{
		
		
		//刪除資料夾 ,如果是非空資料夾,引數2是否遞迴刪除,true遞迴
		fileSystem.delete(new Path("hdfs://hadoop14:9000/admin"), true);
	}
	
	/**
	 * 重新命名檔案或資料夾
	 * @throws Exception
	 */
	@Test
	public void renameAtHDFS() throws Exception{
		
		//重新命名檔案或資料夾
		fileSystem.rename(new Path("hdfs://hadoop14:9000/user/admin/mapreduce/wordcount/test/wc.input"), 
				          new Path("hdfs://hadoop14:9000/user/admin/mapreduce/wordcount/test/wc1.input"));
	}
	
	
	/**
	 * 檢視檔名稱、許可權、長度、塊資訊
	 * @throws Exception
	 */
	@Test
	public void readListFiles() throws Exception {
		
			
		// 思考:為什麼返回迭代器,而不是List之類的容器:減少記憶體損耗
		RemoteIterator<LocatedFileStatus> listFiles = fileSystem.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-offset:" + bl.getOffset());
					
				String[] hosts = bl.getHosts();
					
				for (String host : hosts) {
					System.out.println(host);
				}
			}
				
			System.out.println("--------------李冰冰的分割線--------------");
		}
	}
	
	
	/**
	 * 判斷是檔案還是資料夾
	 * @throws Exception
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	@Test
	public void findAtHDFS() throws Exception, IllegalArgumentException, IOException{
			
		//獲取查詢路徑下的檔案狀態資訊
		FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));

		//遍歷所有檔案狀態
		for (FileStatus status : listStatus) {
			if (status.isFile()) {
				System.out.println("f--" + status.getPath().getName());
			} else {
				System.out.println("d--" + status.getPath().getName());
			}
		}
	}
	
	
}