Hadoop web編程--REST API WebHDFS

問題導讀

1.外部的應用程序需要操作HDFS的情況,該如何解決?
2.WebHDFS REST API可以完成什麼事情?
3.Httpfs 配置啓動,core-site.xml、hdfs-site.xml文件改如何配置?
4.REST API 如何啓動httpfs?
5.通過什麼命令可以查看hdfs文件的狀態?
6.WebHDFS常用操作都包含什麼?該如何實現?




1 介紹    
Hadoop提供了一個Java native API來支持對文件系統進行創建,重命名,刪除文件或者目錄,打開讀取或者寫文件,設置文件權限等操作。這對於運行在hadoop集羣中的應用程序來說是挺棒的,但是,也有許多外部的應用程序需要操作HDFS的情況,怎麼辦?如果解決這種問題呢?Hortonworks 開發了一些額外的API來支持這些基於標準REST功能的需求。

2 WebHDFS REST API    

WebHDFS觀念是基於HTTP操作,比如GET、PUT、POST和DELETE。像OPEN、GETFILESTATUS、LISTSTATUS的操作是使用HTTP GET,其它的像CREAT、MKDIRS、RENAME、SETPERMISSION是依賴於HTTP PUT類型。APPEND操作時基於HTTP POST類型,然而,DELETE是使用HTTP DELETE。
    認證方式可以使用基於user.name參數或者如果安全機制開啓了就依賴於Kerberos。標準的URL格式如下所示:
http://host:port/webhdfs/v1/?op=operation&user.name=username
默認的啓動對口是14000,你可以在httpfs-env.sh 中配置端口的值。所有與httpfs有關的環境參數變量,你可以再httpfs-env.sh中進行個性化的配置。

3 Httpfs 配置啓動   

編輯core-site.xml文件,添加如下內容:

  1.   <property>
  2.     <name>hadoop.proxyuser.#HTTPFSUSER#.hosts</name>
  3.     <value>*</value>
  4.   </property>
  5.   <property>
  6.     <name>hadoop.proxyuser.#HTTPFSUSER#.groups</name>
  7.     <value>*</value>
  8.   </property>
  9. ….
複製代碼




值得注意的是,#HTTPFSUSER#指的是用戶名,即linux啓動httpfs的用戶。


編輯hdfs-site.xml文件,添加下列屬性配置。
  1. <property>
  2.           <name>dfs.webhdfs.enabled</name>
  3.            <value>true</value>
  4. </property>
  5. <property>
  6. <name>dfs.permissions</name>
  7. <value>false</value>
  8. </property>
複製代碼



    另外,想知道更多的有關HDFS端口信息,請參考Cloudera blog.
    啓動httpfs,執行如下命令:

  1. httpfs.sh start  
複製代碼

4 驗證
一切配置好了之後,測試一下是否配置成功是非常有必要的。有這樣一種情況,我們需要知道hdfs 目錄下tmp的文件狀態。按照這樣的需求,我們可以編寫這樣的命令:
命令行下

  1. curl –i http://hadoop-master:14000/webhdfs/v1/tmp?user.name=app&op=GETFILESTATUS  
  2. HTTP/1.1 200 OK  
  3. Server:Apache-Coyote/1.1  
  4. Set-Cookie:hadoop.auth="u=app&p=app&t=simple&e=1393293979907&s=9YBXB7GM3oiIOYgflAjlq3GIpxc=";Version=1; Path=/  
  5. Content-Type:application/json  
  6. Transfer-Encoding:chunked  
  7. Date: Mon, 24Feb 2014 16:06:20 GMT  
  8.    
  9. {"FileStatus":{"pathSuffix":"","type":"DIRECTORY","length":0,"owner":"app","group":"supergroup","permission":"720","accessTime":0,"modificationTime":1391352186043,"blockSize":0,"replication":0}}  
複製代碼

瀏覽器中:

  1. http://hadoop-master:14000/webhdfs/v1/tmp?user.name=app&op=GETFILESTATUS  
複製代碼
返回信息:
  1. {"FileStatus":{"pathSuffix":"","type":"DIRECTORY","length":0,"owner":"app","group":"supergroup","permission":"720","accessTime":0,"modificationTime":1391352186043,"blockSize":0,"replication":0}}  
複製代碼




5介紹幾種常用的WebHDFS操作


5.1 創建
     創建一個目錄/tmp/webhdfs。


  1. curl-i -X PUT"http://hadoop-master:14000/webhdfs/v1/tmp/webhdfs?user.name=app&op=MKDIRS"  
  2. HTTP/1.1 200 OK  
  3. Server: Apache-Coyote/1.1  
  4. Set-Cookie:hadoop.auth="u=app&p=app&t=simple&e=1393356198808&s=74NhnIdH7WceKqgTW7UJ1ia9h10=";Version=1; Path=/  
  5. Content-Type: application/json  
  6. Transfer-Encoding: chunked  
  7. Date: Tue, 25 Feb 2014 09:23:23 GMT
