python上传文件到hdfs
时间: 2023-08-17 07:24:32 浏览: 155
可以使用hdfs3或pyarrow库来实现Python上传文件到HDFS的功能。以下是使用hdfs3库上传文件的示例代码:
```python
import hdfs3
client = hdfs3.HDFileSystem(host='your_hdfs_host', port=your_hdfs_port)
with client.open('/path/on/hdfs/file.txt', 'wb') as f:
with open('/path/on/local/file.txt', 'rb') as lf:
f.write(lf.read())
```
其中,`your_hdfs_host`和`your_hdfs_port`需要替换为实际的HDFS主机和端口,`/path/on/hdfs/file.txt`是上传后在HDFS上的文件路径,`/path/on/local/file.txt`是本地待上传的文件路径。
使用pyarrow库上传文件的示例代码如下:
```python
import pyarrow.hdfs
fs = pyarrow.hdfs.connect(host='your_hdfs_host', port=your_hdfs_port)
with fs.open('/path/on/hdfs/file.txt', 'wb') as f:
with open('/path/on/local/file.txt', 'rb') as lf:
f.write(lf.read())
```
同样地,`your_hdfs_host`和`your_hdfs_port`需要替换为实际的HDFS主机和端口,`/path/on/hdfs/file.txt`是上传后在HDFS上的文件路径,`/path/on/local/file.txt`是本地待上传的文件路径。
阅读全文