You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Python连接Linux端HDFS出现ConnectionError的解决咨询

问题:Windows Python无法连接Linux上的HDFS

我是HDFS新手,已在Linux系统的HDFS中存储了若干数据集。现在本地Windows机器上运行的Python代码需要连接该HDFS,编写的代码如下:

# Set HDFS client
client = InsecureClient('http://192.168.131.129:9000', user='******')

# Function to find the answer based on similarity matching
def find_answer(question):
    # Load questions and answers from .dat files
    with client.read('/ana/questions.dat') as reader:
        questions = [line.strip() for line in reader.read().decode('utf-8').splitlines()]

    with client.read('/ana/answers.dat') as reader:
        answers = [line.strip() for line in reader.read().decode('utf-8').splitlines()]

每次运行都会报错:

ConnectionError: HTTPConnectionPool(host='192.168.131.129', port=55453): Max retries exceeded with url: /webhdfs/v1/ana/questions.dat?user.name=******&offset=0&op=OPEN (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001C3222EA770>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

通过jps查看Namenode状态:

56144 NodeManager
60593 Jps
56021 ResourceManager
55787 SecondaryNameNode
55453 NameNode
55581 DataNode

已修改的core-site.xml配置:

<configuration>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>
    <property>
        <name>hadoop.tmp.dir</name>
        <value>/home/aviparna/hdata</value>
    </property>
</configuration>

hdfs-site.xml配置:

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
    <property>
        <name>dfs.webhdfs.enabled</name>
        <value>true</value>
    </property>
</configuration>

HDFS的WebUI已正常运行,请问如何解决两者的连接问题?


解决方案

1. 修正NameNode绑定地址

你的core-site.xml中fs.defaultFS设置为hdfs://localhost:9000,导致NameNode仅绑定本地回环地址,外部Windows机器无法通过192.168.131.129访问。修改为Linux机器的实际IP:

<property>
    <name>fs.defaultFS</name>
    <value>hdfs://192.168.131.129:9000</value>
</property>

同时检查hadoop-env.sh,如果有HADOOP_NAMENODE_OPTS指定-Djava.rmi.server.hostname=localhost,改为192.168.131.129。

修改后重启HDFS服务:

stop-dfs.sh
start-dfs.sh

2. 调整Python客户端的WebHDFS端口

InsecureClient(来自hdfs库)连接的是WebHDFS的HTTP端口,而非HDFS的RPC端口9000。WebHDFS默认HTTP端口为50070,修改Python代码中的连接地址:

client = InsecureClient('http://192.168.131.129:50070', user='******')

3. 开放Linux机器的防火墙端口

确保Linux机器开放WebHDFS端口(50070),临时关闭防火墙测试或永久开放端口:

# 临时关闭firewalld
systemctl stop firewalld

# 或永久开放端口
firewall-cmd --add-port=50070/tcp --permanent
firewall-cmd --reload

4. 验证网络连通性

在Windows机器上执行以下命令确认网络可达:

# 测试IP连通性
ping 192.168.131.129

# 测试端口开放状态
telnet 192.168.131.129 50070

若telnet能成功连接,说明网络和端口无问题。

5. 确认HDFS文件权限

确保目标目录和文件对指定用户有读权限:

# 查看目录权限
hdfs dfs -ls /ana

# 递归设置读权限
hdfs dfs -chmod -R 755 /ana

内容的提问来源于stack exchange,提问作者Daremitsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:52:49