最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

HDFS的Java API的訪問方式實(shí)例代碼

 更新時(shí)間:2018年02月03日 09:37:52   作者:墨梅寒香  
這篇文章主要介紹了HDFS的Java API的訪問方式實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是HDFS的Java API的訪問方式,具體代碼如下所示,有詳細(xì)注釋。

最近的節(jié)奏有點(diǎn)兒快,等有空的時(shí)候把這個(gè)封裝一下

實(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)
	//如果在本地測(cè)試,需要使用此種方法獲取文件系統(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)讀取每個(gè)文件
	for (Path path : listedPaths) {
		System.out.println(path);
	}
	//釋放資源
	fs.close();
}
/**
   * 將文件上傳至HDFS
   * @throws Exception
   */
public static void copyToHDFS() throws Exception{
	//獲取文件對(duì)象
	FileSystem fs = getFileSystem();
	//源文件路徑是Linux下的路徑 Path srcPath = new Path("/home/hadoop/temp.jar");
	//如果需要在windows下測(cè)試,需要改為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下測(cè)試,需要改為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();
}
/**
   * 查找某個(gè)文件在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í)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • SpringCloud組件OpenFeign之默認(rèn)HTTP請(qǐng)求方式詳解

    SpringCloud組件OpenFeign之默認(rèn)HTTP請(qǐng)求方式詳解

    這篇文章主要介紹了SpringCloud組件OpenFeign之默認(rèn)HTTP請(qǐng)求方式詳解,在SpringMvcContract類中有個(gè)這樣的方法processAnnotationOnMethod,見名思意,這個(gè)方法就是處理Feign接口下方法上的注解的,需要的朋友可以參考下
    2024-01-01
  • Java線程池源碼的深度解析

    Java線程池源碼的深度解析

    線程池的好處和使用本篇文章就不贅敘了,這篇文章主要通過線程池的源碼帶大家深入了解一下jdk8中線程池的實(shí)現(xiàn),感興趣的小伙伴可以了解一下
    2022-10-10
  • Spring AOP基本概念

    Spring AOP基本概念

    這篇文章主要為大家詳細(xì)介紹了spring基礎(chǔ)概念A(yù)OP與動(dòng)態(tài)代理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-07-07
  • JetBrains IntelliJ IDEA 2020安裝與使用教程詳解

    JetBrains IntelliJ IDEA 2020安裝與使用教程詳解

    這篇文章主要介紹了JetBrains IntelliJ IDEA 2020安裝與使用教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 深入解析java中的值傳遞和引用傳遞

    深入解析java中的值傳遞和引用傳遞

    這篇文章主要介紹了深入解析java中的值傳遞和引用傳遞,值傳遞是將變量的值復(fù)制給另一個(gè)變量,兩個(gè)變量之間并沒有直接關(guān)系,引用傳遞是將變量的引用(內(nèi)存地址)傳遞給另一個(gè)變量,兩個(gè)變量之間指向同一個(gè)內(nèi)存地址,修改一個(gè)變量的值也會(huì)影響到另一個(gè)變量
    2023-07-07
  • Mybatis-plus對(duì)單表操作的封裝實(shí)現(xiàn)

    Mybatis-plus對(duì)單表操作的封裝實(shí)現(xiàn)

    本文詳細(xì)介紹了MyBatis-Plus單表操作,包括自定義SQL、邏輯刪除、樂觀鎖、全局?jǐn)r截器和代碼生成器等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-12-12
  • Ubuntu安裝JDK與IntelliJ?IDEA的詳細(xì)過程

    Ubuntu安裝JDK與IntelliJ?IDEA的詳細(xì)過程

    APT是Linux系統(tǒng)上的包管理工具,能自動(dòng)解決軟件包依賴關(guān)系并從遠(yuǎn)程存儲(chǔ)庫中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過程,需要的朋友可以參考下
    2023-08-08
  • feign遠(yuǎn)程調(diào)用無法傳遞對(duì)象屬性405的問題

    feign遠(yuǎn)程調(diào)用無法傳遞對(duì)象屬性405的問題

    這篇文章主要介紹了feign遠(yuǎn)程調(diào)用無法傳遞對(duì)象屬性405的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring中bean標(biāo)簽的用法詳解

    Spring中bean標(biāo)簽的用法詳解

    Bean標(biāo)簽一般用于配置對(duì)象交由Spring?來創(chuàng)建,這篇文章主要來和大家詳細(xì)聊聊Spring中bean標(biāo)簽的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • 詳解OpenFeign服務(wù)調(diào)用(微服務(wù))

    詳解OpenFeign服務(wù)調(diào)用(微服務(wù))

    OpenFeign是Spring Cloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等,這篇文章主要介紹了OpenFeign服務(wù)調(diào)用的相關(guān)知識(shí),需要的朋友可以參考下
    2022-07-07

最新評(píng)論

翼城县| 石家庄市| 寻甸| 温宿县| 高陵县| 肇东市| 万源市| 金门县| 永安市| 洪泽县| 梁山县| 郎溪县| 庆云县| 剑阁县| 德钦县| 马龙县| 秦安县| 周宁县| 汉阴县| 通道| 张北县| 绥阳县| 凤冈县| 信宜市| 龙泉市| 河北省| 锦屏县| 哈尔滨市| 闸北区| 炉霍县| 南木林县| 景宁| 土默特右旗| 峨眉山市| 寿阳县| 凤冈县| 通山县| 凤凰县| 翁源县| 霸州市| 小金县|