- 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.
推薦閱讀
- Instant Raspberry Pi Gaming
- Visualforce Development Cookbook(Second Edition)
- JavaScript實例自學手冊
- 西門子S7-200 SMART PLC從入門到精通
- 人工智能工程化:應用落地與中臺構建
- CompTIA Network+ Certification Guide
- 網絡布線與小型局域網搭建
- 天才與算法:人腦與AI的數學思維
- 菜鳥起飛五筆打字高手
- Appcelerator Titanium Smartphone App Development Cookbook(Second Edition)
- INSTANT R Starter
- Deep Learning with PyTorch Quick Start Guide
- 仿蛛機器人的設計與制作
- 亮劍.NET:圖解ASP.NET網站開發實戰
- 中文版Photoshop CS6數碼照片處理高手速成