Python hdfs 读取文件报错 Temporary failure in name resolution

发布时间 2023-12-22 11:21:48作者: 绪文

问题背景

本人按照菜鸟教程的步骤,在windows系统布置了ubuntu虚拟机环境,并使用centos容器镜像搭建出单节点的hdfs服务。

欲使用Python hdfs api测试hdfs服务的功能,遂在ubuntu中编写以下代码准备测试

from hdfs import Client

client = Client(url='http://172.17.0.3:9870', root='/')

print(client.list('/'))
print(client.status(hdfs_path='/hello/hello.txt', strict=False))
with client.read('/hello/hello.txt') as f:
    print(f.read().decode())
client.download('/hello/hello.txt', '/home/luca/test.txt')

在运行的时候,client.list和client.status这2个方法可以正常返回,列出根路径下的所有目录和/hello/hello.txt这个文件的状态信息。但是涉及到文件读取的client.read和download都会报错,如下
image

问题解决

在先后检查了hdfs的client和防火墙无果后,我刷到了这篇帖子https://cloud.tencent.com/developer/article/1406347
虽然没有直接解决我的问题,但是引导我把目光锁定在hosts相关的配置上。
结合报错信息,我将hdfs服务容器的ip和container id配置进/etc/hosts中,成功解决问题
/etc/hosts

172.17.0.3 hdfs_single
172.17.0.3 decad80d3cb7

docker ps (hdfs容器信息)

decad80d3cb7   hadoop_proto   "/usr/sbin/init"   19 hours ago   Up 19 hours             hdfs_single

测试脚本执行成功
image