HDFS的Java API操作(筆記)

注意:需要獲取哪個打開main函數中的哪個

package com.hadoop.test;

import java.net.URI;
import org.apache.hadoop.conf.Configuration;
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;

public class HDFSAPITest {

/*
1 、獲取HDFS文件系統
*/
// 獲取文件系統
public static FileSystem getFileSystem() throws Exception {
// 讀取配置文件
Configuration conf = new Configuration();
// 返回默認文件系統,如果在Hadoop集羣下運行,使用此種方法可直接獲取默認文件系統
// FileSystem fs = FileSystem.get(conf);
// 指定的文件系統地址
URI uri = new URI("hdfs://master:9000");
// 返回指定的文件系統,如果在本地測試,需要使用此種方法獲取文件系統
FileSystem fs = FileSystem.get(uri, conf);
return fs;
}

/*
2 、文件/目錄的創建與刪除
*/
// 創建文件目錄
public static void mkdir() throws Exception {
// 獲取文件系統
FileSystem fs = getFileSystem();
// 創建文件目錄
fs.mkdirs(new Path("hdfs://master:9000/20191021/test"));
// 釋放資源
fs.close();
}

// 刪除文件或者文件目錄
public static void rmdir() throws Exception {
// 返回FileSystem對象
FileSystem fs = getFileSystem();
// 刪除文件或者文件目錄
fs.delete(new Path("hdfs://master:9000/20191021/test"),true);
// 釋放資源
fs.close();
}

/*
3、 獲取文件
*/
// 獲取目錄下的所有文件
public static void ListAllFile() throws Exception {
// 返回FileSystem對象
FileSystem fs = getFileSystem();
// 列出目錄內容
FileStatus[] status = fs.listStatus(new Path("hdfs://master:9000/"));
// 獲取目錄下的所有文件路徑
Path[] listedPaths = FileUtil.stat2Paths(status);
// 循環讀取每個文件
for(Path p : listedPaths) {
System.out.println(p);
}
// 釋放資源
fs.close();
}

/*
4 、上傳/下載文件
*/
// 文件上傳至HDFS
public static void copyToHDFS() throws Exception {
// 返回FileSystem對象
FileSystem fs = getFileSystem();
// 源文件路徑是Linux下的路徑,如果在Windows下測試,需要改寫爲Windows下的路徑,比如E://Hadoop/weibo.txt
//Path srcPath = new Path("/home/hadoop/weibo.txt");
Path srcPath = new Path("E://Hadoop/weibo.txt");
// 目的路徑
Path dstPath = new Path("hdfs://master:9000/20191021/test");
// 實現文件上傳
fs.copyFromLocalFile(srcPath, dstPath);
// 釋放資源
fs.close();
}

// 從HDFS下載文件
public static void getFile() throws Exception {
// 返回FileSystem對象
FileSystem fs = getFileSystem();
// 源文件路徑
Path srcPath = new Path("hdfs://master:9000/20191021/test/test.txt");
// 目的路徑是Linux下的路徑,如果在Windows下測試,需要改寫爲Windows下的路徑,比如E://hadoop/djt/
//Path dstPath = new Path("/home/hadoop/");
Path dstPath = new Path("E://hadoop/djt/");
// 下載hdfs上的文件
fs.copyToLocalFile(srcPath, dstPath);
// 釋放資源
fs.close();
}

/*
5、 獲取HDFS集羣節點信息
*/
// 獲取HDFS集羣節點信息
public static void getHDFSNodes() throws Exception {
// 返回FileSystem對象
FileSystem fs = getFileSystem();
// 獲取分佈式文件系統
DistributedFileSystem hdfs = (DistributedFileSystem)fs;
// 獲取所有節點
DatanodeInfo[] DataNodeStats = hdfs.getDataNodeStats();
// 循環打印所有節點
for(int i=0;i<DataNodeStats.length;i++) {
System.out.println("DataNode_"+i+"_Name:"+DataNodeStats[i].getHostName());
}
}
public static void main(String[] args) throws Exception {
//mkdir();
//rmdir();
//ListAllFile();
//copyToHDFS();
//getFile();
//getHDFSNodes();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章