1. 程式人生 > 程式設計 >python訪問hdfs的操作

python訪問hdfs的操作

pip install hdfs

python 讀取hdfs目錄或檔案

import hdfs
 
client =hdfs.Client("http://10.10.1.4:50070")
fileDir="/user/hive/warehouse/house.db/dm_house/dt=201800909"
try:
  status=client.status(fileDir,False)
  if status:
    print (status)
    rst=client.download(fileDir,"/home/dev/gewei")
    print (rst)
exception Exception as e:
  print (e)

補充知識:用python訪問hdfs出現webhdfs找不到的情況

有可能是webhdfs服務沒有開啟

向hdfs-site.xml檔案中新增屬性:

<property> 
  <name>dfs.webhdfs.enabled</name> 
  <value>true</value> 
</property> 

可以使用如下命令檢測,

獲得目錄的列表:

curl -i "http://Hadoop:50070/webhdfs/v1/?user.name=hadoop&op=LISTSTATUS"

以上這篇python訪問hdfs的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。