- Hands-On Artificial Intelligence for IoT
- Amita Kapoor
- 154字
- 2021-07-02 14:02:02
Using hdfs3 with HDFS
hdfs3 is a lightweight Python wrapper around the C/C++ libhdfs3 library. It allows us to use HDFS natively from Python. To start, we first need to connect with the HDFS NameNode; this is done using the HDFileSystem class:
from hdfs3 import HDFileSystem
hdfs = HDFileSystem(host = 'localhost', port=8020)
This automatically establishes a connection with the NameNode. Now, we can access a directory listing using the following:
print(hdfs.ls('/tmp'))
This will list all the files and directories in the tmp folder. You can use functions such as mkdir to make a directory and cp to copy a file from one location to another. To write into a file, we open it first using the open method and use write:
with hdfs.open('/tmp/file1.txt','wb') as f:
f.write(b'You are Awesome!')
Data can be read from the file:
with hdfs.open('/tmp/file1.txt') as f:
print(f.read())
You can learn more about hdfs3 from its documentation: https://media.readthedocs.org/pdf/hdfs3/latest/hdfs3.pdf.
推薦閱讀
- Mastering Mesos
- 火格局的時空變異及其在電網防火中的應用
- Mastercam 2017數控加工自動編程經典實例(第4版)
- 基于LPC3250的嵌入式Linux系統開發
- Cloud Analytics with Microsoft Azure
- 小型電動機實用設計手冊
- Windows程序設計與架構
- Linux服務與安全管理
- 網絡服務搭建、配置與管理大全(Linux版)
- Mastering Exploratory Analysis with pandas
- 智能鼠原理與制作(進階篇)
- Windows安全指南
- 電氣控制及Micro800 PLC程序設計
- 數字多媒體技術與應用實例
- Hands-On Geospatial Analysis with R and QGIS