HDFS的Java API的訪問方式執行個體代碼,hdfsapi
本文研究的主要是HDFS的Java API的訪問方式,具體代碼如下所示,有詳細注釋。
最近的節奏有點兒快,等有空的時候把這個封裝一下
實現代碼要匯入的包:
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.FileUtil;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hdfs.DistributedFileSystem;import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
實體方法:
/** * 擷取HDFS檔案系統 * @return * @throws IOException * @throws URISyntaxException */public static FileSystem getFileSystem() throws IOException, URISyntaxException{//read config fileConfiguration conf = new Configuration();//返回預設檔案系統//如果在Hadoop叢集下運行,使用此種方法可以直接擷取預設檔案系統//FileSystem fs = FileSystem.get(conf);//指定的檔案系統地址URI uri = new URI("hdfs://hy:9000");//返回指定的檔案系統//如果在本地測試,需要使用此種方法擷取檔案系統FileSystem fs = FileSystem.get(uri, conf);return fs;}/** * 建立檔案目錄 * @throws Exception */public static void mkdir() throws Exception{//擷取檔案系統FileSystem fs = getFileSystem();//建立檔案目錄fs.mkdirs(new Path("hdfs://hy:9000/hy/weibo"));//釋放資源fs.close();}/** * 刪除檔案或者檔案目錄 * @throws Exception */public static void rmdir() throws Exception{//擷取檔案系統FileSystem fs = getFileSystem();//刪除檔案或者檔案目錄fs.delete(new Path("hdfs://hy:9000/hy/weibo"), true);//釋放資源fs.close();}/** * 擷取目錄下所有檔案 * @throws Exception */public static void listAllFile() throws Exception{//擷取檔案系統FileSystem fs = getFileSystem();//列出目錄內容FileStatus[] status = fs.listStatus(new Path("hdfs://hy:9000/hy/"));//擷取目錄下所有檔案路徑Path[] listedPaths = FileUtil.stat2Paths(status);//迴圈讀取每個檔案for (Path path : listedPaths) {System.out.println(path);}//釋放資源fs.close();}/** * 將檔案上傳至HDFS * @throws Exception */public static void copyToHDFS() throws Exception{//擷取檔案對象FileSystem fs = getFileSystem();//源檔案路徑是Linux下的路徑 Path srcPath = new Path("/home/hadoop/temp.jar");//如果需要在windows下測試,需要改為Windows下的路徑,比如 E://temp.jarPath srcPath = new Path("E://temp.jar");//目的路徑Path dstPath = new Path("hdfs://hy:9000/hy/weibo");//實現檔案上傳fs.copyFromLocalFile(srcPath, dstPath);//釋放資源fs.close();}/** * 從HDFS上下載檔案 * @throws Exception */public static void getFile() throws Exception{//獲得檔案系統FileSystem fs = getFileSystem();//源檔案路徑Path srcPath = new Path("hdfs://hy:9000/hy/weibo/temp.jar");//目的路徑,預設是Linux下的//如果在Windows下測試,需要改為Windows下的路徑,如C://User/andy/Desktop/Path dstPath = new Path("D://");//下載HDFS上的檔案fs.copyToLocalFile(srcPath, dstPath);//釋放資源fs.close();}/** * 擷取HDFS叢集點的資訊 * @throws Exception */public static void getHDFSNodes() throws Exception{//擷取檔案系統FileSystem fs = getFileSystem();//擷取Distributed File SystemDistributedFileSystem hdfs = (DistributedFileSystem)fs;//擷取所有節點DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();//迴圈比遍曆for (int i = 0; i < dataNodeStats.length; i++) {System.out.println("DataNote_" + i + "_Name:" + dataNodeStats[i].getHostName());}//釋放資源fs.close();}/** * 尋找某個檔案在HDFS叢集的位置 * @throws Exception */public static void getFileLocal() throws Exception{//擷取檔案系統FileSystem fs = getFileSystem();//檔案路徑Path path = new Path("hdfs://hy:9000/hy/weibo/temp.jar");//擷取檔案目錄FileStatus fileStatus = fs.getFileStatus(path);//擷取檔案塊位置列表BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());//迴圈輸出塊資訊for (int i = 0; i < blockLocations.length; i++) {String[] hosts = blockLocations[i].getHosts();System.out.println("block_" + i + "_location:" + hosts[0]);}//釋放資源fs.close();}總結
以上就是本文關於HDFS的Java API的訪問方式執行個體代碼的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!