Python 操作HDFS模塊
- python HDFS模塊
- 實體化客戶端
- 讀寫檔案
- 讀取檔案至記憶體
- 結合以下write() 方法將檔案寫入HDFS
- Kerberos認證訪問
- 列出目錄
- 創建目錄(如要寫入,建議使用write方法,沒有會自動創建)
- 洗掉
- 檢查檔案是否存在
- 獲取HDFS上檔案或者目錄(可用于判斷是否存在)
- 移動檔案或檔案夾
- 參考文獻
python HDFS模塊
安裝:pip install hdfs
介紹:該模塊用于python與HDFS的namenode建立鏈接從而獲取到datanode進行操作
鏈接方式:webHDFS
實體化客戶端
from hdfs import InsecureClient
client = InsecureClient(‘http://host:port’, user=‘ann’)
insecureClient 可以添加用戶引數、或者安全認證
from hdfs import Config
client = Config().get_client(‘dev’)
讀寫檔案
讀取檔案至記憶體
with client.read(‘features’) as reader:
features = reader.read()
結合以下write() 方法將檔案寫入HDFS
client.write(‘data/records.jsonl’, data=features, encoding=‘utf-8’)
相關引數:
write(hdfs_path,data = None,overwrite = False,permission= None,blocksize = None,replication= None,buffersize = None,append = False,encoding = None )
[hdfs_path]:寫入路徑,沒有會自動創建
[data]:本地資料,可以為變數
[overwrite]:是否允許覆寫
[permission]:權限
[blocksize]:檔案大小
[replication]:檔案復制數量
[buffersize]:上傳區域大小,類似緩沖區
[append]:附加到檔案而不是創建一個新檔案,
[encoding]:用于對寫入的資料進行序列化的編碼,
Kerberos認證訪問
client = KerberosClient(‘http://host:port’)
列出目錄
list(hdfs_path, status=False)
[status]:回傳每個檔案的對應FileStatus,
創建目錄(如要寫入,建議使用write方法,沒有會自動創建)
makedirs(hdfs_path, permission=None)
洗掉
delete(hdfs_path, recursive=False, skip_trash=True)
[recursive]:默認情況下,如果嘗試刪除非空目錄,此方法將引發hdfs error,
[skip_trash]:跳過垃圾回收
檢查檔案是否存在
checksum(hdfs_path )
必須指向一個檔案
獲取HDFS上檔案或者目錄(可用于判斷是否存在)
content(hdfs_path,strict = True )
[strict]:如果為False,則回傳,None而不是在路徑不存在的情況下引發例外,
移動檔案或檔案夾
rename(hdfs_src_path,hdfs_dst_path )
[hdfs_src_path]:源路徑
[hdfs_dst_path]:目標路徑,存在移入,不存在引發hfds error例外
參考文獻
[HDFS官方API檔案]:https://hdfscli.readthedocs.io/en/latest/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/271934.html
標籤:其他
