溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HDFS中Java API的訪問方式有哪些

發布時間:2021-07-21 14:11:05 來源:億速云 閱讀:210 作者:小新 欄目:編程語言

這篇文章主要介紹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 file
	Configuration 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.jar
	Path 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();
	//獲取分布式文件系統
	DistributedFileSystem 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的訪問方式有哪些”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女