1. 程式人生 > >使用python的hdfs包操作分散式檔案系統(HDFS)

使用python的hdfs包操作分散式檔案系統(HDFS)

=====================================================================================

寫在前邊的話:

        之前做的Hadoop叢集,組合了HiveHbase,sqoop,Spark等開源工具,現在要對他們做一個Web的視覺化操作,由於本小白只懂如何使用Python做一個互動的web應用,所以這裡就選擇了python的Django

        Django教程參考:Django從manage.py shell 到專案部署

        hadoop叢集操作請參考:三臺PC伺服器部署高可用hadoop叢集


言歸正傳:

       使用python操作hdfs本身並不難,只不過是把對應的shell 功能“翻譯”成高階語言,網上大部分使用的是

       pyhdfs:官方文件

       hdfs:官方文件

       libhdfs(比較狗血)

       我這裡選用的是hdfs,下邊的例項都是基於hdfs包進行的

1:安裝

      由於我的是windows環境(Linux其實也一樣),只要有pip或者setup_install安裝起來都是很方便的

[plain]  view plain  copy
  1. pip install hdfs  

2:Client——建立叢集連線

[python]  view plain  copy
  1. >>> from hdfs import *  
  2. >>> client = Client("http://127.0.0.1:50070")  

       其他引數說明:

       classhdfs.client.Client(urlroot=Noneproxy=Nonetimeout=Nonesession=None)

                    url:ip:埠

                    root:制定的hdfs根目錄

                    proxy:制定登陸的使用者身份

                    timeout:設定的超時時間

                    seesion:requests.Session instance, used to emit all requests.(不是太懂,應該四使用者發出請求)

       這裡我們著重看一下proxy這個,首先我們指定root使用者連線

[html]  view plain  copy
  1. >>> client = Client("http://127.0.0.1:50070",root="/",timeout=100,session=False)  
  2. >>> client.list("/")  
  3. [u'hbase']  
       看起來一切正常的樣子,接下來我們指定一個別的使用者,比如說gamer再看 [html]  view plain  copy
  1. >>> client = Client("http://127.0.0.1:50070",root="/",proxy="gamer",timeout=100,session=False)  
  2. >>> client.list("/")  
  3. Traceback (most recent call last):  
  4.   File "<stdin>", line 1, in <module>  
  5.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 893, in list  
  6.     statuses = self._list_status(hdfs_path).json()['FileStatuses']['FileStatus']  
  7.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 92, in api_handler  
  8.     **self.kwargs  
  9.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 181, in _request  
  10.     return _on_error(response)  
  11.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 44, in _on_error  
  12.     raise HdfsError(message)  
  13. hdfs.util.HdfsError: Failed to obtain user group information: org.apache.hadoop.security.authorize.AuthorizationException: User: dr.who is not allowed to impersonate gamer  
       這時候就丟擲異常了

3:dir——檢視支援的方法

[python]  view plain  copy
  1. >>> dir(client)  
  2. ['__class__''__delattr__''__dict__''__dir__''__doc__''__eq__''__format__''__ge__''__getattribute__''__gt__',   
  3. '__hash__''__init__''__le__''__lt__''__module__''__ne__''__new__''__reduce__''__reduce_ex__''__registry__',  
  4.  '__repr__''__setattr__''__sizeof__''__str__''__subclasshook__''__weakref__''_append''_create''_delete',  
  5.  '_get_content_summary''_get_file_checksum''_get_file_status''_get_home_directory''_list_status''_mkdirs''_open',  
  6.  '_proxy''_rename''_request''_session''_set_owner''_set_permission''_set_replication''_set_times''_timeout',   
  7. 'checksum''content''delete''download''from_options''list''makedirs''parts''read''rename''resolve''root',  
  8.  'set_owner''set_permission''set_replication''set_times''status''upload',  
  9.  'url''walk''write']  

4:status——獲取路徑的具體資訊

[python]  view plain  copy
  1. >>> client.status("/")  
  2. {'accessTime'0'pathSuffix''''group''supergroup''type''DIRECTORY''owner''root''childrenNum'4'blockSize'0,  
  3.  'fileId'16385'length'0'replication'0'storagePolicy'0'modificationTime'1473023149031'permission''777'}  

      其他引數:status(hdfs_pathstrict=True)

               hdfs_path:就是hdfs路徑

               strict:設定為True時,如果hdfs_path路徑不存在就會丟擲異常,如果設定為False,如果路徑為不存在,則返回None

[python]  view plain  copy
  1. >>> client = Client("http://127.0.0.1:50070",root="/",timeout=100,session=False)  
  2. >>> client.status("/gamer",strict=True)  
  3. Traceback (most recent call last):  
  4.   File "<stdin>", line 1in <module>  
  5.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 277in status  
  6.     res = self._get_file_status(hdfs_path, strict=strict)  
  7.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 92in api_handler  
  8.     **self.kwargs  
  9.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 181in _request  
  10.     return _on_error(response)  
  11.   File "/usr/local/lib/python2.7/dist-packages/hdfs/client.py", line 44in _on_error  
  12.     raise HdfsError(message)  
  13. hdfs.util.HdfsError: File does not exist: /gamer  
  14. >>> client.status("/gamer",strict=False)  
  15. >>>  
      從例子中可以看出,當設定為false時,路徑不存在,什麼也不輸出

5:list——獲取指定路徑的子目錄資訊

[python]  view plain  copy
  1. >>> client.list("/")  
  2. ['file''gyt''hbase''tmp']  

     其他引數:list(hdfs_pathstatus=False)

      &nb