【HDFS API編程】第一個應用程序的開發-建立文件夾

/**
 * 使用Java API操做HDFS文件系統
 * 關鍵點:
 * 1)建立 Configuration
 * 2)獲取 FileSystem
 * 3)...剩下的就是 HDFS API的操做了
*/api

先上代碼瀏覽器

 1 public class HDFSApp {
 2     public static void main(String[] args) throws Exception {
 3         Configuration configuration = new Configuration();
 4 
 5         FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration,"hadoop");
 6 
 7         Path path = new Path("/hdfsapi/test");
 8         boolean result = fileSystem.mkdirs(path);
 9         System.out.println(result);
10     }
11 }

對於方法的源碼是咱們在學習的時候必須的,咱們能夠按住Ctrl而後點擊對應的方法類名進去進行源碼的下載。app

首先要對HDFS進行操做咱們就必須獲取FileSystem,分佈式

Ctrl點進FileSystem源碼,咱們能看到最頂部的註釋介紹(附上了中文翻譯):ide

 

 1 /****************************************************************
 2  * An abstract base class for a fairly generic filesystem.  It
 3  * may be implemented as a distributed filesystem, or as a "local"
 4  * one that reflects the locally-connected disk.  The local version
 5  * exists for small Hadoop instances and for testing.
 6  *
 7  * <p>
 8  *
 9  * All user code that may potentially use the Hadoop Distributed
10  * File System should be written to use a FileSystem object.  The
11  * Hadoop DFS is a multi-machine system that appears as a single
12  * disk.  It's useful because of its fault tolerance and potentially
13  * very large capacity.
14  * 
15  * <p>
16  * The local implementation is {@link LocalFileSystem} and distributed
17  * implementation is DistributedFileSystem.
18  *****************************************************************/
19 /**********************************************
20 
21 *一個至關通用的文件系統的抽象基類。它能夠實現爲分佈式文件系統,也能夠實現爲「本地」
22 *反映本地鏈接磁盤的磁盤。本地版本
23 *存在於小型Hadoop實例和測試中。
24 *全部可能使用Hadoop分佈式的用戶代碼
25 *應該寫入文件系統以使用文件系統對象。這個Hadoop DFS是一個多機系統,顯示爲單個磁盤。它是有用的,由於它的容錯性和潛在的容量很大。
26 *< P>
27 *本地實現是@link localfilesystem和分佈式的
28 *實現是分佈式文件系統。
29 ***********************************************/

 

 

 

寫做的開門見山,第一句話可以歸納全文中心---> 這是 一個至關通用的文件系統的抽象基類。,因此咱們使用Java API操做HDFS文件系統須要獲取FileSystem。oop

使用FileSystem的get方法獲取fileSystem,FileSystem有三種get方法:學習

不知道怎麼用?哪裏不會Ctrl點哪裏!測試

首先看只有一個參數的get方法:get(Configuration conf): spa

 1 /**
 2    * Returns the configured filesystem implementation.
 3    * @param conf the configuration to use
 4    */
 5 /**
 6 *返回配置的文件系統實現。
 7 *@參數conf 要使用的配置
 8 */
 9   public static FileSystem get(Configuration conf) throws IOException {
10     return get(getDefaultUri(conf), conf);
11   }

從源碼的介紹中能夠知道 Configuration 是某種配置,具體是什麼?Ctrl點一點:翻譯

Provides access to configuration parameters.//提供對配置參數的訪問。

源碼中configuration的註釋介紹第一句已經介紹了這是對配置參數的訪問,因此,使用Java API操做HDFS文件系統須要建立configuration:Configuration configuration = new Configuration();

跳過兩個參數的get方法,咱們來看包含三個參數的get方法,Ctrl 點進去:

 

 1 /**
 2 * Get a filesystem instance based on the uri, the passed
 3 * configuration and the user
 4 * @param uri of the filesystem
 5 * @param conf the configuration to use
 6 * @param user to perform the get as
 7 * @return the filesystem instance
 8 * @throws IOException
 9 * @throws InterruptedException
10 */
11 /**
12 *獲取基於URI的文件系統實例,配置和用戶
13 *@參數uri 文件系統的uri
14 *@參數conf 要使用的配置
15 *@參數user 要執行get as的用戶
16 *@返回文件系統實例
17 *@引起IOException
18 *@引起InterruptedException
19 */ 
20   public static FileSystem get(final URI uri, final Configuration conf,
21         final String user) throws IOException, InterruptedException {
22     String ticketCachePath =
23       conf.get(CommonConfigurationKeys.KERBEROS_TICKET_CACHE_PATH);
24     UserGroupInformation ugi =
25         UserGroupInformation.getBestUGI(ticketCachePath, user);
26     return ugi.doAs(new PrivilegedExceptionAction<FileSystem>() {
27       @Override
28       public FileSystem run() throws IOException {
29         return get(uri, conf);
30       }
31     });
32   }

 

重點看方法上面的註釋,介紹了方法的功能、參數解釋、返回值以及可能引起的異常。

咱們要對HDFS操做,是否是須要知道咱們要對哪一個HDFS操做(URI),是否是要指定用什麼樣的配置參數進行操做(configuration),是否是須要確認是用什麼用戶進行操做(user),這樣咱們纔可以準確地使用正確的用戶以及正確的配置訪問正確的HDFS。

此時咱們拿到的fileSystem已是咱們須要的fileSystem了,那麼剩下的操做就是對HDFS API的操做了,例如咱們建立一個文件夾:

使用fileSystem的mkdirs方法,mkdirs方法不知道怎麼用?Ctrl點!

 

 

1   /**
2    * Call {@link #mkdirs(Path, FsPermission)} with default permission.
3    */
4   public boolean mkdirs(Path f) throws IOException {
5     return mkdirs(f, FsPermission.getDirDefault());
6   }

 

咱們能夠看到mkdir方法接收的參數是一個Path路徑,返回值是boolean類型判斷是否建立成功,因此咱們能夠寫:

Path path = new Path("/hdfsapi/test");//建立一個Path
boolean result = fileSystem.mkdirs(path); System.out.println(result);//輸出result查看是否建立成功
此時咱們能夠經過終端控制檯進行查看文件夾是否建立成功:



固然也能夠經過瀏覽器查看是否建立成功:輸入 地址IP:50070 回車, 例如 192.168.42.110:50070 回車,
進去點擊Utilities下拉列表的Browse the fiel system,點Go!就能看到:

點進去hdfsapi:
操做成功。
相關文章
相關標籤/搜索