python 操作CDH的HDFS

環境:安裝了pyenv,pip
pyenv global 3.6.8

pip install hdfs
pip install ipython  (個人喜歡使用pyenv的自動補全功能)
安裝完hdfs後建議重啓,測試過程出現了hdfs模塊找不到的情況
注意:客戶端分爲Client和InsecureClient,Client需要驗證權限

In [1]: from hdfs import InsecureClient                                                                                                                                
In [2]: client_hdfs = InsecureClient('http://192.168.4.86:9870',user='hdfs')                                                                                           
In [3]: client_hdfs.list('/')                                                                                                                                          
Out[3]: ['dongsc', 'hbase', 'tmp', 'user']
In [4]: client_hdfs.makedirs('/dongsc2')  
In [6]: client_hdfs.delete('/dongsc',recursive='true')       --recursive:是否允許遞歸刪除                                                                                                         
Out[6]: True


假如想使用Client添加對權限的認證,需要修改授權,將root用戶添加進ACLs中
Client模塊下,儘管當前用戶是root,但是hadoop 裏的hdfs是另外一套文件系統了,root在裏面是沒有權限的。
上傳文件時需要注意:
客戶端(運行python的機器)必須要有NameNode和DataNode的解析,可以是DNS,也可以是/etc/hosts,否則會出現:

。。。。。。。。。。。。。。。省略,下面是報錯的最後一句。。。。
/usr/local/lib/python3.6/site-packages/urllib3/connection.py in _new_conn(self)
    167         except SocketError as e:
    168             raise NewConnectionError(
--> 169                 self, "Failed to establish a new connection: %s" % e)
    170 
    171         return conn
NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f734ac449e8>: Failed to establish a new connection: [Errno -2] Name or service not known

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章