HDFS的Java API的訪問方式實(shí)例代碼
本文研究的主要是HDFS的Java API的訪問方式,具體代碼如下所示,有詳細(xì)注釋。
最近的節(jié)奏有點(diǎn)兒快,等有空的時候把這個封裝一下
實(shí)現(xiàn)代碼
要導(dǎo)入的包:
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;
實(shí)體方法:
/** * 獲取HDFS文件系統(tǒng) * @return * @throws IOException * @throws URISyntaxException */ public static FileSystem getFileSystem() throws IOException, URISyntaxException{ //read config file Configuration conf = new Configuration(); //返回默認(rèn)文件系統(tǒng) //如果在Hadoop集群下運(yùn)行,使用此種方法可以直接獲取默認(rèn)文件系統(tǒng) //FileSystem fs = FileSystem.get(conf); //指定的文件系統(tǒng)地址 URI uri = new URI("hdfs://hy:9000"); //返回指定的文件系統(tǒng) //如果在本地測試,需要使用此種方法獲取文件系統(tǒng) FileSystem fs = FileSystem.get(uri, conf); return fs; } /** * 創(chuàng)建文件目錄 * @throws Exception */ public static void mkdir() throws Exception{ //獲取文件系統(tǒng) FileSystem fs = getFileSystem(); //創(chuàng)建文件目錄 fs.mkdirs(new Path("hdfs://hy:9000/hy/weibo")); //釋放資源 fs.close(); } /** * 刪除文件或者文件目錄 * @throws Exception */ public static void rmdir() throws Exception{ //獲取文件系統(tǒng) FileSystem fs = getFileSystem(); //刪除文件或者文件目錄 fs.delete(new Path("hdfs://hy:9000/hy/weibo"), true); //釋放資源 fs.close(); } /** * 獲取目錄下所有文件 * @throws Exception */ public static void listAllFile() throws Exception{ //獲取文件系統(tǒng) FileSystem fs = getFileSystem(); //列出目錄內(nèi)容 FileStatus[] status = fs.listStatus(new Path("hdfs://hy:9000/hy/")); //獲取目錄下所有文件路徑 Path[] listedPaths = FileUtil.stat2Paths(status); //循環(huán)讀取每個文件 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.jar Path srcPath = new Path("E://temp.jar"); //目的路徑 Path dstPath = new Path("hdfs://hy:9000/hy/weibo"); //實(shí)現(xiàn)文件上傳 fs.copyFromLocalFile(srcPath, dstPath); //釋放資源 fs.close(); } /** * 從HDFS上下載文件 * @throws Exception */ public static void getFile() throws Exception{ //獲得文件系統(tǒng) FileSystem fs = getFileSystem(); //源文件路徑 Path srcPath = new Path("hdfs://hy:9000/hy/weibo/temp.jar"); //目的路徑,默認(rèn)是Linux下的 //如果在Windows下測試,需要改為Windows下的路徑,如C://User/andy/Desktop/ Path dstPath = new Path("D://"); //下載HDFS上的文件 fs.copyToLocalFile(srcPath, dstPath); //釋放資源 fs.close(); } /** * 獲取HDFS集群點(diǎn)的信息 * @throws Exception */ public static void getHDFSNodes() throws Exception{ //獲取文件系統(tǒng) FileSystem fs = getFileSystem(); //獲取分布式文件系統(tǒng) DistributedFileSystem hdfs = (DistributedFileSystem)fs; //獲取所有節(jié)點(diǎn) DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); //循環(huán)比遍歷 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{ //獲取文件系統(tǒng) 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()); //循環(huán)輸出塊信息 for (int i = 0; i < blockLocations.length; i++) { String[] hosts = blockLocations[i].getHosts(); System.out.println("block_" + i + "_location:" + hosts[0]); } //釋放資源 fs.close(); }
總結(jié)
以上就是本文關(guān)于HDFS的Java API的訪問方式實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Java基于字符流形式讀寫數(shù)據(jù)的兩種實(shí)現(xiàn)方法示例
這篇文章主要介紹了Java基于字符流形式讀寫數(shù)據(jù)的兩種實(shí)現(xiàn)方法示,結(jié)合實(shí)例形式分析了java逐個字符讀寫及使用緩沖區(qū)進(jìn)行讀寫操作的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01APT?注解處理器實(shí)現(xiàn)?Lombok?常用注解功能詳解
這篇文章主要為大家介紹了使用APT?注解處理器實(shí)現(xiàn)?Lombok?常用注解功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Java通用BouncyCastle實(shí)現(xiàn)的DES3加密的方法
這篇文章主要介紹了Java通用BouncyCastle實(shí)現(xiàn)的DES3加密的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作,具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12MyBatis注解開發(fā)-@Insert和@InsertProvider的使用
這篇文章主要介紹了MyBatis注解開發(fā)-@Insert和@InsertProvider的使用,具有很好的參考價值,希望對大家有所幫助。2022-07-07ruoyi-springboot框架新增模塊調(diào)接口報404的解決方案
這篇文章主要介紹了ruoyi-springboot框架新增模塊調(diào)接口報404的解決方案,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03MyBatis-plus更新對象時將字段值更新為null的實(shí)現(xiàn)方式
mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為 空字符串 或者 null 的則不會執(zhí)行更新,如果要將指定字段更新null,可以通過以下三種方式實(shí)現(xiàn),感興趣的小伙伴跟著小編一起來看看吧2023-10-10