參考博客:https://www.cnblogs.com/Eddyer/p/6641778.html
1.需求
使用hdfs的javaAPI訪問hdfs系統。
2.環境配置
(1)hadoop為本地模式
(2)pom文件代碼如下

<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
3.使用hdfs的javaAPI操作hdfs的代碼
(1)初始化環境
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.Before; import org.junit.Test; import java.io.FileInputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class hadoopJavaApiDemo { FileSystem fs = null; @Before public void init() throws IOException, URISyntaxException, InterruptedException { Configuration configuration = new Configuration(); //設置文件系統為hdfs //獲取文件系統的客戶端實例對象 //注意:運行程序是要執行用戶名為hadoop,否則會出現沒寫權限的情況
fs = FileSystem.get(new URI("hdfs://127.0.0.1:9900"),configuration,"hadoop"); } //...
}
(2)上傳文件
@Test public void testUpload() throws IOException { fs.copyFromLocalFile(new Path("file://[本地文件的路徑,例如/a/b.txt]"),new Path("[hdfs文件系統的路徑,例如/]")); fs.close(); }
(3)下載文件
@Test public void testDownLoad() throws IOException { fs.copyToLocalFile(new Path("[hdfs上的文件路徑,例如/testData]"),new Path("[本地文件路徑,例如/home/a.txt]")); fs.close(); }
(4)創建文件夾
@Test //創建新的文件夾 public void testMakeDir() throws Exception { boolean mkdirs = fs.mkdirs(new Path("/x/y/z")); System.out.println(mkdirs); }
(5)刪除文件夾
@Test public void testDelete() throws Exception{ //第二個參數為true是遞歸刪除
boolean delete = fs.delete(new Path("/x"), true); System.out.println(delete); }
(6)修改文件夾名稱
@Test //測試修改文件夾的名稱
public void testRenameDir() throws IOException { boolean change = fs.rename(new Path("/x"),new Path("/xx")); System.out.println(change); }
(7)查看目錄信息
@Test //測試查看目錄信息功能
public void testListFiles() throws IOException {
//第二個參數為true表示遞歸調用 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),true); while (listFiles.hasNext()){
//打印各種文件信息,和命令行中執行 hdfs dfs -lsr / 效果一樣 LocatedFileStatus status = listFiles.next(); System.out.println(status.getPath().getName()); System.out.println(status.getBlockSize()); System.out.println(status.getPermission()); System.out.println(status.getLen());
//打印文件的每個分組位置,每128M一個分組 BlockLocation[] blockLocations = status.getBlockLocations(); for(BlockLocation bl:blockLocations){ System.out.println("block-length:"+bl.getLength()+"--"+
"block-offset:"+bl.getOffset()); String[] hosts = bl.getHosts(); for(String host:hosts){ System.out.println(host); } } System.out.println(); } }
(8)查看文件以及文件夾信息
@Test //查看文件以及文件夾信息
public void testListAll() throws IOException { FileStatus[] listStatus = fs.listStatus(new Path("/")); String flag = "d-----------------"; for(FileStatus fileStatus:listStatus){ if(fileStatus.isFile()) flag = "f----------------"; else flag = "d----------------"; System.out.println(flag+fileStatus.getPath().getName()); } }
4.注意事項
(1)hdfs各種端口的含義(就因為將127.0.0.1:9900寫為127.0.0.1:50070,debug了好久。。)
9900是fileSystem的端口號(默認是9000,這里我自定義為9900)
50070是namenode主節點的端口號
50090是namenode的secondarynamenode的端口號
(2)file://是一個傳輸協議
比如可以通過在瀏覽器中輸入路徑file:///a/b/c.txt訪問本地文件
5.github鏈接
https://github.com/gulu2016/STBigData/hadoopJavaApiDemo.java