複製代碼

達到相似的功能,Hadoop 命令:
  1. hdfs dfs -mkdir /tmp/webhdfs
複製代碼


創建一個文件

創建一個文件需要兩個步驟:第一步是在namenode運行命令,第二步根據第一步提供的location參數執行PUT操作。

  1. curl -i -X PUT"http://hadoop-master:50070/webhdfs/v1/tmp/webhdfs/webhdfs-test.txt?user.name=app&op=CREATE"  
  2. HTTP/1.1 307 TEMPORARY_REDIRECT  
  3. Cache-Control: no-cache  
  4. Expires: Thu, 01-Jan-1970 00:00:00 GMT  
  5. Date: Wed, 26 Feb 2014 14:35:29 GMT  
  6. Pragma: no-cache  
  7. Date: Wed, 26 Feb 2014 14:35:29 GMT  
  8. Pragma: no-cache  
  9. Set-Cookie:hadoop.auth="u=app&p=app&t=simple&e=1393461329238&s=as2cO3rRtvj8psr0jAhMk6YBHRY=";Path=/  
  10. Location:http://machine-2:50075/webhdfs/v1/tmp/webhdfs/webhdfs-test.txt?op=CREATE&user.name=app&namenoderpcaddress=hadoop-master:9000&overwrite=false  
  11. Content-Type: application/octet-stream  
  12. Content-Length: 0  
  13. Server: Jetty(6.1.26
複製代碼

發送數據到指定文件中:
  1. curl -i -X PUT -T webhdfs-test.txt"http://machine-2:50075/webhdfs/v1/tmp/webhdfs/webhdfs-test.txt?op=CREATE&user.name=app&namenoderpcaddress=hadoop-master:9000&overwrite=false"  
  2. HTTP/1.1 100 Continue  
  3.    
  4. HTTP/1.1 201 Created  
  5. Cache-Control: no-cache  
  6. Expires: Wed, 26 Feb 2014 14:37:51 GMT  
  7. Date: Wed, 26 Feb 2014 14:37:51 GMT  
  8. Pragma: no-cache  
  9. Expires: Wed, 26 Feb 2014 14:37:51 GMT  
  10. Date: Wed, 26 Feb 2014 14:37:51 GMT  
  11. Pragma: no-cache  
  12. Location:webhdfs://0.0.0.0:50070/tmp/webhdfs/webhdfs-test.txt  
  13. Content-Type: application/octet-stream  
  14. Content-Length: 0  
  15. Server: Jetty(6.1.26)
複製代碼



5.2 讀取文件
    讀取/input文件信息,使用-L參數。
  1. [app@hadoop-master ~]$ curl -i -L"http://hadoop-master:14000/webhdfs/v1/input?op=OPEN&user.name=app"  
  2. HTTP/1.1 200 OK  
  3. Server: Apache-Coyote/1.1  
  4. Set-Cookie:hadoop.auth="u=app&p=app&t=simple&e=1393420279602&s=dSnnx9oOwgGwVV/Q/ZmFyRjbtFU=";Version=1; Path=/  
  5. Content-Type: application/octet-stream  
  6. Content-Length: 1358  
  7. Date: Wed, 26 Feb 2014 03:11:19 GMT  
  8. Apache HBase [1] is an open-source,distributed, versioned, column-oriented  
  9. store modeled after Google' Bigtable: ADistributed Storage System for  
  10. Structured Data by Chang et al.[2]  Just as Bigtable leverages the distributed  
  11. data storage provided by the Google FileSystem, HBase provides Bigtable-like  
  12. capabilities on top of Apache Hadoop[3].  
  13.    
  14. To get started using HBase, the fulldocumentation for this release can be  
  15. found under the doc/ directory thataccompanies this README.  Using abrowser,  
  16. open the docs/index.html to view theproject home page (or browse to [1]).  
  17. The hbase 'book' at docs/book.html has a'quick start' section and is where you  
  18. should being your exploration of thehbase project.  
  19.    
  20. The latest HBase can be downloaded froman Apache Mirror [4].  
  21.    
  22. The source code can be found at [5]  
  23.    
  24. The HBase issue tracker is at [6]  
  25.    
  26. Apache HBase is made available under theApache License, version 2.0 [7]  
  27.    
  28. The HBase mailing lists and archives arelisted here [8].  
  29.    
  30. 1. http://hbase.apache.org  
  31. 2.http://labs.google.com/papers/bigtable.html  
  32. 3. http://hadoop.apache.org  
  33. 4.http://www.apache.org/dyn/closer.cgi/hbase/  
  34. 5.http://hbase.apache.org/docs/current/source-repository.html  
  35. 6.http://hbase.apache.org/docs/current/issue-tracking.html  
  36. 7.http://hbase.apache.org/docs/current/license.html  
  37. 8. http://hbase.apache.org/docs/current/mail-lists.html
複製代碼



5.3 重命名目錄
   需要修改op的值和添加destination參數,實例如下所示:
  1. [app@hadoop-master~]$ curl -i -X PUT  
  2. "http://hadoop-master:14000/webhdfs/v1/tmp/webhdfs?op=RENAME&user.name=app&destination=/tmp/webhdfs-new"  
  3. [app@hadoop-master~]$ curl -i -X PUT  
  4. "http://hadoop-master:14000/webhdfs/v1/tmp/webhdfs?op=RENAME&user.name=app&destination=/tmp/webhdfs-new"  
  5. HTTP/1.1 200 OK  
  6. Server:Apache-Coyote/1.1  
  7. Set-Cookie: hadoop.auth="u=app&p=app&t=simple&e=1393420506278&s=Oz9HEzxuYvP8kfAY4SWH6h+Gb50=";Version=1; Path=/  
  8. Content-Type:application/json  
  9. Transfer-Encoding:chunked  
  10. Date: Wed, 26 Feb2014 03:15:06 GMT  
  11.    
  12. {"boolean":true}
複製代碼

驗證是否正確地執行,輸入下列命令:
  1. [app@hadoop-master~]$ hdfs dfs -ls /tmp  
  2. Found 5 items  
  3. drwx------   - app supergroup          0 2014-01-08 08:02 /tmp/hadoop-yarn  
  4. drwxr-xr-x   - app supergroup          0 2014-02-02 09:43 /tmp/hdfs_out  
  5. drwxr-xr-x   - app supergroup          0 2014-02-20 22:39 /tmp/hive-app  
  6. drwxr-xr-x   - app supergroup          0 2014-02-25 04:25 /tmp/jps  
  7. drwxr-xr-x   - app supergroup          0 2014-02-25 04:23 /tmp/webhdfs-new
複製代碼


5.4 刪除目錄
非空的目錄刪除的話會拋出異常,只有爲空的目錄纔會被刪除。

  1. [app@hadoop-master ~]$ curl -i -X DELETE  
  2. "http://hadoop-master:14000/webhdfs/v1/tmp/webhdfs-new?op=DELETE&user.name=app"  
  3. HTTP/1.1 200 OK  
  4. Server: Apache-Coyote/1.1  
  5. Set-Cookie:hadoop.auth="u=app&p=app&t=simple&e=1393421161052&s=r03LOm2hO91ujcc66wNWyMJnDx4=";Version=1; Path=/  
  6. Content-Type: application/json  
  7. Transfer-Encoding: chunked  
  8. Date: Wed, 26 Feb 2014 03:26:01 GMT  
  9.    
  10. {"boolean":true}
複製代碼
覈查是否正確地執行


  1. [app@hadoop-master ~]$ hdfs dfs -ls /tmp  
  2. Found 4 items  
  3. drwx------   -app supergroup          0 2014-01-0808:02 /tmp/hadoop-yarn  
  4. drwxr-xr-x   -app supergroup          0 2014-02-0209:43 /tmp/hdfs_out  
  5. drwxr-xr-x   -app supergroup          0 2014-02-2022:39 /tmp/hive-app  
  6. drwxr-xr-x   -app supergroup          0 2014-02-2504:25 /tmp/jps
複製代碼


總結   

WebDFS提供了一個簡單、標準的方式來執行Hadoop 文件系統操作,這個客戶端不必運行在Hadoop集羣本身中。WebHDFS最主要的特性是讓客戶端通過預定義端口直接地鏈接namenode 和 datanode。這樣的話,規避了好多HDFS proxy的代理層以及預先配置Tomcat的綁定。WebHDFD API是可互相交換的,這樣讓客戶端不需要去打開防火牆端口。

兩種常見的異常:
1. HTTP/1.1 405 HTTP method PUT is not supported by this URL
   修改hdfs-site.xml文件的權限屬性。
  1. <property>  
  2. <name>dfs.permissions</name>  
  3. <value>false</value>  
  4. </property>  
複製代碼


2 {"RemoteException":{"exception":"AccessControlException","javaClassName":"org.apache.hadoop.security.AccessControlException","message":"Permission denied: user=dr.who, access=WRITE, inode=\"/qzhang\":Administrator:supergroup:drwxr-xr-x\n\tat
   問題2與問題1是一個問題,解決方案同上。






#################################################
http://blog.csdn.net/john_f_lau/article/details/20007299
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章