记录一次读取hdfs文件时出现的问题java.net.ConnectException: Connection refused

公司的hadoop集群是之前的同事搭建的,我(小白一个)在spark shell中读取hdfs上的文件时,执行以下指令

>>> word=sc.textFile("hdfs://localhost:9000/user/hadoop/test.txt")
>>> word.first()

报错:java.net.ConnectException: Call From hadoop/133.0.123.130 to localhost:9000 failed on connection exception: java.net.ConnectException: Connection refuse。看来是hdfs与本地服务器连接出了问题,我又查看了以下hdfs上的文件,发现可以正常查看,这说明本地服务器与hdfs的连接、通信是没有问题的!思来想去,我有换另外一种方式读取hdfs上的文件

>>> word=sc.textFile("/user/hadoop/test.txt")
>>> word.first()

因为spark中默认读取的就是hdfs上的文件,因此这种方式也是可以的,结果发现运行正常,这下问题就明了了,是“localhost:9000”出了问题,我查看了hadoop/etc/core-site.xml中端口设置

显示端口设置正常,那么应该是localhost对应的IP地址不对了!接着查看hosts文件,发现

发现localhost对应的IP地址与本地服务器IP地址不一致,终于找到原因了,我将读取hdfs文件的指令更改为:

>>> word=sc.textFile("hdfs://hadoop:9000/user/hadoop/test.txt")
>>> word.first()

结果正常了。

原文地址:https://www.cnblogs.com/hgz-dm/p/11356357.html