官术网_书友最值得收藏!

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

主站蜘蛛池模板: 博白县| 宜章县| 田林县| 巫溪县| 四平市| 石城县| 彭州市| 临武县| 赤壁市| 宜川县| 育儿| 大兴区| 文安县| 澄江县| 武宣县| 正安县| 封开县| 法库县| 吉首市| 大庆市| 南开区| 青川县| 郧西县| 喀喇沁旗| 金阳县| 梨树县| 兰考县| 敦煌市| 五寨县| 巫山县| 曲周县| 淮安市| 重庆市| 措美县| 衡水市| 肥东县| 雅江县| 顺平县| 长垣县| 镇康县| 久治县|