JAVA操作HDFS的文件系統
前言:上篇文章介紹瞭如何利用Shell去操作HDFS中的文件,本文介紹使用Java代碼去操作HDFS中的文件,它的操作內容和shell的操作內容和方法基本一致,開發集成工具選擇IDEA。
一、新建Maven項目
填寫項目名稱和路徑,完成即可
二、添加依賴
打開maven配置文件pom.xml,添加如下依賴,這個下載過程根據網絡情況,可能要很久…
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
maven自動下載完成後,會看到如下三個依賴
三、開啓HDFS集羣
目前hdfs中只有一個aa.txt文件
注意:在瀏覽器中查看HDFS,需要在開啓hdfs集羣后(start-dfs.sh),關閉防火牆(systemctl stop firewalld)
四、編寫Java程序操作HDFS中的文件
1、在java的package下新建package目錄,然後新建TestHDFS類
操作1、獲取HDFS中的所有文件的路徑
1、實現代碼
public class TestHDFS {
//獲取hdfs中的所有文件路徑
@Test
public void GetHDFSPath() throws IOException {
//配置類(Configuration),用於配置hadoop中的core-sit.xml和hdfs-sit.xml
Configuration configuration=new Configuration();
configuration.set("fs.defaultFS","192.168.23.131:9000");//name和value兩個參數,就是我們在hadoop的core-site.xml中配置的參數(指定哪臺機器是namenode)
FileSystem fileSystem=FileSystem.get(configuration);
Path path=new Path("/");
FileStatus[] fileStatuses = fileSystem.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
System.out.println(fileStatus.getPath());
}
}
}
運行GetHDFSPath方法,獲得文件路徑
2、對上面的方法進行優化,得到以下代碼
public class TestHDFS {
private FileSystem fileSystem;
@Before
public void befor() throws IOException {
//配置類(Configuration),用於配置hadoop中的core-sit.xml和hdfs-sit.xml
Configuration configuration=new Configuration();
configuration.set("fs.defaultFS","192.168.23.131:9000");//name和value兩個參數,就是我們在hadoop的core-site.xml中配置的參數(指定哪臺機器是namenode)
fileSystem=FileSystem.get(configuration);
}
//獲取hdfs中的所有文件路徑
@Test
public void GetHDFSPath() throws IOException {
Path path=new Path("/");
FileStatus[] fileStatuses = fileSystem.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
System.out.println(fileStatus.getPath());
}
}
@After
public void after() throws IOException {
fileSystem.close();
}
}
操作2、向HDFS中上傳文件
1、實現方法代碼
@Test
public void UploadFileToHDFS() throws IOException {
//上傳到hdfs的文件流
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/1.txt"));
//從本地文件讀取的文件流
FileInputStream fileInputStream=new FileInputStream(new File("F:\\MyLog.Log"));
//將本地文件流拷貝到待上傳的hdfs文件流,完成上傳
IOUtils.copyBytes(fileInputStream,fsDataOutputStream,1024,true);
}
注意:上傳文件需要root用戶的權限,如果再hdfs中沒有配置關閉權限,需要在java代碼中,修改當前用戶爲root
//System.setProperty("HADOOP_USER_NAME","root");如果未在hadoop配置關閉root權限,則需要執行此代碼,才能實現java向hdfs上傳下載文件的功能
2、補充:如何查找hadoop的配置屬性
因爲不關閉root權限,會有很多操作受限,建議再hadoop的hdfs-site.xml的配置文件中修改該權限屬性。那麼這個配置是怎麼修改的呢?
在hadoop的官方文檔中(https://hadoop.apache.org/docs/r2.9.2/)介紹了core-site.xml以及hdfs-site.xml的默認配置,我們可以查看這些默認屬性配置的說明,進而去修改我們需要的屬性配置
修改root權限是在hdfs-site.xml中,我們可以進入hdfs-default.xml中查找權限的相關屬性(右鍵瀏覽器頁面->查看源文件)
我們發現修改root權限的默認屬性配置,然後我們把它拷貝,將ture修改爲false配置到hadoop的hdfs-site.xml
操作3、從HDFS中下載文件
1、實現代碼
//從HDFS中下載文件
@Test
public void DownloadFileToLoacal() throws IOException {
Path src=new Path("/1.txt");//待下載的hdfs中的文件路徑
Path dst=new Path("E:\\");//下載到本地的目錄路徑
fileSystem.copyToLocalFile(false,src,dst,true);//最後一個參數要設置爲true,這個是解決跨平臺問題的
}