環(huán)境:安裝了pyenv,pip
pyenv global 3.6.8
pip install hdfs
pip install ipython ?(個(gè)人喜歡使用pyenv的自動(dòng)補(bǔ)全功能)
安裝完hdfs后建議重啟,測試過程出現(xiàn)了hdfs模塊找不到的情況
注意:客戶端分為Client和InsecureClient,Client需要驗(yàn)證權(quán)限
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添加對權(quán)限的認(rèn)證,需要修改授權(quán),將root用戶添加進(jìn)ACLs中
Client模塊下,盡管當(dāng)前用戶是root,但是hadoop 里的hdfs是另外一套文件系統(tǒng)了,root在里面是沒有權(quán)限的。
上傳文件時(shí)需要注意:
客戶端(運(yùn)行python的機(jī)器)必須要有NameNode和DataNode的解析,可以是DNS,也可以是/etc/hosts,否則會出現(xiàn):
。。。。。。。。。。。。。。。省略,下面是報(bào)錯(cuò)的最后一句。。。。
/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:
: Failed to establish a new connection: [Errno -2] Name or service not known
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
